diff --git a/grails-app/controllers/blocks/AnnexableController.groovy b/grails-app/controllers/blocks/AnnexableController.groovy index a228907..a6a3c54 100644 --- a/grails-app/controllers/blocks/AnnexableController.groovy +++ b/grails-app/controllers/blocks/AnnexableController.groovy @@ -8,7 +8,7 @@ import java.nio.file.Files class AnnexableController { - def annexableService + AnnexableService annexableService def find() { String namePart = params.remove('namePart') diff --git a/grails-app/controllers/blocks/test/RepositoryController.groovy b/grails-app/controllers/blocks/test/RepositoryController.groovy index c6e69d6..e086bdc 100644 --- a/grails-app/controllers/blocks/test/RepositoryController.groovy +++ b/grails-app/controllers/blocks/test/RepositoryController.groovy @@ -1,5 +1,6 @@ package blocks.test +import blocks.AnnexableService import grails.converters.JSON import blocks.Annex @@ -8,7 +9,7 @@ import java.nio.file.Files class RepositoryController { - def annexableService + AnnexableService annexableService def index() { def buckets = annexableService.annexesGroupByBucket diff --git a/grails-app/services/blocks/AnnexableService.groovy b/grails-app/services/blocks/AnnexableService.groovy index 5c82b40..1891ab3 100644 --- a/grails-app/services/blocks/AnnexableService.groovy +++ b/grails-app/services/blocks/AnnexableService.groovy @@ -188,6 +188,13 @@ class AnnexableService { annex } + def add(Annex annex, boolean copyLocalFile) { + if (annex.file) { + annex = FileRepo.uploadFile(annex, [:], copyLocalFile) + } + return annex + } + def add(def file, def domainObject) { if (!domainObject) { throw new EmptyDomainObjectException() diff --git a/grailsw b/grailsw old mode 100755 new mode 100644 diff --git a/src/main/groovy/blocks/FileRepo.groovy b/src/main/groovy/blocks/FileRepo.groovy index 66c572f..31fb341 100644 --- a/src/main/groovy/blocks/FileRepo.groovy +++ b/src/main/groovy/blocks/FileRepo.groovy @@ -7,6 +7,7 @@ import blocks.exceptions.UnavailableFileSystemException import java.nio.file.Files import java.nio.file.Path import java.nio.file.Paths +import java.nio.file.StandardCopyOption import java.nio.file.StandardOpenOption /** @@ -60,6 +61,10 @@ class FileRepo { trashDir = trashPath } + protected static def uploadFile(Annex annex, Map params=[:]) { + uploadFile(annex, params, false) + } + /** * * @param annex Annex data to upload @@ -67,7 +72,7 @@ class FileRepo { * If not present data will should exists in annex object * @return changed annex object */ - protected static def uploadFile(Annex annex, Map params=[:]) { + protected static def uploadFile(Annex annex, Map params=[:], boolean copyFile) { if (!isInitialized()) { init() } @@ -83,8 +88,15 @@ class FileRepo { params.annexId = params.annexId ?: annex.id params.file = params.file ?: annex.file //params.version = params.version ?: 0 - annex.contentType = params.file.contentType - Long fileSize = saveFileToRepo(params) + if (!annex.contentType && params?.file?.contentType) { + annex.contentType = params.file.contentType + } + Long fileSize = 0 + if (copyFile) { + fileSize = copyFileToRepo(params) + } else { + fileSize = saveFileToRepo(params) + } annex.length = fileSize annex } @@ -159,6 +171,26 @@ class FileRepo { out.write(f.bytes, 0, f.bytes.length); } catch (IOException e) { log.error(e) + throw e + } + file.toFile().length() + } + + private static Long copyFileToRepo(Map params) { + String diskFileName = "${params.annexId}" + VERSION_SEPARTOR + "${params.version}" + Path filePath = Paths.get(repoDir.toString(), params.bucket) + if (Files.notExists(filePath)) { + filePath = Files.createDirectory(filePath) + } + Path file = Paths.get(filePath.toString(), diskFileName) + def f = params.file + file = Files.createFile(file) + + try { + Files.copy(f.toPath(), file, StandardCopyOption.REPLACE_EXISTING); + } catch (IOException e) { + log.error(e) + throw e } file.toFile().length() } diff --git a/src/main/groovy/blocks/traits/AnnexableControllerTrait.groovy b/src/main/groovy/blocks/traits/AnnexableControllerTrait.groovy index d99df6a..cafe613 100644 --- a/src/main/groovy/blocks/traits/AnnexableControllerTrait.groovy +++ b/src/main/groovy/blocks/traits/AnnexableControllerTrait.groovy @@ -20,7 +20,7 @@ import java.nio.file.Files */ @Enhances(ControllerArtefactHandler.TYPE) trait AnnexableControllerTrait { - AnnexableService annexableService; + AnnexableService annexableService private static final log = LogFactory.getLog(this) @Action