Search in sources :

Example 1 with FileAttachment

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;
    }
}
Also used : FileAttachment(uk.ac.bbsrc.tgac.miso.core.data.impl.FileAttachment) Attachable(uk.ac.bbsrc.tgac.miso.core.data.Attachable) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) IOException(java.io.IOException)

Example 2 with FileAttachment

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);
    }
}
Also used : FileAttachment(uk.ac.bbsrc.tgac.miso.core.data.impl.FileAttachment) Attachable(uk.ac.bbsrc.tgac.miso.core.data.Attachable)

Example 3 with FileAttachment

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;
}
Also used : FileAttachment(uk.ac.bbsrc.tgac.miso.core.data.impl.FileAttachment) Date(java.util.Date)

Example 4 with FileAttachment

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);
    }
}
Also used : FileAttachment(uk.ac.bbsrc.tgac.miso.core.data.impl.FileAttachment) IOException(java.io.IOException) Attachable(uk.ac.bbsrc.tgac.miso.core.data.Attachable) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile)

Example 5 with FileAttachment

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());
        }
    }
}
Also used : FileAttachment(uk.ac.bbsrc.tgac.miso.core.data.impl.FileAttachment) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile)

Aggregations

FileAttachment (uk.ac.bbsrc.tgac.miso.core.data.impl.FileAttachment)17 Attachable (uk.ac.bbsrc.tgac.miso.core.data.Attachable)10 Test (org.junit.Test)6 AbstractDAOTest (uk.ac.bbsrc.tgac.miso.AbstractDAOTest)6 File (java.io.File)5 MultipartFile (org.springframework.web.multipart.MultipartFile)5 IOException (java.io.IOException)4 Date (java.util.Date)3 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)3 PostMapping (org.springframework.web.bind.annotation.PostMapping)2 User (com.eaglegenomics.simlims.core.User)1 FileInputStream (java.io.FileInputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 Assert (org.junit.Assert)1 Before (org.junit.Before)1 Rule (org.junit.Rule)1 ExpectedException (org.junit.rules.ExpectedException)1 NotFoundException (org.springframework.security.acls.model.NotFoundException)1 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)1