Skip to content

Commit

Permalink
mime filetype lookup refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ErykKul committed Apr 23, 2024
1 parent f071658 commit 95dd558
Showing 1 changed file with 33 additions and 32 deletions.
65 changes: 33 additions & 32 deletions src/main/java/edu/harvard/iq/dataverse/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -433,12 +433,10 @@ public static String retestIngestableFileType(File file, String fileType) {
}

public static String determineFileType(File f, String fileName) throws IOException{
final ResourceBundle bundle = BundleUtil.getResourceBundle("MimeTypeDetectionByFileName");
if (bundle.keySet().contains(fileName)) {
return bundle.getString(fileName);
String fileType = lookupFileTypeByFileName(fileName);
if (fileType != null) {
return fileType;
}

String fileType = null;
String fileExtension = getFileExtension(fileName);


Expand Down Expand Up @@ -497,17 +495,17 @@ public static String determineFileType(File f, String fileName) throws IOExcepti
if (fileType != null && fileType.startsWith("text/plain") && STATISTICAL_FILE_EXTENSION.containsKey(fileExtension)) {
fileType = STATISTICAL_FILE_EXTENSION.get(fileExtension);
} else {
fileType = determineFileTypeByNameAndExtension(fileName);
fileType = lookupFileTypeByExtension(fileName);
}

logger.fine("mime type recognized by extension: "+fileType);
}
} else {
logger.fine("fileExtension is null");
String fileTypeByName = lookupFileTypeFromPropertiesFile(fileName);
if(!StringUtil.isEmpty(fileTypeByName)) {
logger.fine(String.format("mime type: %s recognized by filename: %s", fileTypeByName, fileName));
fileType = fileTypeByName;
final String fileTypeByExtension = lookupFileTypeByExtensionFromPropertiesFile(fileName);
if(!StringUtil.isEmpty(fileTypeByExtension)) {
logger.fine(String.format("mime type: %s recognized by extension: %s", fileTypeByExtension, fileName));
fileType = fileTypeByExtension;
}
}

Expand Down Expand Up @@ -552,38 +550,41 @@ public static String determineFileType(File f, String fileName) throws IOExcepti
return fileType;
}

public static String determineFileTypeByNameAndExtension(String fileName) {
final ResourceBundle bundle = BundleUtil.getResourceBundle("MimeTypeDetectionByFileName");
if (bundle.keySet().contains(fileName)) {
return bundle.getString(fileName);
public static String determineFileTypeByNameAndExtension(final String fileName) {
final String fileType = lookupFileTypeByFileName(fileName);
if (fileType != null) {
return fileType;
}

String mimetypesFileTypeMapResult = MIME_TYPE_MAP.getContentType(fileName);
return lookupFileTypeByExtension(fileName);
}

private static String lookupFileTypeByExtension(final String fileName) {
final String mimetypesFileTypeMapResult = MIME_TYPE_MAP.getContentType(fileName);
logger.fine("MimetypesFileTypeMap type by extension, for " + fileName + ": " + mimetypesFileTypeMapResult);
if (mimetypesFileTypeMapResult != null) {
if ("application/octet-stream".equals(mimetypesFileTypeMapResult)) {
return lookupFileTypeFromPropertiesFile(fileName);
} else {
return mimetypesFileTypeMapResult;
}
} else {
if (mimetypesFileTypeMapResult == null) {
return null;
}
if ("application/octet-stream".equals(mimetypesFileTypeMapResult)) {
return lookupFileTypeByExtensionFromPropertiesFile(fileName);
}
return mimetypesFileTypeMapResult;
}

private static String lookupFileTypeByFileName(final String fileName) {
return lookupFileTypeFromPropertiesFile("MimeTypeDetectionByFileName", fileName);
}

public static String lookupFileTypeFromPropertiesFile(String fileName) {
String fileKey = FilenameUtils.getExtension(fileName);
String propertyFileName = "MimeTypeDetectionByFileExtension";
if(fileKey == null || fileKey.isEmpty()) {
fileKey = fileName;
propertyFileName = "MimeTypeDetectionByFileName";
private static String lookupFileTypeByExtensionFromPropertiesFile(final String fileName) {
final String fileKey = FilenameUtils.getExtension(fileName);
return lookupFileTypeFromPropertiesFile("MimeTypeDetectionByFileExtension", fileKey);
}

}
String propertyFileNameOnDisk = propertyFileName + ".properties";
private static String lookupFileTypeFromPropertiesFile(final String propertyFileName, final String fileKey) {
final String propertyFileNameOnDisk = propertyFileName + ".properties";
try {
logger.fine("checking " + propertyFileNameOnDisk + " for file key " + fileKey);
return BundleUtil.getStringFromPropertyFile(fileKey, propertyFileName);
} catch (MissingResourceException ex) {
} catch (final MissingResourceException ex) {
logger.info(fileKey + " is a filename/extension Dataverse doesn't know about. Consider adding it to the " + propertyFileNameOnDisk + " file.");
return null;
}
Expand Down

0 comments on commit 95dd558

Please sign in to comment.