From d908acd417de2bc99b7f0819600db8676bc2c2e1 Mon Sep 17 00:00:00 2001 From: Tatyana Valkevych Date: Wed, 11 Nov 2015 11:50:23 +0000 Subject: [PATCH] Merged HEAD (5.1) to 5.1.N (5.1.1) 116418 mrogers: ACE-4523 - Binary File mimetype is set to files edited via CIFS/FTP git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.1.N/root@117194 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../filesys/repo/ContentDiskDriver2.java | 21 ++- .../filesys/repo/ContentDiskDriverTest.java | 143 ++++++++++++++++++ .../filesys/ContentDiskDriverTestTxt1.txt | 1 + 3 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 source/test-resources/filesys/ContentDiskDriverTestTxt1.txt diff --git a/source/java/org/alfresco/filesys/repo/ContentDiskDriver2.java b/source/java/org/alfresco/filesys/repo/ContentDiskDriver2.java index 592c6611cd..a78b7432b3 100644 --- a/source/java/org/alfresco/filesys/repo/ContentDiskDriver2.java +++ b/source/java/org/alfresco/filesys/repo/ContentDiskDriver2.java @@ -3035,9 +3035,28 @@ public class ContentDiskDriver2 extends AlfrescoDiskDriver implements ExtendedD nodeService.setProperty(target, ContentModel.PROP_MODIFIED, new Date()); } - // Take an initial guess at the mimetype (if it has not been set by something already) + /** + * Take a guess at the mimetype + */ String mimetype = mimetypeService.guessMimetype(tempFile.getFullName(), new FileContentReader(tempFile.getFile())); logger.debug("guesssed mimetype:" + mimetype); + + /** + * mime type guessing may have failed in which case we should assume the mimetype has not changed. + */ + if(mimetype.equalsIgnoreCase(MimetypeMap.MIMETYPE_BINARY)) + { + // mimetype guessing may have failed + if(existingContent != null) + { + // copy the mimetype from the existing content. + mimetype = existingContent.getMimetype(); + if(logger.isDebugEnabled()) + { + logger.debug("using mimetype of existing content :" + mimetype); + } + } + } String encoding; // Take a guess at the locale diff --git a/source/test-java/org/alfresco/filesys/repo/ContentDiskDriverTest.java b/source/test-java/org/alfresco/filesys/repo/ContentDiskDriverTest.java index 3a41c2f45e..99a456f5fa 100644 --- a/source/test-java/org/alfresco/filesys/repo/ContentDiskDriverTest.java +++ b/source/test-java/org/alfresco/filesys/repo/ContentDiskDriverTest.java @@ -7939,6 +7939,149 @@ public class ContentDiskDriverTest extends TestCase logger.debug("end testScenarioMountainLionWord2011 Edit By Editor ALF-16257"); } // testScenarioMountainLionWord2011EditByEditor_ALF_16257 + + /** + * Guess mimetype with insufficient data for ACE-4523 + * Simulate creating a plain text document via Alfresco Share + * then updating it via CIFS/FTP + * + * 1. create a document called "foo" with just a little data with an explicit mimetype set. + * 2. update the document with different text + * 3. check the mimetype of the test doc has not changed + */ + public void testMimetypeWithInsufficiantData() throws Exception + { + logger.debug("testMimetypeWithInsufficiantData"); + + // a file without a clue about mimetype + final String FILE_NAME = "foo"; + + class TestContext + { + NetworkFile firstFileHandle; + String mimetype; + }; + final TestContext testContext = new TestContext(); + + final String TEST_DIR = TEST_ROOT_DOS_PATH + "\\testMimetypeWithInsufficiantData"; + + // this is a made up mimetype - so there is no way that it could be guessed. + final String TEST_MIMETYPE = "text\bar"; + + ServerConfiguration scfg = new ServerConfiguration("testServer"); + TestServer testServer = new TestServer("testServer", scfg); + final SrvSession testSession = new TestSrvSession(666, testServer, "test", "remoteName"); + DiskSharedDevice share = getDiskSharedDevice(); + final TreeConnection testConnection = testServer.getTreeConnection(share); + final RetryingTransactionHelper tran = transactionService.getRetryingTransactionHelper(); + + /** + * Clean up just in case garbage is left from a previous run + */ + RetryingTransactionCallback deleteGarbageFileCB = new RetryingTransactionCallback() + { + @Override + public Void execute() throws Throwable + { + driver.deleteFile(testSession, testConnection, TEST_DIR + "\\" + FILE_NAME); + return null; + } + }; + try + { + tran.doInTransaction(deleteGarbageFileCB); + } + catch (Exception e) + { + // expect to go here + } + + logger.debug("a) create new file"); + RetryingTransactionCallback createFileCB = new RetryingTransactionCallback() { + + @Override + public Void execute() throws Throwable + { + /** + * Create the test directory we are going to use + */ + FileOpenParams createRootDirParams = new FileOpenParams(TEST_ROOT_DOS_PATH, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0); + FileOpenParams createDirParams = new FileOpenParams(TEST_DIR, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0); + driver.createDirectory(testSession, testConnection, createRootDirParams); + driver.createDirectory(testSession, testConnection, createDirParams); + + /** + * Create the file we are going to test + */ + FileOpenParams createFileParams = new FileOpenParams(TEST_DIR + "\\" + FILE_NAME, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0); + testContext.firstFileHandle = driver.createFile(testSession, testConnection, createFileParams); + assertNotNull(testContext.firstFileHandle); + driver.closeFile(testSession, testConnection, testContext.firstFileHandle); + + + ClassPathResource fileResource = new ClassPathResource("filesys/ContentDiskDriverTestTxt1.txt"); + // Add the test content via the content writer to simulate being created via Share. + NodeRef file1NodeRef = getNodeForPath(testConnection, TEST_DIR + "\\" + FILE_NAME); + ContentWriter contentWriter2 = contentService.getWriter(file1NodeRef, ContentModel.PROP_CONTENT, true); + // this is a made up mimetype - so there is no way that it could be guessed. + contentWriter2.setMimetype(TEST_MIMETYPE); + contentWriter2.putContent(fileResource.getFile()); + + return null; + } + }; + tran.doInTransaction(createFileCB, false, true); + + /** + * b) Update the file via CIFS + */ + logger.debug("b) update file via CIFS"); + RetryingTransactionCallback updateFileCB = new RetryingTransactionCallback() + { + @Override + public Void execute() throws Exception + { + FileOpenParams createFileParams = new FileOpenParams(TEST_DIR + "\\" + FILE_NAME, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0); + NetworkFile file = driver.openFile(testSession, testConnection, createFileParams); + assertNotNull(file); + String testContent = "Bar"; + byte[] testContentBytes = testContent.getBytes(); + file.writeFile(testContentBytes, testContentBytes.length, 0, 0); + driver.closeFile(testSession, testConnection, file); + return null; + } + }; + tran.doInTransaction(updateFileCB, false, true); + + logger.debug("c) validate results"); + + /** + * Now validate everything is correct + */ + RetryingTransactionCallback validateCB = new RetryingTransactionCallback() { + @Override + public Void execute() throws Throwable + { + NodeRef shuffledNodeRef = getNodeForPath(testConnection, TEST_DIR + "\\" + FILE_NAME); + Map props = nodeService.getProperties(shuffledNodeRef); + ContentData data = (ContentData)props.get(ContentModel.PROP_CONTENT); + + + /** + * Validate mimetype has not changed + */ + assertEquals("mimeType is wrong", TEST_MIMETYPE, data.getMimetype()); + assertEquals("mimeType is wrong", 3, data.getSize()); + + return null; + } + }; + tran.doInTransaction(validateCB, true, true); + logger.debug("end testMimetypeWithInsufficiantData"); + } // testMimetypeWithInsufficiantData" + + + /** * Test server */ diff --git a/source/test-resources/filesys/ContentDiskDriverTestTxt1.txt b/source/test-resources/filesys/ContentDiskDriverTestTxt1.txt new file mode 100644 index 0000000000..772ac09d09 --- /dev/null +++ b/source/test-resources/filesys/ContentDiskDriverTestTxt1.txt @@ -0,0 +1 @@ +txt A \ No newline at end of file