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.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,25 +40,37 @@ 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",
|
||||||
outputToFileTypes.put("WORD", Arrays.asList("doc", "docx", "odt", "rtf"));
|
Arrays.asList(
|
||||||
outputToFileTypes.put("CSV", Arrays.asList("csv"));
|
"png", "jpg", "jpeg", "gif", "webp", "bmp", "tif", "tiff", "svg", "psd",
|
||||||
outputToFileTypes.put("JS", Arrays.asList("js", "jsx"));
|
"ai", "eps"));
|
||||||
outputToFileTypes.put("HTML", Arrays.asList("html", "htm", "xhtml"));
|
outputToFileTypes.put(
|
||||||
outputToFileTypes.put("JSON", Arrays.asList("json"));
|
"ZIP",
|
||||||
outputToFileTypes.put("TXT", Arrays.asList("txt", "text", "md", "markdown"));
|
Arrays.asList("zip", "rar", "7z", "tar", "gz", "bz2", "xz", "lz", "lzma", "z"));
|
||||||
outputToFileTypes.put("PPT", Arrays.asList("ppt", "pptx", "odp"));
|
outputToFileTypes.put("WORD", Arrays.asList("doc", "docx", "odt", "rtf"));
|
||||||
outputToFileTypes.put("XML", Arrays.asList("xml", "xsd", "xsl"));
|
outputToFileTypes.put("CSV", Arrays.asList("csv"));
|
||||||
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("JS", Arrays.asList("js", "jsx"));
|
||||||
}
|
outputToFileTypes.put("HTML", Arrays.asList("html", "htm", "xhtml"));
|
||||||
|
outputToFileTypes.put("JSON", Arrays.asList("json"));
|
||||||
if (apiDocsJsonRootNode == null || apiDocumentation.size() == 0) {
|
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();
|
loadApiDocumentation();
|
||||||
}
|
}
|
||||||
if (!apiDocumentation.containsKey(operationName)) {
|
if (!apiDocumentation.containsKey(operationName)) {
|
||||||
@ -67,16 +80,16 @@ 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+)");
|
||||||
}
|
}
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@ -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;
|
||||||
|
|
||||||
@ -98,33 +96,35 @@ public class PipelineProcessor {
|
|||||||
for (Resource file : outputFiles) {
|
for (Resource file : outputFiles) {
|
||||||
boolean hasInputFileType = false;
|
boolean hasInputFileType = false;
|
||||||
for (String extension : inputFileTypes) {
|
for (String extension : inputFileTypes) {
|
||||||
if (extension.equals("ALL") || file.getFilename().endsWith(extension)) {
|
if (extension.equals("ALL") || file.getFilename().endsWith(extension)) {
|
||||||
hasInputFileType = true;
|
hasInputFileType = true;
|
||||||
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
||||||
body.add("fileInput", file);
|
body.add("fileInput", file);
|
||||||
|
|
||||||
for (Entry<String, Object> entry : parameters.entrySet()) {
|
for (Entry<String, Object> entry : parameters.entrySet()) {
|
||||||
body.add(entry.getKey(), entry.getValue());
|
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
|
// If the operation is filter and the response body is null or empty,
|
||||||
// this
|
// skip
|
||||||
// file
|
// this
|
||||||
if (operation.startsWith("filter-")
|
// file
|
||||||
&& (response.getBody() == null || response.getBody().length == 0)) {
|
if (operation.startsWith("filter-")
|
||||||
logger.info("Skipping file due to failing {}", operation);
|
&& (response.getBody() == null
|
||||||
continue;
|
|| 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;
|
if (!response.getStatusCode().equals(HttpStatus.OK)) {
|
||||||
continue;
|
logPrintStream.println("Error: " + response.getBody());
|
||||||
}
|
hasErrors = true;
|
||||||
processOutputFiles(operation, response, newOutputFiles);
|
continue;
|
||||||
}
|
}
|
||||||
|
processOutputFiles(operation, response, newOutputFiles);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!hasInputFileType) {
|
if (!hasInputFileType) {
|
||||||
@ -139,16 +139,19 @@ public class PipelineProcessor {
|
|||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Filter and collect all files that match the inputFileExtension
|
// Filter and collect all files that match the inputFileExtension
|
||||||
List<Resource> matchingFiles;
|
List<Resource> matchingFiles;
|
||||||
if (inputFileTypes.contains("ALL")) {
|
if (inputFileTypes.contains("ALL")) {
|
||||||
matchingFiles = new ArrayList<>(outputFiles);
|
matchingFiles = new ArrayList<>(outputFiles);
|
||||||
} else {
|
} else {
|
||||||
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(
|
||||||
.collect(Collectors.toList());
|
file ->
|
||||||
}
|
finalinputFileTypes.stream()
|
||||||
|
.anyMatch(file.getFilename()::endsWith))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
// Check if there are matching files
|
// Check if there are matching files
|
||||||
if (!matchingFiles.isEmpty()) {
|
if (!matchingFiles.isEmpty()) {
|
||||||
@ -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(
|
||||||
@ -213,7 +213,7 @@ public class PipelineProcessor {
|
|||||||
// Make the request to the REST endpoint
|
// Make the request to the REST endpoint
|
||||||
return restTemplate.exchange(url, HttpMethod.POST, entity, byte[].class);
|
return restTemplate.exchange(url, HttpMethod.POST, entity, byte[].class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String removeTrailingNaming(String filename) {
|
public static String removeTrailingNaming(String filename) {
|
||||||
// Splitting filename into name and extension
|
// Splitting filename into name and extension
|
||||||
int dotIndex = filename.lastIndexOf(".");
|
int dotIndex = filename.lastIndexOf(".");
|
||||||
@ -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;
|
||||||
@ -250,9 +248,9 @@ public class PipelineProcessor {
|
|||||||
newFilename = extractFilename(response);
|
newFilename = extractFilename(response);
|
||||||
} else {
|
} else {
|
||||||
// Otherwise, keep the original filename.
|
// Otherwise, keep the original filename.
|
||||||
newFilename = removeTrailingNaming(extractFilename(response));
|
newFilename = removeTrailingNaming(extractFilename(response));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the response body is a zip file
|
// Check if the response body is a zip file
|
||||||
if (isZip(response.getBody())) {
|
if (isZip(response.getBody())) {
|
||||||
// Unzip the file and add all the files to the new output files
|
// Unzip the file and add all the files to the new output files
|
||||||
|
@ -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);
|
||||||
|
Loading…
Reference in New Issue
Block a user