Search in sources :

Example 1 with Attachable

use of uk.ac.bbsrc.tgac.miso.core.data.Attachable 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 Attachable

use of uk.ac.bbsrc.tgac.miso.core.data.Attachable 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 Attachable

use of uk.ac.bbsrc.tgac.miso.core.data.Attachable 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 4 with Attachable

use of uk.ac.bbsrc.tgac.miso.core.data.Attachable in project miso-lims by miso-lims.

the class DefaultFileAttachmentService method addShared.

@Override
public void addShared(Collection<Attachable> objects, MultipartFile file, AttachmentCategory category) throws IOException {
    List<Attachable> managed = objects.stream().map(attachableStore::getManaged).collect(Collectors.toList());
    if (managed.stream().map(Attachable::getAttachmentsTarget).distinct().count() > 1L) {
        throw new IllegalArgumentException("Target objects are not all the same type");
    }
    String relativeDir = makeRelativeSharedDir(managed.get(0).getAttachmentsTarget());
    File targetFile = storeFile(relativeDir, file);
    try {
        FileAttachment attachment = makeAttachment(file, relativeDir, targetFile, category);
        for (Attachable item : managed) {
            item.getAttachments().add(attachment);
            attachableStore.save(item);
        }
    } 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 5 with Attachable

use of uk.ac.bbsrc.tgac.miso.core.data.Attachable in project miso-lims by miso-lims.

the class AttachmentRestController method bulkLinkFile.

@PostMapping("/{entityType}/shared")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void bulkLinkFile(@PathVariable String entityType, @RequestParam(name = "fromEntityType", required = true) String fromEntityType, @RequestParam(name = "fromEntityId", required = true) long fromEntityId, @RequestParam(name = "attachmentId", required = true) long attachmentId, @RequestParam(name = "entityIds", required = true) String entityIds) throws IOException {
    List<Attachable> items = new ArrayList<>();
    for (Long id : LimsUtils.parseIds(entityIds)) {
        Attachable item = fileAttachmentService.get(entityType, id);
        if (item == null) {
            throw new ClientErrorException(String.format("%s %d not found", entityType, id));
        } else {
            items.add(item);
        }
    }
    // This lookup will ensure that the user has read access to the source item (and its attachments)
    Attachable sourceItem = getAttachable(fromEntityType, fromEntityId, false);
    FileAttachment attachment = sourceItem.getAttachments().stream().filter(att -> att.getId() == attachmentId).findFirst().orElseThrow(() -> new RestException("Attachment not found", Status.BAD_REQUEST));
    fileAttachmentService.addLinks(items, attachment);
}
Also used : FileAttachment(uk.ac.bbsrc.tgac.miso.core.data.impl.FileAttachment) ArrayList(java.util.ArrayList) ClientErrorException(uk.ac.bbsrc.tgac.miso.webapp.controller.component.ClientErrorException) Attachable(uk.ac.bbsrc.tgac.miso.core.data.Attachable) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus)

Aggregations

Attachable (uk.ac.bbsrc.tgac.miso.core.data.Attachable)13 FileAttachment (uk.ac.bbsrc.tgac.miso.core.data.impl.FileAttachment)9 MultipartFile (org.springframework.web.multipart.MultipartFile)6 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)5 File (java.io.File)4 IOException (java.io.IOException)4 PostMapping (org.springframework.web.bind.annotation.PostMapping)4 ArrayList (java.util.ArrayList)2 NotFoundException (org.springframework.security.acls.model.NotFoundException)2 AttachmentCategory (uk.ac.bbsrc.tgac.miso.core.data.impl.AttachmentCategory)2 ClientErrorException (uk.ac.bbsrc.tgac.miso.webapp.controller.component.ClientErrorException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 FileInputStream (java.io.FileInputStream)1 OutputStream (java.io.OutputStream)1 Test (org.junit.Test)1 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1 AbstractDAOTest (uk.ac.bbsrc.tgac.miso.AbstractDAOTest)1 Sample (uk.ac.bbsrc.tgac.miso.core.data.Sample)1 SampleImpl (uk.ac.bbsrc.tgac.miso.core.data.impl.SampleImpl)1