Resolve wkhtml and formatting

This commit is contained in:
Anthony Stirling 2024-01-18 23:28:39 +00:00
parent 2fa68be36b
commit 75cf3ed0c1
3 changed files with 93 additions and 76 deletions

View file

@ -2,6 +2,7 @@ package stirling.software.SPDF.controller.api.pipeline;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -23,7 +24,7 @@ import jakarta.servlet.ServletContext;
import stirling.software.SPDF.SPdfApplication; import stirling.software.SPDF.SPdfApplication;
import stirling.software.SPDF.model.ApiEndpoint; import stirling.software.SPDF.model.ApiEndpoint;
import stirling.software.SPDF.model.Role; import stirling.software.SPDF.model.Role;
import java.util.List;
@Service @Service
public class ApiDocService { public class ApiDocService {
@ -39,13 +40,20 @@ public class ApiDocService {
return "http://localhost:" + port + contextPath + "/v1/api-docs"; return "http://localhost:" + port + contextPath + "/v1/api-docs";
} }
Map<String, List<String>> outputToFileTypes = new HashMap<>(); Map<String, List<String>> outputToFileTypes = new HashMap<>();
public List getExtensionTypes(boolean output, String operationName) { public List getExtensionTypes(boolean output, String operationName) {
if(outputToFileTypes.size() == 0) { if (outputToFileTypes.size() == 0) {
outputToFileTypes.put("PDF", Arrays.asList("pdf")); outputToFileTypes.put("PDF", Arrays.asList("pdf"));
outputToFileTypes.put("IMAGE", Arrays.asList("png", "jpg", "jpeg", "gif", "webp", "bmp", "tif", "tiff", "svg", "psd", "ai", "eps")); outputToFileTypes.put(
outputToFileTypes.put("ZIP", Arrays.asList("zip", "rar", "7z", "tar", "gz", "bz2", "xz", "lz", "lzma", "z")); "IMAGE",
Arrays.asList(
"png", "jpg", "jpeg", "gif", "webp", "bmp", "tif", "tiff", "svg", "psd",
"ai", "eps"));
outputToFileTypes.put(
"ZIP",
Arrays.asList("zip", "rar", "7z", "tar", "gz", "bz2", "xz", "lz", "lzma", "z"));
outputToFileTypes.put("WORD", Arrays.asList("doc", "docx", "odt", "rtf")); outputToFileTypes.put("WORD", Arrays.asList("doc", "docx", "odt", "rtf"));
outputToFileTypes.put("CSV", Arrays.asList("csv")); outputToFileTypes.put("CSV", Arrays.asList("csv"));
outputToFileTypes.put("JS", Arrays.asList("js", "jsx")); outputToFileTypes.put("JS", Arrays.asList("js", "jsx"));
@ -54,7 +62,12 @@ public class ApiDocService {
outputToFileTypes.put("TXT", Arrays.asList("txt", "text", "md", "markdown")); outputToFileTypes.put("TXT", Arrays.asList("txt", "text", "md", "markdown"));
outputToFileTypes.put("PPT", Arrays.asList("ppt", "pptx", "odp")); outputToFileTypes.put("PPT", Arrays.asList("ppt", "pptx", "odp"));
outputToFileTypes.put("XML", Arrays.asList("xml", "xsd", "xsl")); outputToFileTypes.put("XML", Arrays.asList("xml", "xsd", "xsl"));
outputToFileTypes.put("BOOK", Arrays.asList("epub", "mobi", "azw3", "fb2", "txt", "docx")); // As noted before, "Boolean" isn't a file type but a value type. outputToFileTypes.put(
"BOOK",
Arrays.asList(
"epub", "mobi", "azw3", "fb2", "txt",
"docx")); // As noted before, "Boolean" isn't a file type but a value
// type.
} }
if (apiDocsJsonRootNode == null || apiDocumentation.size() == 0) { if (apiDocsJsonRootNode == null || apiDocumentation.size() == 0) {
@ -67,7 +80,7 @@ public class ApiDocService {
ApiEndpoint endpoint = apiDocumentation.get(operationName); ApiEndpoint endpoint = apiDocumentation.get(operationName);
String description = endpoint.getDescription(); String description = endpoint.getDescription();
Pattern pattern = null; Pattern pattern = null;
if(output) { if (output) {
pattern = Pattern.compile("Output:(\\w+)"); pattern = Pattern.compile("Output:(\\w+)");
} else { } else {
pattern = Pattern.compile("Input:(\\w+)"); pattern = Pattern.compile("Input:(\\w+)");
@ -75,7 +88,7 @@ public class ApiDocService {
Matcher matcher = pattern.matcher(description); Matcher matcher = pattern.matcher(description);
while (matcher.find()) { while (matcher.find()) {
String type = matcher.group(1).toUpperCase(); String type = matcher.group(1).toUpperCase();
if(outputToFileTypes.containsKey(type)) { if (outputToFileTypes.containsKey(type)) {
return outputToFileTypes.get(type); return outputToFileTypes.get(type);
} }
} }

View file

@ -84,12 +84,10 @@ public class PipelineProcessor {
isMultiInputOperation); isMultiInputOperation);
Map<String, Object> parameters = pipelineOperation.getParameters(); Map<String, Object> parameters = pipelineOperation.getParameters();
List<String> inputFileTypes = apiDocService.getExtensionTypes(false, operation); List<String> inputFileTypes = apiDocService.getExtensionTypes(false, operation);
if(inputFileTypes == null) { if (inputFileTypes == null) {
inputFileTypes = new ArrayList<String>(Arrays.asList("ALL")); inputFileTypes = new ArrayList<String>(Arrays.asList("ALL"));
} }
//List outputFileTypes = apiDocService.getExtensionTypes(true, operation); // List outputFileTypes = apiDocService.getExtensionTypes(true, operation);
String url = getBaseUrl() + operation; String url = getBaseUrl() + operation;
@ -109,11 +107,13 @@ public class PipelineProcessor {
ResponseEntity<byte[]> response = sendWebRequest(url, body); ResponseEntity<byte[]> response = sendWebRequest(url, body);
// If the operation is filter and the response body is null or empty, skip // If the operation is filter and the response body is null or empty,
// skip
// this // this
// file // file
if (operation.startsWith("filter-") if (operation.startsWith("filter-")
&& (response.getBody() == null || response.getBody().length == 0)) { && (response.getBody() == null
|| response.getBody().length == 0)) {
logger.info("Skipping file due to failing {}", operation); logger.info("Skipping file due to failing {}", operation);
continue; continue;
} }
@ -146,7 +146,10 @@ public class PipelineProcessor {
final List<String> finalinputFileTypes = inputFileTypes; final List<String> finalinputFileTypes = inputFileTypes;
matchingFiles = matchingFiles =
outputFiles.stream() outputFiles.stream()
.filter(file -> finalinputFileTypes.stream().anyMatch(file.getFilename()::endsWith)) .filter(
file ->
finalinputFileTypes.stream()
.anyMatch(file.getFilename()::endsWith))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@ -168,10 +171,7 @@ public class PipelineProcessor {
// Handle the response // Handle the response
if (response.getStatusCode().equals(HttpStatus.OK)) { if (response.getStatusCode().equals(HttpStatus.OK)) {
processOutputFiles( processOutputFiles(operation, response, newOutputFiles);
operation,
response,
newOutputFiles);
} else { } else {
// Log error if the response status is not OK // Log error if the response status is not OK
logPrintStream.println( logPrintStream.println(
@ -236,9 +236,7 @@ public class PipelineProcessor {
} }
private List<Resource> processOutputFiles( private List<Resource> processOutputFiles(
String operation, String operation, ResponseEntity<byte[]> response, List<Resource> newOutputFiles)
ResponseEntity<byte[]> response,
List<Resource> newOutputFiles)
throws IOException { throws IOException {
// Define filename // Define filename
String newFilename; String newFilename;

View file

@ -63,7 +63,13 @@ public class FileToPdf {
} }
pdfBytes = Files.readAllBytes(tempOutputFile); pdfBytes = Files.readAllBytes(tempOutputFile);
} catch (IOException e) {
pdfBytes = Files.readAllBytes(tempOutputFile);
if (pdfBytes.length < 1) {
throw e;
}
} finally { } finally {
// Clean up temporary files // Clean up temporary files
Files.delete(tempOutputFile); Files.delete(tempOutputFile);
Files.delete(tempInputFile); Files.delete(tempInputFile);