1
0
mirror of https://github.com/Stirling-Tools/Stirling-PDF.git synced 2024-11-04 23:10:11 +01:00
This commit is contained in:
Anthony Stirling 2023-12-31 13:34:35 +00:00
parent d83bd1ae94
commit 39045df785
5 changed files with 201 additions and 194 deletions

View File

@ -102,8 +102,9 @@ public class SecurityConfiguration {
|| trimmedUri.startsWith("/images/") || trimmedUri.startsWith("/images/")
|| trimmedUri.startsWith("/public/") || trimmedUri.startsWith("/public/")
|| trimmedUri.startsWith("/css/") || trimmedUri.startsWith("/css/")
|| trimmedUri.startsWith("/js/") || || trimmedUri.startsWith("/js/")
trimmedUri.startsWith("/api/v1/info/status"); || trimmedUri.startsWith(
"/api/v1/info/status");
}) })
.permitAll() .permitAll()
.anyRequest() .anyRequest()

View File

@ -49,19 +49,13 @@ public class PipelineProcessor {
@Autowired(required = false) @Autowired(required = false)
private UserServiceInterface userService; private UserServiceInterface userService;
@Autowired @Autowired private ServletContext servletContext;
private ServletContext servletContext;
private String getApiKeyForUser() { private String getApiKeyForUser() {
if (userService == null) if (userService == null) return "";
return "";
return userService.getApiKeyForUser(Role.INTERNAL_API_USER.getRoleId()); return userService.getApiKeyForUser(Role.INTERNAL_API_USER.getRoleId());
} }
private String getBaseUrl() { private String getBaseUrl() {
String contextPath = servletContext.getContextPath(); String contextPath = servletContext.getContextPath();
String port = SPdfApplication.getPort(); String port = SPdfApplication.getPort();
@ -69,9 +63,8 @@ public class PipelineProcessor {
return "http://localhost:" + port + contextPath + "/"; return "http://localhost:" + port + contextPath + "/";
} }
List<Resource> runPipelineAgainstFiles(List<Resource> outputFiles, PipelineConfig config)
throws Exception {
List<Resource> runPipelineAgainstFiles(List<Resource> outputFiles, PipelineConfig config) throws Exception {
ByteArrayOutputStream logStream = new ByteArrayOutputStream(); ByteArrayOutputStream logStream = new ByteArrayOutputStream();
PrintStream logPrintStream = new PrintStream(logStream); PrintStream logPrintStream = new PrintStream(logStream);
@ -82,16 +75,19 @@ public class PipelineProcessor {
String operation = pipelineOperation.getOperation(); String operation = pipelineOperation.getOperation();
boolean isMultiInputOperation = apiDocService.isMultiInput(operation); boolean isMultiInputOperation = apiDocService.isMultiInput(operation);
logger.info("Running operation: {} isMultiInputOperation {}", operation, isMultiInputOperation); logger.info(
"Running operation: {} isMultiInputOperation {}",
operation,
isMultiInputOperation);
Map<String, Object> parameters = pipelineOperation.getParameters(); Map<String, Object> parameters = pipelineOperation.getParameters();
String inputFileExtension = ""; String inputFileExtension = "";
//TODO // TODO
//if (operationNode.has("inputFileType")) { // if (operationNode.has("inputFileType")) {
// inputFileExtension = operationNode.get("inputFileType").asText(); // inputFileExtension = operationNode.get("inputFileType").asText();
//} else { // } else {
inputFileExtension = ".pdf"; inputFileExtension = ".pdf";
//} // }
final String finalInputFileExtension = inputFileExtension; final String finalInputFileExtension = inputFileExtension;
String url = getBaseUrl() + operation; String url = getBaseUrl() + operation;
@ -105,14 +101,14 @@ public class PipelineProcessor {
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 this // If the operation is filter and the response body is null or empty, skip
// 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)) {
@ -126,22 +122,26 @@ public class PipelineProcessor {
continue; continue;
} }
processOutputFiles(operation, file.getFilename(), response, newOutputFiles); processOutputFiles(operation, file.getFilename(), response, newOutputFiles);
} }
if (!hasInputFileType) { if (!hasInputFileType) {
logPrintStream.println( logPrintStream.println(
"No files with extension " + inputFileExtension + " found for operation " + operation); "No files with extension "
+ inputFileExtension
+ " found for operation "
+ operation);
hasErrors = true; hasErrors = true;
} }
} }
} else { } else {
// Filter and collect all files that match the inputFileExtension // Filter and collect all files that match the inputFileExtension
List<Resource> matchingFiles = outputFiles.stream() List<Resource> matchingFiles =
.filter(file -> file.getFilename().endsWith(finalInputFileExtension)) outputFiles.stream()
.filter(
file ->
file.getFilename()
.endsWith(finalInputFileExtension))
.collect(Collectors.toList()); .collect(Collectors.toList());
// Check if there are matching files // Check if there are matching files
@ -154,7 +154,7 @@ public class PipelineProcessor {
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());
} }
@ -162,20 +162,28 @@ public class PipelineProcessor {
// Handle the response // Handle the response
if (response.getStatusCode().equals(HttpStatus.OK)) { if (response.getStatusCode().equals(HttpStatus.OK)) {
processOutputFiles(operation, matchingFiles.get(0).getFilename(), response, newOutputFiles); processOutputFiles(
operation,
matchingFiles.get(0).getFilename(),
response,
newOutputFiles);
} else { } else {
// Log error if the response status is not OK // Log error if the response status is not OK
logPrintStream.println("Error in multi-input operation: " + response.getBody()); logPrintStream.println(
"Error in multi-input operation: " + response.getBody());
hasErrors = true; hasErrors = true;
} }
} else { } else {
logPrintStream.println("No files with extension " + inputFileExtension + " found for multi-input operation " + operation); logPrintStream.println(
"No files with extension "
+ inputFileExtension
+ " found for multi-input operation "
+ operation);
hasErrors = true; hasErrors = true;
} }
} }
logPrintStream.close(); logPrintStream.close();
outputFiles = newOutputFiles; outputFiles = newOutputFiles;
} }
if (hasErrors) { if (hasErrors) {
logger.error("Errors occurred during processing. Log: {}", logStream.toString()); logger.error("Errors occurred during processing. Log: {}", logStream.toString());
@ -184,7 +192,7 @@ public class PipelineProcessor {
return outputFiles; return outputFiles;
} }
private ResponseEntity<byte[]> sendWebRequest(String url, MultiValueMap<String, Object> body ){ private ResponseEntity<byte[]> sendWebRequest(String url, MultiValueMap<String, Object> body) {
RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();
// Set up headers, including API key // Set up headers, including API key

View File

@ -33,7 +33,6 @@ import io.swagger.v3.oas.annotations.tags.Tag;
@Tag(name = "General", description = "General APIs") @Tag(name = "General", description = "General APIs")
public class GeneralWebController { public class GeneralWebController {
@GetMapping("/pipeline") @GetMapping("/pipeline")
@Hidden @Hidden
public String pipelineForm(Model model) { public String pipelineForm(Model model) {
@ -42,10 +41,10 @@ public class GeneralWebController {
List<String> pipelineConfigs = new ArrayList<>(); List<String> pipelineConfigs = new ArrayList<>();
List<Map<String, String>> pipelineConfigsWithNames = new ArrayList<>(); List<Map<String, String>> pipelineConfigsWithNames = new ArrayList<>();
if(new File("./pipeline/defaultWebUIConfigs/").exists()) { if (new File("./pipeline/defaultWebUIConfigs/").exists()) {
try (Stream<Path> paths = Files.walk(Paths.get("./pipeline/defaultWebUIConfigs/"))) { try (Stream<Path> paths = Files.walk(Paths.get("./pipeline/defaultWebUIConfigs/"))) {
List<Path> jsonFiles = paths List<Path> jsonFiles =
.filter(Files::isRegularFile) paths.filter(Files::isRegularFile)
.filter(p -> p.toString().endsWith(".json")) .filter(p -> p.toString().endsWith(".json"))
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -55,11 +54,17 @@ public class GeneralWebController {
} }
for (String config : pipelineConfigs) { for (String config : pipelineConfigs) {
Map<String, Object> jsonContent = new ObjectMapper().readValue(config, new TypeReference<Map<String, Object>>(){}); Map<String, Object> jsonContent =
new ObjectMapper()
.readValue(config, new TypeReference<Map<String, Object>>() {});
String name = (String) jsonContent.get("name"); String name = (String) jsonContent.get("name");
if(name == null || name.length() < 1) { if (name == null || name.length() < 1) {
String filename = jsonFiles.get(pipelineConfigs.indexOf(config)).getFileName().toString(); String filename =
jsonFiles
.get(pipelineConfigs.indexOf(config))
.getFileName()
.toString();
name = filename.substring(0, filename.lastIndexOf('.')); name = filename.substring(0, filename.lastIndexOf('.'));
} }
Map<String, String> configWithName = new HashMap<>(); Map<String, String> configWithName = new HashMap<>();
@ -71,14 +76,11 @@ public class GeneralWebController {
pipelineConfigsWithNames.add(configWithName); pipelineConfigsWithNames.add(configWithName);
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
if(pipelineConfigsWithNames.size() == 0) { if (pipelineConfigsWithNames.size() == 0) {
Map<String, String> configWithName = new HashMap<>(); Map<String, String> configWithName = new HashMap<>();
configWithName.put("json", ""); configWithName.put("json", "");
configWithName.put("name", "No preloaded configs found"); configWithName.put("name", "No preloaded configs found");
@ -91,9 +93,6 @@ public class GeneralWebController {
return "pipeline"; return "pipeline";
} }
@GetMapping("/merge-pdfs") @GetMapping("/merge-pdfs")
@Hidden @Hidden
public String mergePdfForm(Model model) { public String mergePdfForm(Model model) {

View File

@ -11,6 +11,5 @@ public class RequestUriUtils {
|| requestURI.startsWith("/pdfjs/") || requestURI.startsWith("/pdfjs/")
|| requestURI.endsWith(".svg") || requestURI.endsWith(".svg")
|| requestURI.startsWith("/api/v1/info/status"); || requestURI.startsWith("/api/v1/info/status");
} }
} }