mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-09-08 17:51:20 +02:00
back to GeneralUtils
This commit is contained in:
parent
ecb23299f4
commit
d1c34aadb7
@ -16,6 +16,8 @@ import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.core.io.support.ResourcePatternUtils;
|
||||
@ -442,6 +444,37 @@ public class GeneralUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts a file from classpath:/static/python to a temporary directory and returns the path.
|
||||
*/
|
||||
public static Path extractScript(String scriptName) throws IOException {
|
||||
// Validate input
|
||||
if (scriptName == null || scriptName.trim().isEmpty()) {
|
||||
throw new IllegalArgumentException("scriptName must not be null or empty");
|
||||
}
|
||||
if (scriptName.contains("..") || scriptName.contains("/")) {
|
||||
throw new IllegalArgumentException(
|
||||
"scriptName must not contain path traversal characters");
|
||||
}
|
||||
|
||||
List<String> validScripts = Arrays.asList("png_to_webp.py", "split_photos.py");
|
||||
|
||||
if (!validScripts.contains(scriptName)) {
|
||||
throw new IllegalArgumentException(
|
||||
"scriptName must be either 'png_to_webp.py' or 'split_photos.py'");
|
||||
}
|
||||
// 1. load the script from classpath
|
||||
ClassPathResource resource = new ClassPathResource("static/python/" + scriptName);
|
||||
try (InputStream is = resource.getInputStream()) {
|
||||
File scriptFile = Files.createTempFile(scriptName.split("\\.")[0], ".py").toFile();
|
||||
FileUtils.copyInputStreamToFile(is, scriptFile);
|
||||
return scriptFile.toPath();
|
||||
} catch (IOException e) {
|
||||
log.error("Failed to load image signature file");
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isVersionHigher(String currentVersion, String compareVersion) {
|
||||
if (currentVersion == null || compareVersion == null) {
|
||||
return false;
|
||||
|
@ -2,17 +2,12 @@ package stirling.software.common.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.time.Duration;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@ -251,62 +246,4 @@ public class TempFileManager {
|
||||
|
||||
return registry.registerDirectory(loTempDir);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts a Python script from classpath:/static/python to a registered temp directory
|
||||
* and returns the script's path.
|
||||
*
|
||||
* @param scriptName name of the script (png_to_webp.py or split_photos.py)
|
||||
* @return Path to the extracted script file
|
||||
* @throws IOException if I/O operations fail
|
||||
* @throws IllegalArgumentException if scriptName is invalid
|
||||
*/
|
||||
public Path extractScript(String scriptName) throws IOException {
|
||||
// Validate input
|
||||
if (scriptName == null || scriptName.trim().isEmpty()) {
|
||||
throw new IllegalArgumentException("scriptName must not be null or empty");
|
||||
}
|
||||
if (scriptName.contains("..") || scriptName.contains("/")) {
|
||||
throw new IllegalArgumentException(
|
||||
"scriptName must not contain path traversal characters");
|
||||
}
|
||||
|
||||
List<String> validScripts = Arrays.asList("png_to_webp.py", "split_photos.py");
|
||||
if (!validScripts.contains(scriptName)) {
|
||||
throw new IllegalArgumentException(
|
||||
"scriptName must be either 'png_to_webp.py' or 'split_photos.py'");
|
||||
}
|
||||
|
||||
// Load the resource from classpath
|
||||
ClassPathResource resource = new ClassPathResource("static/python/" + scriptName);
|
||||
|
||||
// Determine prefix and base directory for scripts
|
||||
ApplicationProperties.TempFileManagement tempFiles =
|
||||
applicationProperties.getSystem().getTempFileManagement();
|
||||
String baseDir = tempFiles.getBaseTmpDir();
|
||||
String prefix = tempFiles.getPrefix() + "-scripts";
|
||||
|
||||
// Create and register temp directory
|
||||
Path scriptDir;
|
||||
if (baseDir != null && !baseDir.isEmpty()) {
|
||||
Path custom = Path.of(baseDir);
|
||||
if (!Files.exists(custom)) {
|
||||
Files.createDirectories(custom);
|
||||
}
|
||||
scriptDir = Files.createTempDirectory(custom, prefix);
|
||||
} else {
|
||||
scriptDir = Files.createTempDirectory(prefix);
|
||||
}
|
||||
registry.registerDirectory(scriptDir);
|
||||
scriptDir.toFile().deleteOnExit();
|
||||
|
||||
// Copy script into the directory
|
||||
Path target = scriptDir.resolve(scriptName);
|
||||
try (InputStream in = resource.getInputStream()) {
|
||||
Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
|
||||
log.debug("Extracted script '{}' to {}", scriptName, target);
|
||||
return target;
|
||||
}
|
||||
}
|
||||
|
@ -119,7 +119,7 @@ public class ConvertImgPDFController {
|
||||
}
|
||||
|
||||
String pythonVersion = CheckProgramInstall.getAvailablePythonCommand();
|
||||
Path pngToWebpScript = tempFileManager.extractScript("png_to_webp.py");
|
||||
Path pngToWebpScript = GeneralUtils.extractScript("png_to_webp.py");
|
||||
|
||||
List<String> command = new ArrayList<>();
|
||||
command.add(pythonVersion);
|
||||
|
@ -34,6 +34,7 @@ import stirling.software.SPDF.model.api.misc.ExtractImageScansRequest;
|
||||
import stirling.software.common.service.CustomPDFDocumentFactory;
|
||||
import stirling.software.common.util.CheckProgramInstall;
|
||||
import stirling.software.common.util.ExceptionUtils;
|
||||
import stirling.software.common.util.GeneralUtils;
|
||||
import stirling.software.common.util.ProcessExecutor;
|
||||
import stirling.software.common.util.ProcessExecutor.ProcessExecutorResult;
|
||||
import stirling.software.common.util.TempFileManager;
|
||||
@ -80,7 +81,7 @@ public class ExtractImageScansController {
|
||||
}
|
||||
|
||||
String pythonVersion = CheckProgramInstall.getAvailablePythonCommand();
|
||||
Path splitPhotosScript = tempFileManager.extractScript("split_photos.py");
|
||||
Path splitPhotosScript = GeneralUtils.extractScript("split_photos.py");
|
||||
try {
|
||||
// Check if input file is a PDF
|
||||
if ("pdf".equalsIgnoreCase(extension)) {
|
||||
|
Loading…
Reference in New Issue
Block a user