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);
}
}