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";
}
}
Aggregations