From 2d925c5358186a0b1cf69465597ea7c7fecdb207 Mon Sep 17 00:00:00 2001 From: Jon Palmer Date: Sat, 17 Mar 2012 23:49:21 -0400 Subject: [PATCH] Add new setting keepFilename that retains the original filename and just appends the hashed id before the extension. --- .../HashAndCacheResourceMapper.groovy | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/grails-app/resourceMappers/org/grails/plugin/cachedresources/HashAndCacheResourceMapper.groovy b/grails-app/resourceMappers/org/grails/plugin/cachedresources/HashAndCacheResourceMapper.groovy index c576604..978f713 100644 --- a/grails-app/resourceMappers/org/grails/plugin/cachedresources/HashAndCacheResourceMapper.groovy +++ b/grails-app/resourceMappers/org/grails/plugin/cachedresources/HashAndCacheResourceMapper.groovy @@ -3,6 +3,7 @@ package org.grails.plugin.cachedresources import org.codehaus.groovy.grails.plugins.codecs.SHA256BytesCodec import org.grails.plugin.cachedresources.util.Base62 import org.grails.plugin.resource.mapper.MapperPhase +import org.apache.commons.io.FilenameUtils class HashAndCacheResourceMapper { @@ -20,7 +21,8 @@ class HashAndCacheResourceMapper { log.debug "Hashing resources to unique names..." } - resource.processedFile = renameToHashOfContents(resource.processedFile, resource.processedFileExtension) + def filename = FilenameUtils.getBaseName(resource.processedFile.name) + resource.processedFile = renameToHashOfContents(resource.processedFile, filename, resource.processedFileExtension) resource.updateActualUrlFromProcessedFile() // Do all the horrible cache header stuff @@ -59,18 +61,29 @@ class HashAndCacheResourceMapper { boolean getFlattenDirs() { resourceService.getConfigParamOrDefault('flatten', true) } + + boolean getKeepFilename() { + resourceService.getConfigParamOrDefault('keepFilename', false) + } /** * Renames the given input file in the same directory to be the SHA256 hash of it's contents. */ - def renameToHashOfContents(File input, String extension) { - def newName + def renameToHashOfContents(File input, String filename, String extension) { + def nameHash if (shortenLinks) { def hash = SHA256BytesCodec.encode(getBytes(input)) - newName = Base62.encode(hash) + nameHash = Base62.encode(hash) } else { - newName = SHA256Codec.encode(getBytes(input)) + nameHash = SHA256Codec.encode(getBytes(input)) } + def newName + if (keepFilename) { + newName = "${filename}_${nameHash}" + } else { + newName = nameHash + } + def parent = flattenDirs ? resourceService.workDir : input.parentFile def target = new File(parent, extension ? "${newName}.${extension}" : newName)