From e07ba4780c3eff6e2f2083f3896b28b5b38027cd Mon Sep 17 00:00:00 2001 From: seeland <anett.seeland@tik.uni-stuttgart.de> Date: Tue, 15 Feb 2022 08:44:38 +0100 Subject: [PATCH] Added prefix to overwrite MIME type detection --- .../harvard/iq/dataverse/util/FileUtil.java | 44 ++++++++++++------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/src/main/java/edu/harvard/iq/dataverse/util/FileUtil.java b/src/main/java/edu/harvard/iq/dataverse/util/FileUtil.java index a132794664c..d41a529cae3 100644 --- a/src/main/java/edu/harvard/iq/dataverse/util/FileUtil.java +++ b/src/main/java/edu/harvard/iq/dataverse/util/FileUtil.java @@ -208,6 +208,8 @@ public class FileUtil implements java.io.Serializable { private static final String FILE_FACET_CLASS_TEXT = "Text"; private static final String FILE_FACET_CLASS_OTHER = "Other"; private static final String FILE_FACET_CLASS_UNKNOWN = "Unknown"; + + public static final String OVERWRITE_MIME_TYPE_PREFIX = "force-"; // The file type facets and type-specific thumbnail classes (above) are // very similar, but not exactly 1:1; so the following map is for @@ -802,24 +804,32 @@ public static List<DataFile> createDataFiles(DatasetVersion version, InputStream // than the type supplied: // -- L.A. String recognizedType = null; - - try { - recognizedType = determineFileType(tempFile.toFile(), fileName); - logger.fine("File utility recognized the file as " + recognizedType); - if (recognizedType != null && !recognizedType.equals("")) { - if (useRecognizedType(suppliedContentType, recognizedType)) { - finalType = recognizedType; - } - } - - } catch (Exception ex) { - logger.warning("Failed to run the file utility mime type check on file " + fileName); + + // If we detect the dataverse prefix in the supplied mimetype we do no + // further recognition + if (suppliedContentType.toLowerCase().startsWith(OVERWRITE_MIME_TYPE_PREFIX)) { + finalType = suppliedContentType.toLowerCase().substring(OVERWRITE_MIME_TYPE_PREFIX.length()); + logger.fine("Overwrite prefix detected. Using supplied mime type."); } - - if (finalType == null) { - finalType = (suppliedContentType == null || suppliedContentType.equals("")) - ? MIME_TYPE_UNDETERMINED_DEFAULT - : suppliedContentType; + else { + try { + recognizedType = determineFileType(tempFile.toFile(), fileName); + logger.fine("File utility recognized the file as " + recognizedType); + if (recognizedType != null && !recognizedType.equals("")) { + if (useRecognizedType(suppliedContentType, recognizedType)) { + finalType = recognizedType; + } + } + + } catch (Exception ex) { + logger.warning("Failed to run the file utility mime type check on file " + fileName); + } + + if (finalType == null) { + finalType = (suppliedContentType == null || suppliedContentType.equals("")) + ? MIME_TYPE_UNDETERMINED_DEFAULT + : suppliedContentType; + } } // A few special cases: