Skip to content

Commit

Permalink
Added prefix to overwrite MIME type detection
Browse files Browse the repository at this point in the history
  • Loading branch information
aseeland committed Feb 15, 2022
1 parent fb24c87 commit e07ba47
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions src/main/java/edu/harvard/iq/dataverse/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit e07ba47

Please sign in to comment.