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
278f1405ae
commit
b5ed905fed
@ -26,19 +26,16 @@
|
|||||||
*/
|
*/
|
||||||
package org.alfresco.transform.base.fs;
|
package org.alfresco.transform.base.fs;
|
||||||
|
|
||||||
import jakarta.servlet.http.Part;
|
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
|
||||||
import org.alfresco.transform.base.logging.LogEntry;
|
|
||||||
import org.alfresco.transform.base.util.Util;
|
import static org.springframework.http.HttpHeaders.CONTENT_DISPOSITION;
|
||||||
import org.alfresco.transform.common.ExtensionService;
|
import static org.springframework.http.HttpStatus.BAD_REQUEST;
|
||||||
import org.alfresco.transform.exceptions.TransformException;
|
import static org.springframework.http.HttpStatus.INSUFFICIENT_STORAGE;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
|
||||||
import org.springframework.core.io.Resource;
|
import static org.springframework.util.StringUtils.getFilename;
|
||||||
import org.springframework.core.io.UrlResource;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
import static org.alfresco.transform.common.ExtensionService.getExtensionForMimetype;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
|
||||||
import org.springframework.web.util.UriUtils;
|
|
||||||
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@ -47,14 +44,20 @@ import java.net.MalformedURLException;
|
|||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.Part;
|
||||||
|
|
||||||
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import static org.alfresco.transform.common.ExtensionService.getExtensionForMimetype;
|
import org.springframework.core.io.Resource;
|
||||||
import static org.springframework.http.HttpHeaders.CONTENT_DISPOSITION;
|
import org.springframework.core.io.UrlResource;
|
||||||
import static org.springframework.http.HttpStatus.BAD_REQUEST;
|
import org.springframework.http.ResponseEntity;
|
||||||
import static org.springframework.http.HttpStatus.INSUFFICIENT_STORAGE;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
|
import org.springframework.web.util.UriUtils;
|
||||||
import static org.springframework.util.StringUtils.getFilename;
|
|
||||||
|
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
|
public class FileManager
|
||||||
{
|
{
|
||||||
@ -62,8 +65,7 @@ public class FileManager
|
|||||||
public static final String TARGET_FILE = "targetFile";
|
public static final String TARGET_FILE = "targetFile";
|
||||||
|
|
||||||
private FileManager()
|
private FileManager()
|
||||||
{
|
{}
|
||||||
}
|
|
||||||
|
|
||||||
public static File createSourceFile(HttpServletRequest request, InputStream inputStream, String sourceMimetype)
|
public static File createSourceFile(HttpServletRequest request, InputStream inputStream, String sourceMimetype)
|
||||||
{
|
{
|
||||||
@ -72,7 +74,8 @@ public class FileManager
|
|||||||
|
|
||||||
String extension = "." + getExtensionForMimetype(sourceMimetype);
|
String extension = "." + getExtensionForMimetype(sourceMimetype);
|
||||||
File file;
|
File file;
|
||||||
if (request != null && request.getParts() != null) {
|
if (request != null && request.getParts() != null)
|
||||||
|
{
|
||||||
String submittedFileName = request.getParts().stream()
|
String submittedFileName = request.getParts().stream()
|
||||||
.map(Part::getSubmittedFileName)
|
.map(Part::getSubmittedFileName)
|
||||||
.findFirst()
|
.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
|
try
|
||||||
{
|
{
|
||||||
File file = TempFileProvider.createTempDirForDocFile(sourceFileName);
|
File file = TempFileProvider.createTempDirForDocFile(sourceFileName);
|
||||||
@ -111,19 +114,19 @@ public class FileManager
|
|||||||
}
|
}
|
||||||
LogEntry.setSource(file.getName(), file.length());
|
LogEntry.setSource(file.getName(), file.length());
|
||||||
return file;
|
return file;
|
||||||
} catch (Exception e)
|
}
|
||||||
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
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)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
String extension = "."+ExtensionService.getExtensionForTargetMimetype(targetMimetype, sourceMimetype);
|
String extension = "." + ExtensionService.getExtensionForTargetMimetype(targetMimetype, sourceMimetype);
|
||||||
File file = TempFileProvider.createTempFile("target_", extension);
|
File file = TempFileProvider.createTempFile("target_", extension);
|
||||||
if (request != null)
|
if (request != null)
|
||||||
{
|
{
|
||||||
@ -158,20 +161,20 @@ public class FileManager
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
throw new TransformException(INTERNAL_SERVER_ERROR,
|
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)
|
catch (MalformedURLException e)
|
||||||
{
|
{
|
||||||
throw new TransformException(INTERNAL_SERVER_ERROR,
|
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)
|
public static InputStream getMultipartFileInputStream(MultipartFile sourceMultipartFile)
|
||||||
{
|
{
|
||||||
InputStream inputStream;
|
InputStream inputStream;
|
||||||
if (sourceMultipartFile == null)
|
if (sourceMultipartFile == null)
|
||||||
{
|
{
|
||||||
throw new TransformException(BAD_REQUEST, "Required request part 'file' is not present");
|
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 should never be null (will be "transform."+<something>), so we should not worry about encodePath(null)
|
||||||
targetFilename = UriUtils.encodePath(getFilename(targetFilename), "UTF-8");
|
targetFilename = UriUtils.encodePath(getFilename(targetFilename), "UTF-8");
|
||||||
return ResponseEntity.ok().header(CONTENT_DISPOSITION,
|
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
|
public static class TempFileProvider
|
||||||
{
|
{
|
||||||
private TempFileProvider()
|
private TempFileProvider()
|
||||||
{
|
{}
|
||||||
}
|
|
||||||
|
|
||||||
public static File createTempFile(final String prefix, final String suffix)
|
public static File createTempFile(final String prefix, final String suffix)
|
||||||
{
|
{
|
||||||
@ -252,8 +254,9 @@ public class FileManager
|
|||||||
catch (IOException e)
|
catch (IOException e)
|
||||||
{
|
{
|
||||||
throw new RuntimeException(
|
throw new RuntimeException(
|
||||||
"Failed to created temp file: \n prefix: " + prefix +
|
"Failed to created temp file: \n prefix: " + prefix +
|
||||||
"\n suffix: " + suffix + "\n directory: " + directory, e);
|
"\n suffix: " + suffix + "\n directory: " + directory,
|
||||||
|
e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -272,7 +275,7 @@ public class FileManager
|
|||||||
}
|
}
|
||||||
catch (Exception e)
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,17 +21,17 @@
|
|||||||
*/
|
*/
|
||||||
package org.alfresco.transform.client.model;
|
package org.alfresco.transform.client.model;
|
||||||
|
|
||||||
import org.alfresco.transform.common.ExtensionService;
|
import static org.alfresco.transform.messages.TransformStack.PIPELINE_FLAG;
|
||||||
import org.alfresco.transform.messages.TransformStack;
|
import static org.alfresco.transform.messages.TransformStack.levelBuilder;
|
||||||
|
import static org.alfresco.transform.messages.TransformStack.setInitialTransformRequestOptions;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
import static org.alfresco.transform.messages.TransformStack.PIPELINE_FLAG;
|
import org.alfresco.transform.common.ExtensionService;
|
||||||
import static org.alfresco.transform.messages.TransformStack.levelBuilder;
|
import org.alfresco.transform.messages.TransformStack;
|
||||||
import static org.alfresco.transform.messages.TransformStack.setInitialTransformRequestOptions;
|
|
||||||
|
|
||||||
// This class is in the package org.alfresco.transform.messages in HxP because that is more readable, but in
|
// 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.
|
// org.alfresco.transform.client.model in Alfresco for backward compatibility.
|
||||||
@ -161,15 +161,17 @@ public class TransformRequest implements Serializable
|
|||||||
this.internalContext = internalContext;
|
this.internalContext = internalContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSourceFileName() {
|
public String getSourceFileName()
|
||||||
|
{
|
||||||
return sourceFileName;
|
return sourceFileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSourceFileName(String sourceFileName) {
|
public void setSourceFileName(String sourceFileName)
|
||||||
|
{
|
||||||
this.sourceFileName = sourceFileName;
|
this.sourceFileName = sourceFileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
//endregion
|
// endregion
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o)
|
public boolean equals(Object o)
|
||||||
@ -188,34 +190,32 @@ public class TransformRequest implements Serializable
|
|||||||
return Objects.hash(requestId);
|
return Objects.hash(requestId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public String toString()
|
@Override
|
||||||
|
public String toString()
|
||||||
{
|
{
|
||||||
return "TransformRequest{" +
|
return "TransformRequest{" +
|
||||||
"requestId='" + requestId + '\'' +
|
"requestId='" + requestId + '\'' +
|
||||||
", sourceReference='" + sourceReference + '\'' +
|
", sourceReference='" + sourceReference + '\'' +
|
||||||
", sourceMediaType='" + sourceMediaType + '\'' +
|
", sourceMediaType='" + sourceMediaType + '\'' +
|
||||||
", sourceSize=" + sourceSize +
|
", sourceSize=" + sourceSize +
|
||||||
", sourceExtension='" + sourceExtension + '\'' +
|
", sourceExtension='" + sourceExtension + '\'' +
|
||||||
", targetMediaType='" + targetMediaType + '\'' +
|
", targetMediaType='" + targetMediaType + '\'' +
|
||||||
", targetExtension='" + targetExtension + '\'' +
|
", targetExtension='" + targetExtension + '\'' +
|
||||||
", clientData='" + clientData + '\'' +
|
", clientData='" + clientData + '\'' +
|
||||||
", schema=" + schema +
|
", schema=" + schema +
|
||||||
", transformRequestOptions=" + transformRequestOptions +
|
", transformRequestOptions=" + transformRequestOptions +
|
||||||
", internalContext=" + internalContext +
|
", internalContext=" + internalContext +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets up the internal context structure when a client request is initially received by the router,
|
* 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.
|
||||||
* 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()
|
public TransformRequest initialiseContextWhenReceivedByRouter()
|
||||||
{
|
{
|
||||||
setInternalContext(InternalContext.initialise(getInternalContext()));
|
setInternalContext(InternalContext.initialise(getInternalContext()));
|
||||||
setTargetExtension(ExtensionService.getExtensionForTargetMimetype(getTargetMediaType(),
|
setTargetExtension(ExtensionService.getExtensionForTargetMimetype(getTargetMediaType(),
|
||||||
getSourceMediaType()));
|
getSourceMediaType()));
|
||||||
getInternalContext().getMultiStep().setInitialRequestId(getRequestId());
|
getInternalContext().getMultiStep().setInitialRequestId(getRequestId());
|
||||||
getInternalContext().getMultiStep().setInitialSourceMediaType(getSourceMediaType());
|
getInternalContext().getMultiStep().setInitialSourceMediaType(getSourceMediaType());
|
||||||
getInternalContext().setTransformRequestOptions(getTransformRequestOptions());
|
getInternalContext().setTransformRequestOptions(getTransformRequestOptions());
|
||||||
@ -233,7 +233,8 @@ public class TransformRequest implements Serializable
|
|||||||
{
|
{
|
||||||
private final TransformRequest request = new TransformRequest();
|
private final TransformRequest request = new TransformRequest();
|
||||||
|
|
||||||
private Builder() {}
|
private Builder()
|
||||||
|
{}
|
||||||
|
|
||||||
public Builder withRequestId(final String requestId)
|
public Builder withRequestId(final String requestId)
|
||||||
{
|
{
|
||||||
@ -290,7 +291,7 @@ public class TransformRequest implements Serializable
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Builder withTransformRequestOptions(
|
public Builder withTransformRequestOptions(
|
||||||
final Map<String, String> transformRequestOptions)
|
final Map<String, String> transformRequestOptions)
|
||||||
{
|
{
|
||||||
request.transformRequestOptions = transformRequestOptions;
|
request.transformRequestOptions = transformRequestOptions;
|
||||||
return this;
|
return this;
|
||||||
@ -306,11 +307,12 @@ public class TransformRequest implements Serializable
|
|||||||
{
|
{
|
||||||
request.initialiseContextWhenReceivedByRouter();
|
request.initialiseContextWhenReceivedByRouter();
|
||||||
TransformStack.addTransformLevel(request.internalContext, levelBuilder(PIPELINE_FLAG)
|
TransformStack.addTransformLevel(request.internalContext, levelBuilder(PIPELINE_FLAG)
|
||||||
.withStep("dummyTransformerName", request.sourceMediaType, request.targetMediaType));
|
.withStep("dummyTransformerName", request.sourceMediaType, request.targetMediaType));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Builder withSourceFileName(final String sourceFileName) {
|
public Builder withSourceFileName(final String sourceFileName)
|
||||||
|
{
|
||||||
request.sourceFileName = sourceFileName;
|
request.sourceFileName = sourceFileName;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user