mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2024-11-16 12:20:12 +01:00
Resolve wkhtml and formatting
This commit is contained in:
parent
2fa68be36b
commit
75cf3ed0c1
@ -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<String, List<String>> 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.
|
||||
}
|
||||
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) {
|
||||
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;
|
||||
|
@ -84,12 +84,10 @@ public class PipelineProcessor {
|
||||
isMultiInputOperation);
|
||||
Map<String, Object> parameters = pipelineOperation.getParameters();
|
||||
List<String> inputFileTypes = apiDocService.getExtensionTypes(false, operation);
|
||||
if(inputFileTypes == null) {
|
||||
inputFileTypes = new ArrayList<String>(Arrays.asList("ALL"));
|
||||
if (inputFileTypes == null) {
|
||||
inputFileTypes = new ArrayList<String>(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<String, Object> body = new LinkedMultiValueMap<>();
|
||||
body.add("fileInput", file);
|
||||
if (extension.equals("ALL") || file.getFilename().endsWith(extension)) {
|
||||
hasInputFileType = true;
|
||||
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
||||
body.add("fileInput", file);
|
||||
|
||||
for (Entry<String, Object> entry : parameters.entrySet()) {
|
||||
body.add(entry.getKey(), entry.getValue());
|
||||
}
|
||||
for (Entry<String, Object> entry : parameters.entrySet()) {
|
||||
body.add(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
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
|
||||
// this
|
||||
// file
|
||||
if (operation.startsWith("filter-")
|
||||
&& (response.getBody() == null || response.getBody().length == 0)) {
|
||||
logger.info("Skipping file due to failing {}", operation);
|
||||
continue;
|
||||
}
|
||||
// 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 (!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<Resource> matchingFiles;
|
||||
if (inputFileTypes.contains("ALL")) {
|
||||
matchingFiles = new ArrayList<>(outputFiles);
|
||||
} else {
|
||||
final List<String> finalinputFileTypes = inputFileTypes;
|
||||
matchingFiles =
|
||||
outputFiles.stream()
|
||||
.filter(file -> finalinputFileTypes.stream().anyMatch(file.getFilename()::endsWith))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
List<Resource> matchingFiles;
|
||||
if (inputFileTypes.contains("ALL")) {
|
||||
matchingFiles = new ArrayList<>(outputFiles);
|
||||
} else {
|
||||
final List<String> 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(
|
||||
@ -236,9 +236,7 @@ public class PipelineProcessor {
|
||||
}
|
||||
|
||||
private List<Resource> processOutputFiles(
|
||||
String operation,
|
||||
ResponseEntity<byte[]> response,
|
||||
List<Resource> newOutputFiles)
|
||||
String operation, ResponseEntity<byte[]> response, List<Resource> newOutputFiles)
|
||||
throws IOException {
|
||||
// Define filename
|
||||
String newFilename;
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user