From 75cf3ed0c119c6d9b87d027054c4fe64427d318a Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Thu, 18 Jan 2024 23:28:39 +0000 Subject: [PATCH] Resolve wkhtml and formatting --- .../api/pipeline/ApiDocService.java | 61 ++++++----- .../api/pipeline/PipelineProcessor.java | 102 +++++++++--------- .../software/SPDF/utils/FileToPdf.java | 6 ++ 3 files changed, 93 insertions(+), 76 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/controller/api/pipeline/ApiDocService.java b/src/main/java/stirling/software/SPDF/controller/api/pipeline/ApiDocService.java index 9df659ac..25537ee2 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/pipeline/ApiDocService.java +++ b/src/main/java/stirling/software/SPDF/controller/api/pipeline/ApiDocService.java @@ -2,6 +2,7 @@ package stirling.software.SPDF.controller.api.pipeline; import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -23,7 +24,7 @@ import jakarta.servlet.ServletContext; import stirling.software.SPDF.SPdfApplication; import stirling.software.SPDF.model.ApiEndpoint; import stirling.software.SPDF.model.Role; -import java.util.List; + @Service public class ApiDocService { @@ -39,25 +40,37 @@ public class ApiDocService { return "http://localhost:" + port + contextPath + "/v1/api-docs"; } + Map> outputToFileTypes = new HashMap<>(); - - public List getExtensionTypes(boolean output, String operationName) { - if(outputToFileTypes.size() == 0) { - 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("ZIP", Arrays.asList("zip", "rar", "7z", "tar", "gz", "bz2", "xz", "lz", "lzma", "z")); - outputToFileTypes.put("WORD", Arrays.asList("doc", "docx", "odt", "rtf")); - outputToFileTypes.put("CSV", Arrays.asList("csv")); - outputToFileTypes.put("JS", Arrays.asList("js", "jsx")); - outputToFileTypes.put("HTML", Arrays.asList("html", "htm", "xhtml")); - outputToFileTypes.put("JSON", Arrays.asList("json")); - outputToFileTypes.put("TXT", Arrays.asList("txt", "text", "md", "markdown")); - outputToFileTypes.put("PPT", Arrays.asList("ppt", "pptx", "odp")); - 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. - } - - if (apiDocsJsonRootNode == null || apiDocumentation.size() == 0) { + + public List getExtensionTypes(boolean output, String operationName) { + if (outputToFileTypes.size() == 0) { + 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( + "ZIP", + Arrays.asList("zip", "rar", "7z", "tar", "gz", "bz2", "xz", "lz", "lzma", "z")); + outputToFileTypes.put("WORD", Arrays.asList("doc", "docx", "odt", "rtf")); + outputToFileTypes.put("CSV", Arrays.asList("csv")); + outputToFileTypes.put("JS", Arrays.asList("js", "jsx")); + outputToFileTypes.put("HTML", Arrays.asList("html", "htm", "xhtml")); + outputToFileTypes.put("JSON", Arrays.asList("json")); + outputToFileTypes.put("TXT", Arrays.asList("txt", "text", "md", "markdown")); + outputToFileTypes.put("PPT", Arrays.asList("ppt", "pptx", "odp")); + 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. + } + + if (apiDocsJsonRootNode == null || apiDocumentation.size() == 0) { loadApiDocumentation(); } if (!apiDocumentation.containsKey(operationName)) { @@ -67,16 +80,16 @@ public class ApiDocService { ApiEndpoint endpoint = apiDocumentation.get(operationName); String description = endpoint.getDescription(); Pattern pattern = null; - if(output) { - pattern = Pattern.compile("Output:(\\w+)"); + if (output) { + pattern = Pattern.compile("Output:(\\w+)"); } else { - pattern = Pattern.compile("Input:(\\w+)"); + pattern = Pattern.compile("Input:(\\w+)"); } Matcher matcher = pattern.matcher(description); while (matcher.find()) { String type = matcher.group(1).toUpperCase(); - if(outputToFileTypes.containsKey(type)) { - return outputToFileTypes.get(type); + if (outputToFileTypes.containsKey(type)) { + return outputToFileTypes.get(type); } } return null; diff --git a/src/main/java/stirling/software/SPDF/controller/api/pipeline/PipelineProcessor.java b/src/main/java/stirling/software/SPDF/controller/api/pipeline/PipelineProcessor.java index 392bdac8..5ff6842c 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/pipeline/PipelineProcessor.java +++ b/src/main/java/stirling/software/SPDF/controller/api/pipeline/PipelineProcessor.java @@ -84,12 +84,10 @@ public class PipelineProcessor { isMultiInputOperation); Map parameters = pipelineOperation.getParameters(); List inputFileTypes = apiDocService.getExtensionTypes(false, operation); - if(inputFileTypes == null) { - inputFileTypes = new ArrayList(Arrays.asList("ALL")); + if (inputFileTypes == null) { + inputFileTypes = new ArrayList(Arrays.asList("ALL")); } - //List outputFileTypes = apiDocService.getExtensionTypes(true, operation); - - + // List outputFileTypes = apiDocService.getExtensionTypes(true, operation); String url = getBaseUrl() + operation; @@ -98,33 +96,35 @@ public class PipelineProcessor { for (Resource file : outputFiles) { boolean hasInputFileType = false; for (String extension : inputFileTypes) { - if (extension.equals("ALL") || file.getFilename().endsWith(extension)) { - hasInputFileType = true; - MultiValueMap body = new LinkedMultiValueMap<>(); - body.add("fileInput", file); - - for (Entry entry : parameters.entrySet()) { - body.add(entry.getKey(), entry.getValue()); - } - - ResponseEntity response = sendWebRequest(url, body); - - // If the operation is filter and the response body is null or empty, skip - // this - // file - if (operation.startsWith("filter-") - && (response.getBody() == null || response.getBody().length == 0)) { - logger.info("Skipping file due to failing {}", operation); - continue; - } - - if (!response.getStatusCode().equals(HttpStatus.OK)) { - logPrintStream.println("Error: " + response.getBody()); - hasErrors = true; - continue; - } - processOutputFiles(operation, response, newOutputFiles); - } + if (extension.equals("ALL") || file.getFilename().endsWith(extension)) { + hasInputFileType = true; + MultiValueMap body = new LinkedMultiValueMap<>(); + body.add("fileInput", file); + + for (Entry entry : parameters.entrySet()) { + body.add(entry.getKey(), entry.getValue()); + } + + ResponseEntity response = sendWebRequest(url, body); + + // If the operation is filter and the response body is null or empty, + // skip + // this + // file + if (operation.startsWith("filter-") + && (response.getBody() == null + || response.getBody().length == 0)) { + logger.info("Skipping file due to failing {}", operation); + continue; + } + + if (!response.getStatusCode().equals(HttpStatus.OK)) { + logPrintStream.println("Error: " + response.getBody()); + hasErrors = true; + continue; + } + processOutputFiles(operation, response, newOutputFiles); + } } if (!hasInputFileType) { @@ -139,16 +139,19 @@ public class PipelineProcessor { } else { // Filter and collect all files that match the inputFileExtension - List matchingFiles; - if (inputFileTypes.contains("ALL")) { - matchingFiles = new ArrayList<>(outputFiles); - } else { - final List finalinputFileTypes = inputFileTypes; - matchingFiles = - outputFiles.stream() - .filter(file -> finalinputFileTypes.stream().anyMatch(file.getFilename()::endsWith)) - .collect(Collectors.toList()); - } + List matchingFiles; + if (inputFileTypes.contains("ALL")) { + matchingFiles = new ArrayList<>(outputFiles); + } else { + final List finalinputFileTypes = inputFileTypes; + matchingFiles = + outputFiles.stream() + .filter( + file -> + finalinputFileTypes.stream() + .anyMatch(file.getFilename()::endsWith)) + .collect(Collectors.toList()); + } // Check if there are matching files if (!matchingFiles.isEmpty()) { @@ -168,10 +171,7 @@ public class PipelineProcessor { // Handle the response if (response.getStatusCode().equals(HttpStatus.OK)) { - processOutputFiles( - operation, - response, - newOutputFiles); + processOutputFiles(operation, response, newOutputFiles); } else { // Log error if the response status is not OK logPrintStream.println( @@ -213,7 +213,7 @@ public class PipelineProcessor { // Make the request to the REST endpoint return restTemplate.exchange(url, HttpMethod.POST, entity, byte[].class); } - + public static String removeTrailingNaming(String filename) { // Splitting filename into name and extension int dotIndex = filename.lastIndexOf("."); @@ -236,9 +236,7 @@ public class PipelineProcessor { } private List processOutputFiles( - String operation, - ResponseEntity response, - List newOutputFiles) + String operation, ResponseEntity response, List newOutputFiles) throws IOException { // Define filename String newFilename; @@ -250,9 +248,9 @@ public class PipelineProcessor { newFilename = extractFilename(response); } else { // Otherwise, keep the original filename. - newFilename = removeTrailingNaming(extractFilename(response)); + newFilename = removeTrailingNaming(extractFilename(response)); } - + // Check if the response body is a zip file if (isZip(response.getBody())) { // Unzip the file and add all the files to the new output files diff --git a/src/main/java/stirling/software/SPDF/utils/FileToPdf.java b/src/main/java/stirling/software/SPDF/utils/FileToPdf.java index 885cb6e5..a57bfda4 100644 --- a/src/main/java/stirling/software/SPDF/utils/FileToPdf.java +++ b/src/main/java/stirling/software/SPDF/utils/FileToPdf.java @@ -63,7 +63,13 @@ public class FileToPdf { } pdfBytes = Files.readAllBytes(tempOutputFile); + } catch (IOException e) { + pdfBytes = Files.readAllBytes(tempOutputFile); + if (pdfBytes.length < 1) { + throw e; + } } finally { + // Clean up temporary files Files.delete(tempOutputFile); Files.delete(tempInputFile);