diff --git a/src/main/java/org/alfresco/repo/jscript/ScriptNode.java b/src/main/java/org/alfresco/repo/jscript/ScriptNode.java index 4691511f32..adfa5a53ce 100644 --- a/src/main/java/org/alfresco/repo/jscript/ScriptNode.java +++ b/src/main/java/org/alfresco/repo/jscript/ScriptNode.java @@ -3950,6 +3950,20 @@ public class ScriptNode implements Scopeable, NamespacePrefixResolverProvider // update cached variables after putContent() updateContentData(true); } + + /** + * Set the content stream from another content object. + * + * @param content ScriptContent to set + * @param applyMimetype If true, apply the mimetype from the Content object, else leave the original mimetype + * @param guessEncoding If true, guess the encoding from the underlying input stream, else use encoding set in + * the Content object as supplied. + */ + @Deprecated + public void write(Content content, boolean applyMimetype, boolean guessEncoding) + { + write(content, applyMimetype, guessEncoding, null); + } /** * Set the content stream from another content object. @@ -3958,15 +3972,23 @@ public class ScriptNode implements Scopeable, NamespacePrefixResolverProvider * @param applyMimetype If true, apply the mimetype from the Content object, else leave the original mimetype * @param guessEncoding If true, guess the encoding from the underlying input stream, else use encoding set in * the Content object as supplied. + * @param fileName The filename for the attachment. */ - public void write(Content content, boolean applyMimetype, boolean guessEncoding) + public void write(Content content, boolean applyMimetype, boolean guessEncoding, String fileName) { ContentService contentService = services.getContentService(); ContentWriter writer = contentService.getWriter(nodeRef, this.property, true); InputStream is = null; if (applyMimetype) { - writer.setMimetype(content.getMimetype().toLowerCase()); + if (fileName != null && !fileName.isEmpty()) + { + writer.setMimetype(services.getMimetypeService().guessMimetype(fileName)); + } + else + { + writer.setMimetype(content.getMimetype().toLowerCase()); + } } if (guessEncoding) { diff --git a/src/test/java/org/alfresco/repo/jscript/ScriptNodeTest.java b/src/test/java/org/alfresco/repo/jscript/ScriptNodeTest.java index e90b30a1b3..083e42a2a0 100644 --- a/src/test/java/org/alfresco/repo/jscript/ScriptNodeTest.java +++ b/src/test/java/org/alfresco/repo/jscript/ScriptNodeTest.java @@ -95,6 +95,7 @@ import org.junit.rules.RuleChain; import org.junit.rules.TestName; import org.mozilla.javascript.Context; import org.mozilla.javascript.ScriptableObject; +import org.springframework.extensions.surf.util.InputStreamContent; /** @@ -848,4 +849,44 @@ public class ScriptNodeTest fail("Converting multiple property for Activiti script fails with " + e); } } + + /** + * https://issues.alfresco.com/jira/browse/MNT-19682 + * Test that mimetype is correctly set according to the content + */ + @Test + public void testWriteContentWithMimetypeAndWithoutFilename() + { + createTestContent(true); + ScriptNode scriptNode = new ScriptNode(testNode, SERVICE_REGISTRY); + scriptNode.setScope(getScope()); + + ScriptContentData scd = scriptNode.new ScriptContentData(null, ContentModel.PROP_CONTENT); + + InputStream inputStream = getClass().getClassLoader().getResourceAsStream(TEST_CONTENT_MODEL); + InputStreamContent inputStreamContent = new InputStreamContent(inputStream, MimetypeMap.MIMETYPE_APPLICATION_PS, "UTF-8"); + + scd.write(inputStreamContent, true, false); + assertEquals(MimetypeMap.MIMETYPE_APPLICATION_PS, scriptNode.getMimetype()); + } + + /** + * https://issues.alfresco.com/jira/browse/MNT-19682 + * Test that mimetype is correctly set according to the filename + */ + @Test + public void testWriteContentWithMimetypeAndFilename() + { + createTestContent(true); + ScriptNode scriptNode = new ScriptNode(testNode, SERVICE_REGISTRY); + scriptNode.setScope(getScope()); + + ScriptContentData scd = scriptNode.new ScriptContentData(null, ContentModel.PROP_CONTENT); + + InputStream inputStream = getClass().getClassLoader().getResourceAsStream(TEST_CONTENT_MODEL); + InputStreamContent inputStreamContent = new InputStreamContent(inputStream, MimetypeMap.MIMETYPE_APPLICATION_PS, "UTF-8"); + + scd.write(inputStreamContent, true, false, "test.ai"); + assertEquals(MimetypeMap.MIMETYPE_APPLICATION_ILLUSTRATOR, scriptNode.getMimetype()); + } }