private FileAttachment getManaged(FileAttachment attachment) { FileAttachment managedAttachment = attachableStore.getAttachment(attachment.getId()); if (managedAttachment == null) { throw new IllegalArgumentException("Attachment not found"); } return managedAttachment; }
private FileAttachment makeAttachment(MultipartFile sourceFile, String relativeDir, File targetFile, AttachmentCategory category) throws IOException { FileAttachment attachment = new FileAttachment(); attachment.setFilename(sourceFile.getOriginalFilename()); attachment.setPath(relativeDir + File.separator + targetFile.getName()); attachment.setCategory(category); attachment.setCreator(authorizationManager.getCurrentUser()); attachment.setCreationTime(new Date()); return attachment; }
public static AttachmentDto asDto(@Nonnull FileAttachment from) { AttachmentDto dto = new AttachmentDto(); setId(dto::setId, from); setString(dto::setFilename, from.getFilename()); setString(dto::setPath, from.getPath()); setString(dto::setCategory, maybeGetProperty(from.getCategory(), AttachmentCategory::getAlias)); setString(dto::setCreator, maybeGetProperty(from.getCreator(), User::getLoginName)); setDateString(dto::setCreated, from.getCreationTime()); return dto; }
@Override public void delete(Attachable object, FileAttachment attachment) throws IOException { if (!object.getAttachments().contains(attachment)) { throw new IllegalArgumentException("Attachment does not belong to this object"); } Attachable managed = attachableStore.getManaged(object); FileAttachment managedAttachment = managed.getAttachments().stream() .filter(a -> a.getId() == attachment.getId()) .findFirst().orElseThrow(() -> new IllegalArgumentException("Attachment not found in persisted entity")); authorizationManager.throwIfNonAdminOrMatchingOwner(managedAttachment.getCreator()); File file = new File(makeFullPath(managedAttachment.getPath())); if (file.exists()) { if (!file.isFile()) { throw new IOException("Cannot delete. Not a file"); } if (!file.canWrite()) { throw new IOException("Cannot delete file. Permission denied"); } } managed.getAttachments().remove(managedAttachment); attachableStore.save(managed); // Only delete the file if it is not attached to any other items if (file.exists() && attachableStore.getUsage(managedAttachment) == 1) { attachableStore.delete(managedAttachment); deleteFileOrLog(file, managed, managedAttachment); } }
private void deleteFileOrLog(File file, Attachable object, FileAttachment attachment) { if (!file.delete()) { log.error("File no longer associated with an object, but failed to delete: {}{} from {} {}", new Object[] { file.getName(), (attachment == null ? "" : " (" + attachment.getFilename() + ")"), object.getAttachmentsTarget(), Long.valueOf(object.getId()) }); } }
@Override public void afterDelete(Attachable object) { for (FileAttachment a : object.getAttachments()) { File file = new File(makeFullPath(a.getPath())); deleteFileOrLog(file, object, a); } File dir = new File(makeFullPath(makeRelativeDir(object.getAttachmentsTarget(), object.getId()))); if (dir.exists() && dir.isDirectory()) { if (dir.listFiles() != null) { for (File file : dir.listFiles()) { deleteFileOrLog(file, object, null); } } if (!dir.delete()) { log.error("Failed to delete directory for deleted object: {}", dir.getAbsolutePath()); } } }
@Override public long getUsage(FileAttachment attachment) { AttachmentUsage view = (AttachmentUsage) currentSession().createCriteria(AttachmentUsage.class) .add(Restrictions.eq("attachmentId", attachment.getId())) .uniqueResult(); if (view == null) { throw new IllegalArgumentException("No persisted attachment found with ID " + attachment.getId()); } return view.getUsage(); }