1
0
mirror of https://github.com/Stirling-Tools/Stirling-PDF.git synced 2024-09-29 16:10:11 +02:00
This commit is contained in:
Anthony Stirling 2023-12-29 13:30:28 +00:00
parent 5c6936b494
commit 168a0f001c
3 changed files with 72 additions and 12 deletions

View File

@ -64,6 +64,9 @@ public class PipelineController {
logger.info("Received POST request to /handleData with {} files", files.length); logger.info("Received POST request to /handleData with {} files", files.length);
try { try {
List<Resource> inputFiles = processor.generateInputFiles(files); List<Resource> inputFiles = processor.generateInputFiles(files);
if(inputFiles == null || inputFiles.size() == 0) {
return null;
}
List<Resource> outputFiles = processor.runPipelineAgainstFiles(inputFiles, config); List<Resource> outputFiles = processor.runPipelineAgainstFiles(inputFiles, config);
if (outputFiles != null && outputFiles.size() == 1) { if (outputFiles != null && outputFiles.size() == 1) {
// If there is only one file, return it directly // If there is only one file, return it directly

View File

@ -17,9 +17,11 @@ import java.util.Optional;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
@ -29,20 +31,52 @@ import stirling.software.SPDF.model.PipelineConfig;
import stirling.software.SPDF.model.PipelineOperation; import stirling.software.SPDF.model.PipelineOperation;
@Service @Service
public class DirectoryProcessor { public class PipelineDirectoryProcessor {
private Logger logger; private static final Logger logger = LoggerFactory.getLogger(PipelineDirectoryProcessor.class);
@Autowired @Autowired
private ObjectMapper objectMapper; private ObjectMapper objectMapper;
@Autowired
private ApiDocService apiDocService; private ApiDocService apiDocService;
@Autowired
private ApplicationProperties applicationProperties; private ApplicationProperties applicationProperties;
private String finishedFoldersDir;
final String watchedFoldersDir = "./pipeline/watchedFolders/";
final String finishedFoldersDir = "./pipeline/finishedFolders/";
@Autowired @Autowired
PipelineProcessor processor; PipelineProcessor processor;
// Constructor and other necessary initializations...
@Scheduled(fixedRate = 60000)
public void scanFolders() {
if (!Boolean.TRUE.equals(applicationProperties.getSystem().getEnableAlphaFunctionality())) {
return;
}
Path watchedFolderPath = Paths.get(watchedFoldersDir);
if (!Files.exists(watchedFolderPath)) {
try {
Files.createDirectories(watchedFolderPath);
logger.info("Created directory: {}", watchedFolderPath);
} catch (IOException e) {
logger.error("Error creating directory: {}", watchedFolderPath, e);
return;
}
}
try (Stream<Path> paths = Files.walk(watchedFolderPath)) {
paths.filter(Files::isDirectory).forEach(t -> {
try {
if (!t.equals(watchedFolderPath) && !t.endsWith("processing")) {
handleDirectory(t);
}
} catch (Exception e) {
logger.error("Error handling directory: {}", t, e);
}
});
} catch (Exception e) {
logger.error("Error walking through directory: {}", watchedFolderPath, e);
}
}
public void handleDirectory(Path dir) throws IOException { public void handleDirectory(Path dir) throws IOException {
logger.info("Handling directory: {}", dir); logger.info("Handling directory: {}", dir);
Path processingDir = createProcessingDirectory(dir); Path processingDir = createProcessingDirectory(dir);
@ -110,22 +144,47 @@ public class DirectoryProcessor {
private List<File> prepareFilesForProcessing(File[] files, Path processingDir) throws IOException { private List<File> prepareFilesForProcessing(File[] files, Path processingDir) throws IOException {
List<File> filesToProcess = new ArrayList<>(); List<File> filesToProcess = new ArrayList<>();
for (File file : files) { for (File file : files) {
Path targetPath = processingDir.resolve(file.getName()); Path targetPath = resolveUniqueFilePath(processingDir, file.getName());
Files.move(file.toPath(), targetPath); Files.move(file.toPath(), targetPath);
filesToProcess.add(targetPath.toFile()); filesToProcess.add(targetPath.toFile());
} }
return filesToProcess; return filesToProcess;
} }
private Path resolveUniqueFilePath(Path directory, String originalFileName) {
Path filePath = directory.resolve(originalFileName);
int counter = 1;
while (Files.exists(filePath)) {
String newName = appendSuffixToFileName(originalFileName, "(" + counter + ")");
filePath = directory.resolve(newName);
counter++;
}
return filePath;
}
private String appendSuffixToFileName(String originalFileName, String suffix) {
int dotIndex = originalFileName.lastIndexOf('.');
if (dotIndex == -1) {
return originalFileName + suffix;
} else {
return originalFileName.substring(0, dotIndex) + suffix + originalFileName.substring(dotIndex);
}
}
private void runPipelineAgainstFiles(List<File> filesToProcess, PipelineConfig config, Path dir, Path processingDir) throws IOException { private void runPipelineAgainstFiles(List<File> filesToProcess, PipelineConfig config, Path dir, Path processingDir) throws IOException {
try { try {
List<Resource> inputFiles = processor.generateInputFiles(filesToProcess.toArray(new File[0])); List<Resource> inputFiles = processor.generateInputFiles(filesToProcess.toArray(new File[0]));
if(inputFiles == null || inputFiles.size() == 0) {
return;
}
List<Resource> outputFiles = processor.runPipelineAgainstFiles(inputFiles, config); List<Resource> outputFiles = processor.runPipelineAgainstFiles(inputFiles, config);
if (outputFiles == null) return; if (outputFiles == null) return;
moveAndRenameFiles(outputFiles, config, dir); moveAndRenameFiles(outputFiles, config, dir);
deleteOriginalFiles(filesToProcess, processingDir); deleteOriginalFiles(filesToProcess, processingDir);
} catch (Exception e) { } catch (Exception e) {
logger.error("error during processing", e);
moveFilesBack(filesToProcess, processingDir); moveFilesBack(filesToProcess, processingDir);
} }
} }
@ -166,7 +225,7 @@ public class DirectoryProcessor {
private Path determineOutputPath(PipelineConfig config, Path dir) { private Path determineOutputPath(PipelineConfig config, Path dir) {
String outputDir = config.getOutputDir() String outputDir = config.getOutputDir()
.replace("{outputFolder}", applicationProperties.getAutoPipeline().getOutputFolder()) .replace("{outputFolder}", finishedFoldersDir)
.replace("{folderName}", dir.toString()) .replace("{folderName}", dir.toString())
.replaceAll("\\\\?watchedFolders", ""); .replaceAll("\\\\?watchedFolders", "");
@ -184,7 +243,7 @@ public class DirectoryProcessor {
for (File file : filesToProcess) { for (File file : filesToProcess) {
try { try {
Files.move(processingDir.resolve(file.getName()), file.toPath()); Files.move(processingDir.resolve(file.getName()), file.toPath());
logger.info("Moved file back to original location: {}", file.getName()); logger.info("Moved file back to original location: {} , {}",file.toPath(), file.getName());
} catch (IOException e) { } catch (IOException e) {
logger.error("Error moving file back to original location: {}", file.getName(), e); logger.error("Error moving file back to original location: {}", file.getName(), e);
} }

View File

@ -43,8 +43,6 @@ public class PipelineProcessor {
private static final Logger logger = LoggerFactory.getLogger(PipelineProcessor.class); private static final Logger logger = LoggerFactory.getLogger(PipelineProcessor.class);
final String watchedFoldersDir = "./pipeline/watchedFolders/";
final String finishedFoldersDir = "./pipeline/finishedFolders/";
@Autowired @Autowired
private ApiDocService apiDocService; private ApiDocService apiDocService;