Uploaded image for project: 'Hippo CMS'
  1. Hippo CMS
  2. CMS-7930

MimeType of Uploaded files are not resolved based on file extensions therefore mime-types.properties files have no effect.

    XMLWordPrintable

Details

    • Bug
    • Status: Closed
    • Normal
    • Resolution: Outdated
    • 2.24.07.1
    • 2.28.00
    • None

    Description

      While I was setting up a HTML5 video player, I notice that .mp4 files uploaded to the assets have the wrong mime type ("video/quicktime" insteaed of "video/mp4"). To fix it, I added a file "mime-types.properties" with the following content.

      mp4=video/mp4
      webm=video/webm
      ogv=video/ogg

      but it did not work. So I did a little bit of debugging to see whether it was being picked up by "eu.medsea.mimeutil.detector.ExtensionMimeDetector". It turned out that it was being picked up and the extensions were being added but it did not do the trick because when the MimeUtil is being called at "org.hippoecm.frontend.plugins.yui.upload.MagicMimeTypeFileItem.resolveMimeType (line 68)"

      65 private String resolveMimeType(FileItem fileItem) {
      66 Collection<?> mimeTypes = null;
      67 if(fileItem instanceof DiskFileItem && !fileItem.isInMemory())

      { 68 mimeTypes = MimeUtil.getMimeTypes(((DiskFileItem)fileItem).getStoreLocation()); 69 }

      else {

      the value of "(DiskFileItem)fileItem).getStoreLocation()" is a temp file with extension .tmp.
      So MimeUtil does try to find the MimeType based on the file extension but to no avail (since the file extension is always .tmp) and then resorts to guessing the MimeType based on the content of the file which is not always accurate.

      So I fixed this problem by replacing the following lines in the class "org.hippoecm.frontend.plugins.yui.upload.MagicMimeTypeFileItem"

      private String resolveMimeType(FileItem fileItem) {
      Collection<?> mimeTypes = null;
      if(fileItem instanceof DiskFileItem && !fileItem.isInMemory())

      { mimeTypes = MimeUtil.getMimeTypes(((DiskFileItem)fileItem).getStoreLocation()); }

      else {
      InputStream inputStream = null;
      try

      { inputStream = fileItem.getInputStream(); mimeTypes = MimeUtil.getMimeTypes(new BufferedInputStream(inputStream)); }

      catch (IOException e)

      { log.warn("IOException prevented retrieval of mimetype; using default", e); }

      finally {
      try {
      if (inputStream != null)

      { inputStream.close(); }

      } catch (IOException e)

      { log.warn("Could not close inputstream after retrieving mimetype", e); }

      }
      }
      ....
      with

      private String resolveMimeType(FileItem fileItem) {
      Collection<?> mimeTypes = null;
      if (StringUtils.isNotBlank(fileItem.getName()))

      { mimeTypes = MimeUtil.getMimeTypes(fileItem.getName()); }

      if (mimeTypes == null) {
      if(fileItem instanceof DiskFileItem && !fileItem.isInMemory())

      { mimeTypes = MimeUtil.getMimeTypes(((DiskFileItem)fileItem).getStoreLocation()); }

      else {
      InputStream inputStream = null;
      try

      { inputStream = fileItem.getInputStream(); mimeTypes = MimeUtil.getMimeTypes(new BufferedInputStream(inputStream)); }

      catch (IOException e)

      { log.warn("IOException prevented retrieval of mimetype; using default", e); }

      finally {
      try {
      if (inputStream != null)

      { inputStream.close(); }

      } catch (IOException e)

      { log.warn("Could not close inputstream after retrieving mimetype", e); }

      }
      }
      }

      So now it first tries to resolved the mimetype based on the extension of original file and if that is unsuccessful it resolves the type based on the content of the inputstream.

      Attachments

        1. MagicMimeTypeFileItem.java
          6 kB
          Ebrahim Aharpour
        2. MagicMimeTypeFileItem.java
          6 kB
          Ebrahim Aharpour
        3. MagicMimeTypeFileItem.java
          6 kB
          Ebrahim Aharpour

        Issue Links

          Activity

            People

              Unassigned Unassigned
              aharpour Ebrahim Aharpour (Inactive)
              Votes:
              1 Vote for this issue
              Watchers:
              7 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: