cancel
Showing results for 
Search instead for 
Did you mean: 

Howto Change Mimetype for Existing Content

stevewickii
Champ in-the-making
Champ in-the-making
How would I change the mime-type on a piece of existing content?

I have tried several methods and none seem to work.  The program executes with no problem, it just doesn't do anything to the content.

Method 1: Using the ContentWriter
TransactionService transactionService = serviceRegistry.getTransactionService();
RetryingTransactionCallback<Object> exampleWork = new RetryingTransactionCallback<Object>()
{
            public Object execute() throws Exception
            {
                  ContentWriter content = contentService.getWriter(nodeIWantToChange, ContentModel.PROP_CONTENT, true);
                content.setMimetype(MimetypeMap.MIMETYPE_HTML);
                return null;
            }
};
transactionService.getRetryingTransactionHelper().doInTransaction(exampleWork);

Method 2: Using the ContentReader
TransactionService transactionService = serviceRegistry.getTransactionService();
RetryingTransactionCallback<Object> exampleWork = new RetryingTransactionCallback<Object>()
{
            public Object execute() throws Exception
            {
                  ContentReader content = contentService.getReader(nodeIWantToChange, ContentModel.PROP_CONTENT);
                content.setMimetype(MimetypeMap.MIMETYPE_HTML);
                return null;
            }
};
transactionService.getRetryingTransactionHelper().doInTransaction(exampleWork);
3 REPLIES 3

tommorris
Champ in-the-making
Champ in-the-making
I'm not sure. That's how I would try it. It could be a defect.

Perhaps that only works if you're actually going to go ahead and write something using the channel you get from the ContentWriter…?
It would be a shame to stream the content out and then back in though.

Maybe try opening up random access channel and then force write-flush, with the 'metadata' param as true:

fileChannel = contentWriter.getFileChannel(false);
contentWriter.setMimetype("text/plain");
fileChannel.force(true);
fileChannel.close();

…?

lotharm
Champ on-the-rise
Champ on-the-rise
You can set the mimetype using the NodeService instead:


ContentData cd = (ContentData) nodeService.getProperty(nodeRef, ContentModel.PROP_CONTENT);
ContentData newCD = ContentData.setMimetype(cd, "other/mimetype");
nodeService.setProperty(nodeRef, ContentModel.PROP_CONTENT, newCD);

It seems, that the ContentReader or ContentWriter .setMimetype method is only usable for creating new content. Might be a bug or a feature 😎

Regards,
  lothar

tommorris
Champ in-the-making
Champ in-the-making
Of course! Thank you.