cancel
Showing results for 
Search instead for 
Did you mean: 

Howto Automatically Determine Mime Type

stevewickii
Champ in-the-making
Champ in-the-making
I am writing a method that uses the WebService API (CMLCreate, and CMLWriteContent) to add a file to the CMS.

Is there is a class I can use to automatically determine the mime-type for the file being added based upon file name extension or something like that?

Thanks!
3 REPLIES 3

rivetlogic
Champ on-the-rise
Champ on-the-rise
Hi,

There is a MimeTypeService that is available but not as a webservice.
Here is the code that shows one of the ways this service is being used.



org.alfresco.web.bean.repository.Repository

public static String getMimeTypeForFileName(FacesContext context, String filename)
   {
      // base the mimetype from the file extension
      MimetypeService mimetypeService = (MimetypeService)getServiceRegistry(context).getMimetypeService();
     
      // fall back to binary mimetype if no match found
      String mimetype = MimetypeMap.MIMETYPE_BINARY;
      int extIndex = filename.lastIndexOf('.');
      if (extIndex != -1)
      {
         String ext = filename.substring(extIndex + 1).toLowerCase();
         String mt = mimetypeService.getMimetypesByExtension().get(ext);
         if (mt != null)
         {
            mimetype = mt;
         }
      }
     
      return mimetype;
   }

You could leverage this functionality using a custom action, which you could call using webservice.

Take a look at the implementation of this service.

org.alfresco.repo.content.MimetypeMap  implements MimetypeService


Best Regards,
Shagul

stevewickii
Champ in-the-making
Champ in-the-making
Thanks.  Yeah, I saw the MimeTypeService, but I couldn't use it, because it's not available as a web service.

I ended up using the mime type specified in the HTTP file upload.
 request.getContentType()

Thanks again for the suggestion!

daek
Champ in-the-making
Champ in-the-making
Hi stevewickii,

I had the same problem, and I discover "a Java library for determining the MIME type of files or streams" …

:arrow: Jmimemagic

In juste one line, you can have the MIME Type of your byte[] (the_file) :

net.sf.jmimemagic.Magic.getMagicMatch(the_file).getMimeType()

Maybe this library can help you … 😎