From c6849e3665821431772c50d15e4736b13a56af6b Mon Sep 17 00:00:00 2001 From: Michael Kimsal Date: Thu, 12 Mar 2015 12:24:38 -0400 Subject: [PATCH] service helper method to copy attachments from one object to another --- README.md | 16 ++++++++ .../services/AttachmentableService.groovy | 38 ++++++++++++++----- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index f9be165..885ef58 100644 --- a/README.md +++ b/README.md @@ -155,6 +155,22 @@ To add some logic to handle newly created attachments, just add an "onAddAttachm } } +To copy all attachments from one object to another, use the copyAttachments() method of the attachmentableService + + class FooController { + + def attachmentableService + + def foo() { + def object1 = Topic.get(1) + def object2 = new Topic().save() + attachmentableService.copyAttachments(object1, object2, "Copy of") + } + } + +The third parameter is a filename prefix to use on copied files (optional). + + ## Internationalization Check the *messages.properties* file from the plugin directory. diff --git a/grails-app/services/com/macrobit/grails/plugins/attachmentable/services/AttachmentableService.groovy b/grails-app/services/com/macrobit/grails/plugins/attachmentable/services/AttachmentableService.groovy index c4382c9..b73c022 100644 --- a/grails-app/services/com/macrobit/grails/plugins/attachmentable/services/AttachmentableService.groovy +++ b/grails-app/services/com/macrobit/grails/plugins/attachmentable/services/AttachmentableService.groovy @@ -24,6 +24,7 @@ import com.macrobit.grails.plugins.attachmentable.util.AttachmentableUtil import org.apache.commons.io.FilenameUtils import com.macrobit.grails.plugins.attachmentable.core.exceptions.EmptyFileException import java.lang.reflect.UndeclaredThrowableException +import java.nio.file.Files class AttachmentableService { @@ -89,31 +90,35 @@ class AttachmentableService { } def addAttachment(def poster, def reference, CommonsMultipartFile file) { - doAddAttachment(grailsApplication.config, poster, reference, file) + doAddAttachment(grailsApplication.config, poster, reference, file, file?.contentType, file?.size, file.name, file.originalFilename) } def addAttachment(def poster, def reference, MultipartFile file) { - doAddAttachment(grailsApplication.config, poster, reference, file) + doAddAttachment(grailsApplication.config, poster, reference, file, file?.contentType, file?.size, file.name, file.originalFilename) } + def addAttachment(def poster, def reference, def file, fileContentType, fileSize, String newName) { + doAddAttachment(grailsApplication.config, poster, reference, file, fileContentType, fileSize, newName, newName) + } + private Object doAddAttachment(def config, def poster, def reference, - def file) { + def file, String fileContentType, def fileSize, String _name, String _originalFilename) { if (reference.ident() == null) { throw new AttachmentableException( "You must save the entity [${delegate}] before calling addAttachment.") } - if (!file?.size) { - throw new EmptyFileException(file.name, file.originalFilename) + if (!fileSize || fileSize==0) { + throw new EmptyFileException(_name, _originalFilename) } String delegateClassName = AttachmentableUtil.fixClassName(reference.class) String posterClass = (poster instanceof String) ? poster : AttachmentableUtil.fixClassName(poster.class.name) Long posterId = (poster instanceof String) ? 0L : poster.id - String filename = file.originalFilename + String filename = _originalFilename // link def link = AttachmentLink.findByReferenceClassAndReferenceId( @@ -130,12 +135,12 @@ class AttachmentableService { name: FilenameUtils.getBaseName(filename), ext: FilenameUtils.getExtension(filename), length: 0L, - contentType: file.contentType, + contentType: fileContentType, // poster posterClass: posterClass, posterId: posterId, // input - inputName: file.name) + inputName: _name) link.addToAttachments attachment if (!link.save(flush: true)) { @@ -145,7 +150,12 @@ class AttachmentableService { // save file to disk File diskFile = AttachmentableUtil.getFile(config, attachment, true) - file.transferTo(diskFile) + if(file instanceof MultipartFile) + { + file.transferTo(diskFile) + } else { + Files.copy(new FileInputStream(file), new FileOutputStream(diskFile)) + } attachment.length = diskFile.length() @@ -350,4 +360,14 @@ class AttachmentableService { result } + def copyAttachments(fromObject, toObject, prefix="") + { + def attachments = findAttachmentsByReference(fromObject) + attachments.each { Attachment att-> + String newName = prefix+att.filename + File oldFile = AttachmentableUtil.getFile(grailsApplication.config, att) + addAttachment("admin", toObject, oldFile, att?.contentType, oldFile?.length(), newName) + } + } + }