MNT-24883 office pdf render header-fix

This commit is contained in:
bsayan2 2025-05-21 12:13:51 +05:30
parent 0c534f1081
commit f6d03a18a2
5 changed files with 94 additions and 6 deletions

View File

@ -26,6 +26,7 @@
*/
package org.alfresco.transform.base.fs;
import jakarta.servlet.http.Part;
import org.alfresco.transform.base.logging.LogEntry;
import org.alfresco.transform.common.ExtensionService;
import org.alfresco.transform.exceptions.TransformException;
@ -43,6 +44,7 @@ import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.util.UUID;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static org.alfresco.transform.common.ExtensionService.getExtensionForMimetype;
@ -65,8 +67,23 @@ public class FileManager
{
try
{
String extension = "."+getExtensionForMimetype(sourceMimetype);
File file = TempFileProvider.createTempFile("source_", extension);
String extension = "." + getExtensionForMimetype(sourceMimetype);
File file;
if (request != null && request.getParts() != null) {
String submittedFileName = request.getParts().stream()
.map(Part::getSubmittedFileName)
.filter(name -> name != null && extension.contains(".doc"))
.findFirst()
.orElse(null);
file = (submittedFileName != null)
? TempFileProvider.createTempDirForDocFile(submittedFileName)
: TempFileProvider.createTempFile("source_", extension);
}
else
{
file = TempFileProvider.createTempFile("source_", extension);
}
Files.copy(inputStream, file.toPath(), REPLACE_EXISTING);
if (request != null)
{
@ -81,6 +98,27 @@ public class FileManager
}
}
public static File createSourceFileWithSameName(HttpServletRequest request, String sourceFileName, InputStream inputStream, String sourceMimetype) {
try
{
File file = TempFileProvider.createTempDirForDocFile(sourceFileName);
Files.copy(inputStream, file.toPath(), REPLACE_EXISTING);
if (request != null)
{
request.setAttribute(SOURCE_FILE, file);
}
LogEntry.setSource(file.getName(), file.length());
return file;
} catch (Exception e)
{
throw new TransformException(INSUFFICIENT_STORAGE, "Failed to store the source file", e);
}
}
public static File createTargetFile(HttpServletRequest request, String sourceMimetype, String targetMimetype)
{
try
@ -219,6 +257,27 @@ public class FileManager
}
}
public static File createTempDirForDocFile(String sourceFileName)
{
final File alfrescoTempDirectory = getTempDir();
try
{
UUID uuid = UUID.randomUUID();
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);
// return File.createTempFile(sourceFileName, ".docx", tempDir);
// return File.createTempFile(sourceFileName, ".docx", alfrescoTempDirectory);
}
catch (Exception e)
{
throw new RuntimeException("Failed to created temp file: \n file: " + sourceFileName +"\n", e);
}
}
private static File getTempDir()
{
final String dirName = "Alfresco";

View File

@ -68,6 +68,7 @@ abstract class ProcessHandler extends FragmentHandler
private static final List<String> NON_TRANSFORM_OPTION_REQUEST_PARAMETERS = Arrays.asList(SOURCE_EXTENSION,
TARGET_EXTENSION, TARGET_MIMETYPE, SOURCE_MIMETYPE, DIRECT_ACCESS_URL);
protected String sourceFileName=null;
protected final String sourceMimetype;
protected final String targetMimetype;
private final Map<String, String> transformOptions;
@ -92,6 +93,22 @@ abstract class ProcessHandler extends FragmentHandler
this.customTransformers = customTransformers;
}
ProcessHandler(String sourceFileName,String sourceMimetype, String targetMimetype, Map<String, String> transformOptions,
String reference, TransformServiceRegistry transformRegistry, TransformerDebug transformerDebug,
ProbeTransform probeTransform, CustomTransformers customTransformers)
{
this.sourceFileName = sourceFileName;
this.sourceMimetype = sourceMimetype;
this.targetMimetype = targetMimetype;
this.transformOptions = cleanTransformOptions(transformOptions);
this.reference = reference;
this.transformRegistry = transformRegistry;
this.transformerDebug = transformerDebug;
this.probeTransform = probeTransform;
this.customTransformers = customTransformers;
}
private static Map<String, String> cleanTransformOptions(Map<String, String> requestParameters)
{
Map<String, String> transformOptions = new HashMap<>(requestParameters);

View File

@ -190,7 +190,7 @@ public class TransformHandler
ProbeTransform probeTransform)
{
TransformReply reply = createBasicTransformReply(request);
new ProcessHandler(request.getSourceMediaType(), request.getTargetMediaType(),
new ProcessHandler(request.getSourceFileName(),request.getSourceMediaType(), request.getTargetMediaType(),
request.getTransformRequestOptions(),"unset", transformRegistry,
transformerDebug, probeTransform, customTransformers)
{

View File

@ -29,6 +29,7 @@ package org.alfresco.transform.base.transform;
import org.alfresco.transform.base.TransformManager;
import org.alfresco.transform.base.fs.FileManager;
import org.alfresco.transform.base.util.OutputStreamLengthRecorder;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@ -53,6 +54,7 @@ public class TransformManagerImpl implements TransformManager
private OutputStreamLengthRecorder outputStreamLengthRecorder;
private String sourceMimetype;
private String targetMimetype;
private String sourceFileName;
private File sourceFile;
private File targetFile;
private boolean keepTargetFile;
@ -157,9 +159,10 @@ public class TransformManagerImpl implements TransformManager
}
createSourceFileCalled = true;
if (sourceFile == null)
{
sourceFile = FileManager.createSourceFile(request, inputStream, sourceMimetype);
if (sourceFile == null) {
sourceFile = StringUtils.isEmpty(sourceFileName)
? FileManager.createSourceFile(request, inputStream, sourceMimetype)
: FileManager.createSourceFileWithSameName(request, sourceFileName, inputStream, sourceMimetype);
}
return sourceFile;
}

View File

@ -48,6 +48,7 @@ public class TransformRequest implements Serializable
private int schema;
private Map<String, String> transformRequestOptions = new HashMap<>();
private InternalContext internalContext;
private String sourceFileName;
// regions [Accessors]
public String getRequestId()
@ -160,6 +161,14 @@ public class TransformRequest implements Serializable
this.internalContext = internalContext;
}
public String getSourceFileName() {
return sourceFileName;
}
public void setSourceFileName(String sourceFileName) {
this.sourceFileName = sourceFileName;
}
//endregion
@Override