MNT-24883 base engine changes to maintain source file name

This commit is contained in:
bsayan2 2025-05-23 10:41:14 +05:30
parent 278f1405ae
commit b5ed905fed
2 changed files with 71 additions and 66 deletions

View File

@ -26,19 +26,16 @@
*/
package org.alfresco.transform.base.fs;
import jakarta.servlet.http.Part;
import org.alfresco.transform.base.logging.LogEntry;
import org.alfresco.transform.base.util.Util;
import org.alfresco.transform.common.ExtensionService;
import org.alfresco.transform.exceptions.TransformException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.UriUtils;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static org.springframework.http.HttpHeaders.CONTENT_DISPOSITION;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.INSUFFICIENT_STORAGE;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.util.StringUtils.getFilename;
import static org.alfresco.transform.common.ExtensionService.getExtensionForMimetype;
import jakarta.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@ -47,14 +44,20 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.util.UUID;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.Part;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static org.alfresco.transform.common.ExtensionService.getExtensionForMimetype;
import static org.springframework.http.HttpHeaders.CONTENT_DISPOSITION;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.INSUFFICIENT_STORAGE;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.util.StringUtils.getFilename;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.UriUtils;
import org.alfresco.transform.base.logging.LogEntry;
import org.alfresco.transform.base.util.Util;
import org.alfresco.transform.common.ExtensionService;
import org.alfresco.transform.exceptions.TransformException;
public class FileManager
{
@ -62,8 +65,7 @@ public class FileManager
public static final String TARGET_FILE = "targetFile";
private FileManager()
{
}
{}
public static File createSourceFile(HttpServletRequest request, InputStream inputStream, String sourceMimetype)
{
@ -72,7 +74,8 @@ public class FileManager
String extension = "." + getExtensionForMimetype(sourceMimetype);
File file;
if (request != null && request.getParts() != null) {
if (request != null && request.getParts() != null)
{
String submittedFileName = request.getParts().stream()
.map(Part::getSubmittedFileName)
.findFirst()
@ -99,8 +102,8 @@ public class FileManager
}
}
public static File createSourceDocFileWithSameName(HttpServletRequest request, String sourceFileName, InputStream inputStream, String sourceMimetype) {
public static File createSourceDocFileWithSameName(HttpServletRequest request, String sourceFileName, InputStream inputStream, String sourceMimetype)
{
try
{
File file = TempFileProvider.createTempDirForDocFile(sourceFileName);
@ -111,19 +114,19 @@ public class FileManager
}
LogEntry.setSource(file.getName(), file.length());
return file;
} catch (Exception e)
}
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
{
String extension = "."+ExtensionService.getExtensionForTargetMimetype(targetMimetype, sourceMimetype);
String extension = "." + ExtensionService.getExtensionForTargetMimetype(targetMimetype, sourceMimetype);
File file = TempFileProvider.createTempFile("target_", extension);
if (request != null)
{
@ -158,20 +161,20 @@ public class FileManager
else
{
throw new TransformException(INTERNAL_SERVER_ERROR,
"Could not read the target file: " + file.getPath());
"Could not read the target file: " + file.getPath());
}
}
catch (MalformedURLException e)
{
throw new TransformException(INTERNAL_SERVER_ERROR,
"The target filename was malformed: " + file.getPath(), e);
"The target filename was malformed: " + file.getPath(), e);
}
}
public static InputStream getMultipartFileInputStream(MultipartFile sourceMultipartFile)
{
InputStream inputStream;
if (sourceMultipartFile == null)
if (sourceMultipartFile == null)
{
throw new TransformException(BAD_REQUEST, "Required request part 'file' is not present");
}
@ -230,7 +233,7 @@ public class FileManager
// targetFilename should never be null (will be "transform."+<something>), so we should not worry about encodePath(null)
targetFilename = UriUtils.encodePath(getFilename(targetFilename), "UTF-8");
return ResponseEntity.ok().header(CONTENT_DISPOSITION,
"attachment; filename*=UTF-8''" + targetFilename).body(targetResource);
"attachment; filename*=UTF-8''" + targetFilename).body(targetResource);
}
/**
@ -239,8 +242,7 @@ public class FileManager
public static class TempFileProvider
{
private TempFileProvider()
{
}
{}
public static File createTempFile(final String prefix, final String suffix)
{
@ -252,8 +254,9 @@ public class FileManager
catch (IOException e)
{
throw new RuntimeException(
"Failed to created temp file: \n prefix: " + prefix +
"\n suffix: " + suffix + "\n directory: " + directory, e);
"Failed to created temp file: \n prefix: " + prefix +
"\n suffix: " + suffix + "\n directory: " + directory,
e);
}
}
@ -272,7 +275,7 @@ public class FileManager
}
catch (Exception e)
{
throw new RuntimeException("Failed to created temp file: \n file: " + sourceFileName +"\n", e);
throw new RuntimeException("Failed to created temp file: \n file: " + sourceFileName + "\n", e);
}
}

View File

@ -21,17 +21,17 @@
*/
package org.alfresco.transform.client.model;
import org.alfresco.transform.common.ExtensionService;
import org.alfresco.transform.messages.TransformStack;
import static org.alfresco.transform.messages.TransformStack.PIPELINE_FLAG;
import static org.alfresco.transform.messages.TransformStack.levelBuilder;
import static org.alfresco.transform.messages.TransformStack.setInitialTransformRequestOptions;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import static org.alfresco.transform.messages.TransformStack.PIPELINE_FLAG;
import static org.alfresco.transform.messages.TransformStack.levelBuilder;
import static org.alfresco.transform.messages.TransformStack.setInitialTransformRequestOptions;
import org.alfresco.transform.common.ExtensionService;
import org.alfresco.transform.messages.TransformStack;
// This class is in the package org.alfresco.transform.messages in HxP because that is more readable, but in
// org.alfresco.transform.client.model in Alfresco for backward compatibility.
@ -161,15 +161,17 @@ public class TransformRequest implements Serializable
this.internalContext = internalContext;
}
public String getSourceFileName() {
public String getSourceFileName()
{
return sourceFileName;
}
public void setSourceFileName(String sourceFileName) {
public void setSourceFileName(String sourceFileName)
{
this.sourceFileName = sourceFileName;
}
//endregion
// endregion
@Override
public boolean equals(Object o)
@ -188,34 +190,32 @@ public class TransformRequest implements Serializable
return Objects.hash(requestId);
}
@Override public String toString()
@Override
public String toString()
{
return "TransformRequest{" +
"requestId='" + requestId + '\'' +
", sourceReference='" + sourceReference + '\'' +
", sourceMediaType='" + sourceMediaType + '\'' +
", sourceSize=" + sourceSize +
", sourceExtension='" + sourceExtension + '\'' +
", targetMediaType='" + targetMediaType + '\'' +
", targetExtension='" + targetExtension + '\'' +
", clientData='" + clientData + '\'' +
", schema=" + schema +
", transformRequestOptions=" + transformRequestOptions +
", internalContext=" + internalContext +
'}';
"requestId='" + requestId + '\'' +
", sourceReference='" + sourceReference + '\'' +
", sourceMediaType='" + sourceMediaType + '\'' +
", sourceSize=" + sourceSize +
", sourceExtension='" + sourceExtension + '\'' +
", targetMediaType='" + targetMediaType + '\'' +
", targetExtension='" + targetExtension + '\'' +
", clientData='" + clientData + '\'' +
", schema=" + schema +
", transformRequestOptions=" + transformRequestOptions +
", internalContext=" + internalContext +
'}';
}
/**
* Sets up the internal context structure when a client request is initially received by the router,
* so that we don't have to keep checking if bits of it are initialised. Prior to making this call,
* the id, sourceMimetypes, targetMimetype, transformRequestOptions and sourceReference should have
* been set, if they are to be set.
* Sets up the internal context structure when a client request is initially received by the router, so that we don't have to keep checking if bits of it are initialised. Prior to making this call, the id, sourceMimetypes, targetMimetype, transformRequestOptions and sourceReference should have been set, if they are to be set.
*/
public TransformRequest initialiseContextWhenReceivedByRouter()
{
setInternalContext(InternalContext.initialise(getInternalContext()));
setTargetExtension(ExtensionService.getExtensionForTargetMimetype(getTargetMediaType(),
getSourceMediaType()));
getSourceMediaType()));
getInternalContext().getMultiStep().setInitialRequestId(getRequestId());
getInternalContext().getMultiStep().setInitialSourceMediaType(getSourceMediaType());
getInternalContext().setTransformRequestOptions(getTransformRequestOptions());
@ -233,7 +233,8 @@ public class TransformRequest implements Serializable
{
private final TransformRequest request = new TransformRequest();
private Builder() {}
private Builder()
{}
public Builder withRequestId(final String requestId)
{
@ -290,7 +291,7 @@ public class TransformRequest implements Serializable
}
public Builder withTransformRequestOptions(
final Map<String, String> transformRequestOptions)
final Map<String, String> transformRequestOptions)
{
request.transformRequestOptions = transformRequestOptions;
return this;
@ -306,11 +307,12 @@ public class TransformRequest implements Serializable
{
request.initialiseContextWhenReceivedByRouter();
TransformStack.addTransformLevel(request.internalContext, levelBuilder(PIPELINE_FLAG)
.withStep("dummyTransformerName", request.sourceMediaType, request.targetMediaType));
.withStep("dummyTransformerName", request.sourceMediaType, request.targetMediaType));
return this;
}
public Builder withSourceFileName(final String sourceFileName) {
public Builder withSourceFileName(final String sourceFileName)
{
request.sourceFileName = sourceFileName;
return this;
}