mirror of
https://github.com/Alfresco/alfresco-transform-core.git
synced 2025-06-30 18:14:51 +00:00
MNT-24883 base engine changes to maintain source file name
This commit is contained in:
parent
e7ed64d055
commit
4088f51b67
@ -120,7 +120,6 @@ public class FileManager
|
|||||||
{
|
{
|
||||||
throw new TransformException(INSUFFICIENT_STORAGE, "Failed to store the source file", e);
|
throw new TransformException(INSUFFICIENT_STORAGE, "Failed to store the source file", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static File createTargetFile(HttpServletRequest request, String sourceMimetype, String targetMimetype)
|
public static File createTargetFile(HttpServletRequest request, String sourceMimetype, String targetMimetype)
|
||||||
@ -263,21 +262,12 @@ public class FileManager
|
|||||||
|
|
||||||
public static File createTempDirForDocFile(String sourceFileName)
|
public static File createTempDirForDocFile(String sourceFileName)
|
||||||
{
|
{
|
||||||
final File alfrescoTempDirectory = getTempDir();
|
File tempDir = new File(getTempDir(), UUID.randomUUID().toString());
|
||||||
try
|
if (!tempDir.mkdirs() && !tempDir.exists())
|
||||||
{
|
{
|
||||||
UUID uuid = UUID.randomUUID();
|
throw new TransformException(INSUFFICIENT_STORAGE, "Failed to create temp directory: " + tempDir);
|
||||||
final File tempDir = new File(alfrescoTempDirectory, uuid.toString());
|
|
||||||
if (!tempDir.exists() && !tempDir.mkdirs() && !tempDir.exists())
|
|
||||||
{
|
|
||||||
throw new RuntimeException("Failed to create temp directory: " + tempDir);
|
|
||||||
}
|
|
||||||
return new File(tempDir, sourceFileName);
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
throw new RuntimeException("Failed to created temp file: \n file: " + sourceFileName + "\n", e);
|
|
||||||
}
|
}
|
||||||
|
return new File(tempDir, sourceFileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static File getTempDir()
|
private static File getTempDir()
|
||||||
|
@ -30,6 +30,7 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
import java.util.Locale;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
@ -236,12 +237,14 @@ public class TransformManagerImpl implements TransformManager
|
|||||||
public void deleteDocUUIDFolder()
|
public void deleteDocUUIDFolder()
|
||||||
{
|
{
|
||||||
if (sourceFile == null)
|
if (sourceFile == null)
|
||||||
|
{
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
if (Util.isDocFile(sourceFile.getPath()))
|
if (Util.isDocFile(sourceFile.getPath()))
|
||||||
{
|
{
|
||||||
File parentDir = sourceFile.getParentFile();
|
File parentDir = sourceFile.getParentFile();
|
||||||
if (parentDir != null
|
if (parentDir != null
|
||||||
&& !StringUtils.equalsAny(parentDir.getName().toLowerCase(), "alfresco", "temp", "tmp")
|
&& !StringUtils.equalsAny(parentDir.getName().toLowerCase(Locale.ROOT), "alfresco", "temp", "tmp")
|
||||||
&& !parentDir.delete())
|
&& !parentDir.delete())
|
||||||
{
|
{
|
||||||
logger.error("Failed to delete parent directory {}", parentDir.getPath());
|
logger.error("Failed to delete parent directory {}", parentDir.getPath());
|
||||||
|
@ -81,12 +81,6 @@ public class StreamHandlerTest
|
|||||||
return new BufferedInputStream(new FileInputStream(sourceFile));
|
return new BufferedInputStream(new FileInputStream(sourceFile));
|
||||||
}
|
}
|
||||||
|
|
||||||
private File tempDocFile() throws IOException
|
|
||||||
{
|
|
||||||
return File.createTempFile("temp_", ".docx", tempDir);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private File tempFile() throws IOException
|
private File tempFile() throws IOException
|
||||||
{
|
{
|
||||||
return File.createTempFile("temp_", null, tempDir);
|
return File.createTempFile("temp_", null, tempDir);
|
||||||
@ -188,67 +182,6 @@ public class StreamHandlerTest
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testStartWithInputStreamAndCallCreateSourceFileForDocxFiles() throws Exception
|
|
||||||
{
|
|
||||||
try (InputStream inputStream = getSourceInputStreamFromBytes();
|
|
||||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream())
|
|
||||||
{
|
|
||||||
transformManager.setSourceFileName("test.docx");
|
|
||||||
transformManager.setInputStream(inputStream);
|
|
||||||
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
|
||||||
|
|
||||||
File sourceFileCreatedByTransform = transformManager.createSourceFile();
|
|
||||||
assertTrue(sourceFileCreatedByTransform.exists());
|
|
||||||
write(outputStreamLengthRecorder, read(sourceFileCreatedByTransform) + CHANGE);
|
|
||||||
|
|
||||||
transformManager.copyTargetFileToOutputStream();
|
|
||||||
transformManager.getOutputStream().close();
|
|
||||||
closeInputStreamWithoutException(inputStream);
|
|
||||||
Long outputLength = transformManager.getOutputLength();
|
|
||||||
transformManager.deleteSourceFile();
|
|
||||||
transformManager.deleteTargetFile();
|
|
||||||
|
|
||||||
assertEquals(EXPECTED, read(outputStream));
|
|
||||||
assertEquals(EXPECTED.length(), outputLength);
|
|
||||||
assertFalse(sourceFileCreatedByTransform.exists());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testStartWithInputStreamAndCallCreateSourceFileForDocxFilesWithHttpRequest() throws Exception
|
|
||||||
{
|
|
||||||
try (InputStream inputStream = getSourceInputStreamFromBytes();
|
|
||||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream())
|
|
||||||
{
|
|
||||||
HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class);
|
|
||||||
Part mockPart = Mockito.mock(Part.class);
|
|
||||||
Mockito.when(mockPart.getSubmittedFileName()).thenReturn("dummy.docx");
|
|
||||||
Collection<Part> parts = Arrays.asList(mockPart);
|
|
||||||
Mockito.when(mockRequest.getParts()).thenReturn(parts);
|
|
||||||
|
|
||||||
transformManager.setSourceMimetype("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
|
|
||||||
transformManager.setInputStream(inputStream);
|
|
||||||
transformManager.setRequest(mockRequest);
|
|
||||||
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
|
||||||
|
|
||||||
File sourceFileCreatedByTransform = transformManager.createSourceFile();
|
|
||||||
assertTrue(sourceFileCreatedByTransform.exists());
|
|
||||||
write(outputStreamLengthRecorder, read(sourceFileCreatedByTransform) + CHANGE);
|
|
||||||
|
|
||||||
transformManager.copyTargetFileToOutputStream();
|
|
||||||
transformManager.getOutputStream().close();
|
|
||||||
closeInputStreamWithoutException(inputStream);
|
|
||||||
Long outputLength = transformManager.getOutputLength();
|
|
||||||
transformManager.deleteSourceFile();
|
|
||||||
transformManager.deleteTargetFile();
|
|
||||||
|
|
||||||
assertEquals(EXPECTED, read(outputStream));
|
|
||||||
assertEquals(EXPECTED.length(), outputLength);
|
|
||||||
assertFalse(sourceFileCreatedByTransform.exists());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testStartWithSourceFile() throws Exception
|
public void testStartWithSourceFile() throws Exception
|
||||||
{
|
{
|
||||||
@ -643,4 +576,65 @@ public class StreamHandlerTest
|
|||||||
}.handleTransformRequest();
|
}.handleTransformRequest();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStartWithInputStreamAndCallCreateSourceFileForDocxFiles() throws Exception
|
||||||
|
{
|
||||||
|
try (InputStream inputStream = getSourceInputStreamFromBytes();
|
||||||
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream())
|
||||||
|
{
|
||||||
|
transformManager.setSourceFileName("test.docx");
|
||||||
|
transformManager.setInputStream(inputStream);
|
||||||
|
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
||||||
|
|
||||||
|
File sourceFileCreatedByTransform = transformManager.createSourceFile();
|
||||||
|
assertTrue(sourceFileCreatedByTransform.exists());
|
||||||
|
write(outputStreamLengthRecorder, read(sourceFileCreatedByTransform) + CHANGE);
|
||||||
|
|
||||||
|
transformManager.copyTargetFileToOutputStream();
|
||||||
|
transformManager.getOutputStream().close();
|
||||||
|
closeInputStreamWithoutException(inputStream);
|
||||||
|
Long outputLength = transformManager.getOutputLength();
|
||||||
|
transformManager.deleteSourceFile();
|
||||||
|
transformManager.deleteTargetFile();
|
||||||
|
|
||||||
|
assertEquals(EXPECTED, read(outputStream));
|
||||||
|
assertEquals(EXPECTED.length(), outputLength);
|
||||||
|
assertFalse(sourceFileCreatedByTransform.exists());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStartWithInputStreamAndCallCreateSourceFileForDocxFilesWithHttpRequest() throws Exception
|
||||||
|
{
|
||||||
|
try (InputStream inputStream = getSourceInputStreamFromBytes();
|
||||||
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream())
|
||||||
|
{
|
||||||
|
HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class);
|
||||||
|
Part mockPart = Mockito.mock(Part.class);
|
||||||
|
Mockito.when(mockPart.getSubmittedFileName()).thenReturn("dummy.docx");
|
||||||
|
Collection<Part> parts = Arrays.asList(mockPart);
|
||||||
|
Mockito.when(mockRequest.getParts()).thenReturn(parts);
|
||||||
|
|
||||||
|
transformManager.setSourceMimetype("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
|
||||||
|
transformManager.setInputStream(inputStream);
|
||||||
|
transformManager.setRequest(mockRequest);
|
||||||
|
OutputStream outputStreamLengthRecorder = transformManager.setOutputStream(outputStream);
|
||||||
|
|
||||||
|
File sourceFileCreatedByTransform = transformManager.createSourceFile();
|
||||||
|
assertTrue(sourceFileCreatedByTransform.exists());
|
||||||
|
write(outputStreamLengthRecorder, read(sourceFileCreatedByTransform) + CHANGE);
|
||||||
|
|
||||||
|
transformManager.copyTargetFileToOutputStream();
|
||||||
|
transformManager.getOutputStream().close();
|
||||||
|
closeInputStreamWithoutException(inputStream);
|
||||||
|
Long outputLength = transformManager.getOutputLength();
|
||||||
|
transformManager.deleteSourceFile();
|
||||||
|
transformManager.deleteTargetFile();
|
||||||
|
|
||||||
|
assertEquals(EXPECTED, read(outputStream));
|
||||||
|
assertEquals(EXPECTED.length(), outputLength);
|
||||||
|
assertFalse(sourceFileCreatedByTransform.exists());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user