cancel
Showing results for 
Search instead for 
Did you mean: 

Question about mimetype and file extension

alestudent
Champ in-the-making
Champ in-the-making
Hi,
i'm new to Alfresco so i have a problem. I need to obtain file extension, i.e for a Microsoft Word file named "Prova.doc"; this file is in company_home folder. I wrote this lines of code:

STORE1= new Store (WORKSPACE,SPACESSTORE);
Reference r1 = new Reference (STORE1, null, "/app:company_home/*[@cm:name='Prova.doc']");
//From activation.jar library
String mt= new MimetypesFileTypeMap().getContentType(r1.getPath());
System.out.println(mt);

But when i print the String mt, the returned mimetype is "application/octet-stream" and looking to mimetype-map.xml i see that it isn't the correct mimetype of a Microsoft Word file (it would be "application/msword");

So i can't understand how to obtain the correct extension of a file, maybe my code lines are wrong.
Can someone help me please?
3 REPLIES 3

jbarmash
Champ in-the-making
Champ in-the-making
You probably already considered this, but could you just chop off the extension of the file using string manipulation ?

stijndereede
Champ in-the-making
Champ in-the-making
If you really just want the file extension, I think string manipulation on the name of the node is easiest.
What I've noticed is that when I upload a file with a webscript, the mimetype doesn't get set properly, but stays on the default application/octet-stream.
I wrote an action that extract the mimetype from the node, and saves it in the properties. This is the Java code:

   public void executeImpl(Action ruleAction, NodeRef actionedUponNodeRef) {
      if (this.nodeService.exists(actionedUponNodeRef) == true) {
         String name = (String) nodeService.getProperty(actionedUponNodeRef, ContentModel.PROP_NAME);
         String mime = mimetypeService.guessMimetype(name);
         logger.debug("Guessed mimetype to be: "+mime);
         // guess the mimetype
         ContentData oldContent = (ContentData) nodeService.getProperty(actionedUponNodeRef,
               ContentModel.PROP_CONTENT);
         ContentData newContent = ContentData.setMimetype(oldContent, mime);
         // guess the encoding
         ContentReader reader = contentService.getReader(actionedUponNodeRef, ContentModel.PROP_CONTENT);
         InputStream is = reader.getContentInputStream();
         Charset charset = contentCharsetFinder.getCharset(is, mime);
         logger.debug("Guessed charset to be: "+charset.name());
         newContent = ContentData.setEncoding(newContent, charset.name());
         nodeService.setProperty(actionedUponNodeRef, ContentModel.PROP_CONTENT, newContent);
         logger.debug("Setting new mimetype and charset");
      }
   }

I hope this helps you a bit.

alestudent
Champ in-the-making
Champ in-the-making
HI,
THANK YOU FOR ANSWERS;
MAKING SOME TESTS I USED STRING MANIPULATION ON STRING CONTENT. IN DETAILS I MADE A QUERY ON COMPANY_HOME FOLDER SO AS TO OBTAIN A LIST OF CONTENTRESULT OBJECTS. EACH OF THEM HAS A STRING CONTENT IN WHICH THERE ARE SOME INFORMATIONS (CORRECT MIMETYPE ALSO); IN THIS WAY I COULD UNDERSTAND THE CORRECT FILE FORMAT AND EXTENSION.

I CREATED A SPECIFIC CLASS TO MANIPULATE THE STRING AND WITH THE CODE POSTED BY stijndereede (THANK YOU), NOW  I CAN SET THE PROPERTIES CORRECTLY.

I HOPE THAT WHAT I DID IS USEFUL.