How to get node’s mimetype on a content update behavior on Alfresco

Officially, Alfresco provides access to the mimetype through node’s ContentData.

    public String getMimeType(NodeRef nodeRef) {
        ContentData contentData = (ContentData) nodeService.getProperty(nodeRef, ContentModel.PROP_CONTENT);
        return contentData != null ? contentData.getMimetype() : null;

However, when this is called on an content update behavior (using any of the content update policies)
– ContentServicePolicies.OnContentPropertyUpdatePolicy
– ContentServicePolicies.OnContentUpdatePolicy

it returns the old mimetype not the new one. For that reason, to make sure you capture the new and actual mimetype, you would need to calculate it by yourself. In the following case we take advantage of MimetypesFileTypeMap to calculate the mimetype based on the node’s name. This is a simple approach but you can leverage on other libraries to verify the actual content and guess the mimetype from it.

    public String getMimeTypeFromFileName(NodeRef nodeRef) {
        String nodeName = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);        
        return MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(nodeName);

This mimetype bug is present on ACS 5.2. I haven’t tested the scenario on newer versions.