Fix mime types of .ico files
In diffs .ico files did not get shown, as the reported mime type was "application/octet-stream" instead of "image/x-icon". While mime-utils correctly reports both of the above mime types as possible mime types for .ico files, application/octet-stream are incorrectly reported with specificity 1 (instead of 0 or below), which caused problems when sorting the mime types to get the most specific one. We now override MimeType's computation of specificity for generic types and force low enough values for the sorting to work as expected. Thereby, .ico files get correctly reported as image/x-icon. Change-Id: I587a3fdbe29eda94a9a7dd0e1a353c0ea4a3958a
This commit is contained in:
@@ -60,6 +60,38 @@ public class MimeUtilFileTypeRegistry implements FileTypeRegistry {
|
||||
mimeUtil.registerMimeDetector(name);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get specificity of mime types with generic types forced to low values
|
||||
*
|
||||
* "application/octet-stream" is forced to -1.
|
||||
* "text/plain" is forced to 0.
|
||||
* All other mime types return the specificity reported by mimeType itself.
|
||||
*
|
||||
* @param mimeType The mimeType to get the corrected specificity for.
|
||||
* @return The corrected specificity.
|
||||
*/
|
||||
private int getCorrectedMimeSpecificity(MimeType mimeType) {
|
||||
// Although the documentation of MimeType's getSpecificity claims that for
|
||||
// example "application/octet-stream" always has a specificity of 0, it
|
||||
// effectively returns 1 for us. This causes problems when trying to get
|
||||
// the correct mime type via sorting. For example in
|
||||
// [application/octet-stream, image/x-icon] both mime types come with
|
||||
// specificity 1 for us. Hence, getMimeType below may end up using
|
||||
// application/octet-stream instead of the more specific image/x-icon.
|
||||
// Therefore, we have to force the specificity of generic types below the
|
||||
// default of 1.
|
||||
//
|
||||
final String mimeTypeStr = mimeType.toString();
|
||||
if (mimeTypeStr.equals("application/octet-stream")) {
|
||||
return -1;
|
||||
}
|
||||
if (mimeTypeStr.equals("text/plain")) {
|
||||
return 0;
|
||||
}
|
||||
return mimeType.getSpecificity();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public MimeType getMimeType(final String path, final byte[] content) {
|
||||
Set<MimeType> mimeTypes = new HashSet<MimeType>();
|
||||
@@ -84,7 +116,7 @@ public class MimeUtilFileTypeRegistry implements FileTypeRegistry {
|
||||
Collections.sort(types, new Comparator<MimeType>() {
|
||||
@Override
|
||||
public int compare(MimeType a, MimeType b) {
|
||||
return b.getSpecificity() - a.getSpecificity();
|
||||
return getCorrectedMimeSpecificity(b) - getCorrectedMimeSpecificity(a);
|
||||
}
|
||||
});
|
||||
return types.get(0);
|
||||
|
Reference in New Issue
Block a user