1. James Murty
  2. jets3t

Commits

James Murty  committed 4f36b0f

The default mimetype value used when uploading files with a missing or unrecognized extension can now be configured by using the special '*' extension value in the mime.types config file. Fixes #131

  • Participants
  • Parent commits 16b9cd4
  • Branches default

Comments (0)

Files changed (3)

File RELEASE_NOTES.markdown

View file
    now ignore case in metadata item names. The case of item names is
    retained and available if necessary, but metadata add/get methods
    no longer pay attention to case.
+ * Specify a default mimetype to use for uploaded files with an
+   unrecognized or missing extension by defining a '*' extension
+   in the mime.types config file.
 
 GoogleStorageService:
 

File configs/mime.types

View file
 # registry is at <http://www.iana.org/assignments/media-types/>.
 
 # MIME type			Extensions
+
+# Default mimetype
+application/octet-stream    *
+
 application/activemessage
 application/andrew-inset	ez
 application/applefile

File src/org/jets3t/service/utils/Mimetypes.java

View file
      * Determines the mimetype of a file by looking up the file's extension in an internal listing
      * to find the corresponding mime type. If the file has no extension, or the extension is not
      * available in the listing contained in this class, the default mimetype
-     * <code>application/octet-stream</code> is returned.
+     * is returned (generally <code>application/octet-stream</code>).
      * <p>
      * A file extension is one or more characters that occur after the last period (.) in the file's name.
      * If a file has no extension,
      * the name of the file whose extension may match a known mimetype.
      *
      * @return
-     * the file's mimetype based on its extension, or a default value of
-     * <code>application/octet-stream</code> if a mime type value cannot be found.
+     * the file's mimetype based on its extension, or a default value if a mimetype mapping
+     * is not found.
      */
     public String getMimetype(String fileName) {
+        // Look up default mimetype, represented by '*' in the mime.types file or use
+        // application/octet-stream as a fallback.
+        String mimetype = extensionToMimetypeMap.get("*");
+        if (mimetype == null) {
+            mimetype = MIMETYPE_OCTET_STREAM;
+        }
+
         int lastPeriodIndex = fileName.lastIndexOf(".");
         if (lastPeriodIndex > 0 && lastPeriodIndex + 1 < fileName.length()) {
             String ext = fileName.substring(lastPeriodIndex + 1);
             if (extensionToMimetypeMap.keySet().contains(ext)) {
-                String mimetype = extensionToMimetypeMap.get(ext);
+                mimetype = extensionToMimetypeMap.get(ext);
                 if (log.isDebugEnabled()) {
                     log.debug("Recognised extension '" + ext + "', mimetype is: '" + mimetype + "'");
                 }
             } else {
                 if (log.isDebugEnabled()) {
                     log.debug("Extension '" + ext + "' is unrecognized in mime type listing"
-                    + ", using default mime type: '" + MIMETYPE_OCTET_STREAM + "'");
+                    + ", using default mime type: '" + mimetype + "'");
                 }
             }
         } else {
                 log.debug("File name has no extension, mime type cannot be recognised for: " + fileName);
             }
         }
-        return MIMETYPE_OCTET_STREAM;
+        return mimetype;
     }
 
     /**