From: Richard Millet Date: Wed, 13 Feb 2013 08:31:16 +0000 (-0800) Subject: CSPACE-5497: Will now create a temp file if we find a file with a name that Nuxeo... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=ef9c2ad8fe4b9e814574e1d1cd211f1f49656911;p=tmp%2Fjakarta-migration.git CSPACE-5497: Will now create a temp file if we find a file with a name that Nuxeo does not like. We preserve the original file name in the BlobsCommon instance. --- diff --git a/services/blob/client/src/test/resources/blobs/silly==1,d.jpg b/services/blob/client/src/test/resources/blobs/silly==1,d.jpg new file mode 100644 index 000000000..15b559c91 Binary files /dev/null and b/services/blob/client/src/test/resources/blobs/silly==1,d.jpg differ diff --git a/services/common/src/main/java/org/collectionspace/services/common/FileUtils.java b/services/common/src/main/java/org/collectionspace/services/common/FileUtils.java index 9e45b8386..7727fbc69 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/FileUtils.java +++ b/services/common/src/main/java/org/collectionspace/services/common/FileUtils.java @@ -50,6 +50,7 @@ import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.disk.DiskFileItemFactory; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -66,11 +67,30 @@ public class FileUtils { public static final String DEFAULT_BLOB_NAME = "blob"; private static final String FILE_FORM_FIELD = "file"; + public static String getExtension(File file) { + return getExtension(file.getName()); + } + + // Call this method to get the extension by passing the file name + public static String getExtension(String fileName) { + if (fileName == null) { + return null; + } + + int index = fileName.lastIndexOf("."); + if (index == -1) { + return ""; + } + + return fileName.substring(index + 1); + } + /* * Creates a copy of the srcFile to a temp file */ static public File createTmpFile(File srcFile, String prefix) throws Exception { - File result = createTmpFile(new FileInputStream(srcFile), prefix); + String fileExtension = FileUtils.getExtension(srcFile); + File result = createTmpFile(new FileInputStream(srcFile), prefix, fileExtension); return result; } @@ -83,11 +103,25 @@ public class FileUtils { */ static public File createTmpFile(InputStream streamIn, String filePrefix) { + return createTmpFile(streamIn, filePrefix, null); + } + + /** + * Creates the tmp file. + * + * @param streamIn the stream in + * @param filePrefix the file prefix + * @param fileExtension the file extension + * @return the file + */ + static public File createTmpFile(InputStream streamIn, + String filePrefix, String fileExtension) { File result = null; filePrefix = filePrefix != null ? filePrefix : ""; + fileExtension = fileExtension != null ? "." + fileExtension : ""; String tmpDir = System.getProperty("java.io.tmpdir"); - result = new File(tmpDir, filePrefix + UUID.randomUUID().toString()); + result = new File(tmpDir, filePrefix + UUID.randomUUID().toString() + fileExtension); if (logger.isDebugEnabled() == true) { logger.debug("Creating temp file at:" + result.getAbsolutePath()); } diff --git a/services/common/src/main/java/org/collectionspace/services/common/imaging/nuxeo/NuxeoBlobUtils.java b/services/common/src/main/java/org/collectionspace/services/common/imaging/nuxeo/NuxeoBlobUtils.java index 613ff631a..c357ffe26 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/imaging/nuxeo/NuxeoBlobUtils.java +++ b/services/common/src/main/java/org/collectionspace/services/common/imaging/nuxeo/NuxeoBlobUtils.java @@ -1026,7 +1026,14 @@ public class NuxeoBlobUtils { logger.error("Could not create image blob.", e); throw e; } finally { - + // + // If we created a temp file then we should delete it. + // + if (targetFile.equals(originalFile) == false) { + if (targetFile.delete() == false) { + logger.warn(String.format("Attempt to delete temporary file '%s' failed.", targetFile.getAbsolutePath())); + } + } } return result;