use of uk.ac.bbsrc.tgac.miso.core.data.impl.FileAttachment in project miso-lims by miso-lims.
the class DefaultFileAttachmentService method add.
@Override
public void add(Attachable object, MultipartFile file, AttachmentCategory category) throws IOException {
Attachable managed = attachableStore.getManaged(object);
String relativeDir = makeRelativeDir(object.getAttachmentsTarget(), object.getId());
File targetFile = storeFile(relativeDir, file);
try {
FileAttachment attachment = makeAttachment(file, relativeDir, targetFile, category);
managed.getAttachments().add(attachment);
attachableStore.save(managed);
} catch (Exception e) {
if (!targetFile.delete()) {
log.error("Failed to save attachment, but file was still saved: {}", targetFile.getAbsolutePath());
}
throw e;
}
}
use of uk.ac.bbsrc.tgac.miso.core.data.impl.FileAttachment in project miso-lims by miso-lims.
the class DefaultFileAttachmentService method addLinks.
@Override
public void addLinks(Collection<Attachable> objects, FileAttachment attachment) throws IOException {
List<Attachable> managed = objects.stream().map(attachableStore::getManaged).collect(Collectors.toList());
FileAttachment managedAttachment = getManaged(attachment);
for (Attachable item : managed) {
addLinkIfNecessary(item, managedAttachment);
}
}
use of uk.ac.bbsrc.tgac.miso.core.data.impl.FileAttachment in project miso-lims by miso-lims.
the class DefaultFileAttachmentService method makeAttachment.
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());
attachableStore.save(attachment);
return attachment;
}
use of uk.ac.bbsrc.tgac.miso.core.data.impl.FileAttachment in project miso-lims by miso-lims.
the class DefaultFileAttachmentService method delete.
@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);
}
}
use of uk.ac.bbsrc.tgac.miso.core.data.impl.FileAttachment in project miso-lims by miso-lims.
the class DefaultFileAttachmentService method afterDelete.
@Override
public void afterDelete(Attachable object) throws IOException {
// Delete attachments from the object and the associated files if they are not used by anything else
if (object.getPendingAttachmentDeletions() != null) {
for (FileAttachment attachment : object.getPendingAttachmentDeletions()) {
File file = new File(makeFullPath(attachment.getPath()));
if (file.exists() && attachableStore.getUsage(attachment) == 0) {
attachableStore.delete(attachment);
deleteFileOrLog(file, object, attachment);
}
}
}
// Delete the entity's attachments directory and anything in it
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());
}
}
}
Aggregations