Skip to content

Commit

Permalink
New methods for uploading local files
Browse files Browse the repository at this point in the history
  • Loading branch information
fgroch committed Aug 7, 2018
1 parent 9d222ce commit 90222d3
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 6 deletions.
2 changes: 1 addition & 1 deletion grails-app/controllers/blocks/AnnexableController.groovy
Expand Up @@ -8,7 +8,7 @@ import java.nio.file.Files

class AnnexableController {

def annexableService
AnnexableService annexableService

def find() {
String namePart = params.remove('namePart')
Expand Down
@@ -1,5 +1,6 @@
package blocks.test

import blocks.AnnexableService
import grails.converters.JSON
import blocks.Annex

Expand All @@ -8,7 +9,7 @@ import java.nio.file.Files

class RepositoryController {

def annexableService
AnnexableService annexableService

def index() {
def buckets = annexableService.annexesGroupByBucket
Expand Down
7 changes: 7 additions & 0 deletions grails-app/services/blocks/AnnexableService.groovy
Expand Up @@ -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()
Expand Down
Empty file modified grailsw 100755 → 100644
Empty file.
38 changes: 35 additions & 3 deletions src/main/groovy/blocks/FileRepo.groovy
Expand Up @@ -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

/**
Expand Down Expand Up @@ -60,14 +61,18 @@ class FileRepo {
trashDir = trashPath
}

protected static def uploadFile(Annex annex, Map params=[:]) {
uploadFile(annex, params, false)
}

/**
*
* @param annex Annex data to upload
* @param params map of params like version, annex, bucket or file.
* 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()
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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()
}
Expand Down
Expand Up @@ -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
Expand Down

0 comments on commit 90222d3

Please sign in to comment.