Long blogId = uploadItem.getBlogId();
Long commentId = uploadItem.getCommentId();
// MultipartFile file = uploadItem.getFileData();
String uploadType = uploadItem.getUploadType();
Message message;
if (file.getSize() > 0) {
if (uploadType.equalsIgnoreCase("entry")) {
// Construct Attachment object
EntryAttachment entryAttachment = new EntryAttachment();
entryAttachment.setFileName(getFileName(file));
entryAttachment.setFileData(IOUtils.toByteArray(file.getInputStream()));
entryAttachment.setContentType(file.getContentType());
Entry entry = entryService.findById(blogId);
entryAttachment.setEntry(entry);
entry.addAttachment(entryAttachment);
entryService.save(entry);
} else {
// Construct Attachment object
CommentAttachment commentAttachment = new CommentAttachment();
commentAttachment.setFileName(getFileName(file));
commentAttachment.setFileData(IOUtils.toByteArray(file.getInputStream()));
commentAttachment.setContentType(file.getContentType());
Comment comment = commentService.findById(commentId);
commentAttachment.setComment(comment);
comment.addAttachment(commentAttachment);
commentService.save(comment);
}
message = new Message("success", "File '" + getFileName(file)
+ "' uploaded successfully");
redirectAttributes.addFlashAttribute("message", message);
} else {
redirectAttributes.addFlashAttribute("validationResult", "error");
message = new Message("error", "Please select a file for upload");
redirectAttributes.addFlashAttribute("message", message);
}
return "redirect:/blogs/"
+ UrlUtil.encodeUrlPathSegment(blogId.toString(), request);