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;
}
}
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);
}
}
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);
}
}
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;
}
}
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);
}
Aggregations