Search in sources :

Example 1 with PhotoUploadValidator

use of cloudinary.lib.PhotoUploadValidator in project cloudinary_java by cloudinary.

the class PhotoController method uploadPhoto.

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String uploadPhoto(@ModelAttribute PhotoUpload photoUpload, BindingResult result, ModelMap model) throws IOException {
    PhotoUploadValidator validator = new PhotoUploadValidator();
    validator.validate(photoUpload, result);
    Map uploadResult = null;
    if (photoUpload.getFile() != null && !photoUpload.getFile().isEmpty()) {
        Singleton.getCloudinary().config.properties.put("connectionManager", connectionManager);
        uploadResult = Singleton.getCloudinary().uploader().upload(photoUpload.getFile().getBytes(), ObjectUtils.asMap("resource_type", "auto"));
        photoUpload.setPublicId((String) uploadResult.get("public_id"));
        photoUpload.setVersion(((Integer) uploadResult.get("version")).longValue());
        photoUpload.setSignature((String) uploadResult.get("signature"));
        photoUpload.setFormat((String) uploadResult.get("format"));
        photoUpload.setResourceType((String) uploadResult.get("resource_type"));
    }
    if (result.hasErrors()) {
        model.addAttribute("photoUpload", photoUpload);
        return "upload_form";
    } else {
        Key photoKey = KeyFactory.createKey("photos", "album");
        Entity photo = new Entity("photo", photoKey);
        photoUpload.toEntity(photo);
        model.addAttribute("upload", uploadResult);
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        datastore.put(photo);
        model.addAttribute("photo", photoUpload);
        return "upload";
    }
}
Also used : Entity(com.google.appengine.api.datastore.Entity) DatastoreService(com.google.appengine.api.datastore.DatastoreService) PhotoUploadValidator(cloudinary.lib.PhotoUploadValidator) ModelMap(org.springframework.ui.ModelMap) Map(java.util.Map) Key(com.google.appengine.api.datastore.Key)

Aggregations

PhotoUploadValidator (cloudinary.lib.PhotoUploadValidator)1 DatastoreService (com.google.appengine.api.datastore.DatastoreService)1 Entity (com.google.appengine.api.datastore.Entity)1 Key (com.google.appengine.api.datastore.Key)1 Map (java.util.Map)1 ModelMap (org.springframework.ui.ModelMap)1