mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2024-11-16 12:20:12 +01:00
Custom uid (#883)
* init * user and pass to just pass lang update * session management fixes and avoid demo user locking * fix for UMASK and extract cleanups
This commit is contained in:
parent
9246b42057
commit
08205ed32d
@ -15,7 +15,10 @@ ARG VERSION_TAG
|
|||||||
ENV DOCKER_ENABLE_SECURITY=false \
|
ENV DOCKER_ENABLE_SECURITY=false \
|
||||||
VERSION_TAG=$VERSION_TAG \
|
VERSION_TAG=$VERSION_TAG \
|
||||||
JAVA_TOOL_OPTIONS="$JAVA_TOOL_OPTIONS -XX:MaxRAMPercentage=75" \
|
JAVA_TOOL_OPTIONS="$JAVA_TOOL_OPTIONS -XX:MaxRAMPercentage=75" \
|
||||||
HOME=/home/stirlingpdfuser
|
HOME=/home/stirlingpdfuser \
|
||||||
|
PUID=1000 \
|
||||||
|
PGID=1000 \
|
||||||
|
UMASK=022
|
||||||
|
|
||||||
|
|
||||||
# JDK for app
|
# JDK for app
|
||||||
|
@ -7,7 +7,10 @@ ARG VERSION_TAG
|
|||||||
ENV DOCKER_ENABLE_SECURITY=false \
|
ENV DOCKER_ENABLE_SECURITY=false \
|
||||||
HOME=/home/stirlingpdfuser \
|
HOME=/home/stirlingpdfuser \
|
||||||
VERSION_TAG=$VERSION_TAG \
|
VERSION_TAG=$VERSION_TAG \
|
||||||
JAVA_TOOL_OPTIONS="$JAVA_TOOL_OPTIONS -XX:MaxRAMPercentage=75"
|
JAVA_TOOL_OPTIONS="$JAVA_TOOL_OPTIONS -XX:MaxRAMPercentage=75" \
|
||||||
|
PUID=1000 \
|
||||||
|
PGID=1000 \
|
||||||
|
UMASK=022
|
||||||
|
|
||||||
# Copy necessary files
|
# Copy necessary files
|
||||||
COPY scripts/download-security-jar.sh /scripts/download-security-jar.sh
|
COPY scripts/download-security-jar.sh /scripts/download-security-jar.sh
|
||||||
|
@ -7,10 +7,10 @@ ARG VERSION_TAG
|
|||||||
ENV DOCKER_ENABLE_SECURITY=false \
|
ENV DOCKER_ENABLE_SECURITY=false \
|
||||||
HOME=/home/stirlingpdfuser \
|
HOME=/home/stirlingpdfuser \
|
||||||
VERSION_TAG=$VERSION_TAG \
|
VERSION_TAG=$VERSION_TAG \
|
||||||
JAVA_TOOL_OPTIONS="$JAVA_TOOL_OPTIONS -XX:MaxRAMPercentage=75"
|
JAVA_TOOL_OPTIONS="$JAVA_TOOL_OPTIONS -XX:MaxRAMPercentage=75" \
|
||||||
# PUID=1000 \
|
PUID=1000 \
|
||||||
# PGID=1000 \
|
PGID=1000 \
|
||||||
# UMASK=022 \
|
UMASK=022
|
||||||
|
|
||||||
# Copy necessary files
|
# Copy necessary files
|
||||||
COPY scripts/download-security-jar.sh /scripts/download-security-jar.sh
|
COPY scripts/download-security-jar.sh /scripts/download-security-jar.sh
|
||||||
|
@ -1,5 +1,15 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
|
# Update the user and group IDs as per environment variables
|
||||||
|
if [ ! -z "$PUID" ] && [ "$PUID" != "$(id -u stirlingpdfuser)" ]; then
|
||||||
|
usermod -o -u "$PUID" stirlingpdfuser
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ! -z "$PGID" ] && [ "$PGID" != "$(id -g stirlingpdfgroup)" ]; then
|
||||||
|
groupmod -o -g "$PGID" stirlingpdfgroup
|
||||||
|
fi
|
||||||
|
umask "$UMASK"
|
||||||
|
|
||||||
echo "Setting permissions and ownership for necessary directories..."
|
echo "Setting permissions and ownership for necessary directories..."
|
||||||
chown -R stirlingpdfuser:stirlingpdfgroup /logs /scripts /usr/share/fonts/opentype/noto /usr/share/tessdata /configs /customFiles
|
chown -R stirlingpdfuser:stirlingpdfgroup /logs /scripts /usr/share/fonts/opentype/noto /usr/share/tessdata /configs /customFiles
|
||||||
chmod -R 755 /logs /scripts /usr/share/fonts/opentype/noto /usr/share/tessdata /configs /customFiles
|
chmod -R 755 /logs /scripts /usr/share/fonts/opentype/noto /usr/share/tessdata /configs /customFiles
|
||||||
|
@ -13,6 +13,17 @@ if [ -d /usr/share/tesseract-ocr/5/tessdata ]; then
|
|||||||
cp -r /usr/share/tesseract-ocr/5/tessdata/* /usr/share/tessdata || true;
|
cp -r /usr/share/tesseract-ocr/5/tessdata/* /usr/share/tessdata || true;
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
# Update the user and group IDs as per environment variables
|
||||||
|
if [ ! -z "$PUID" ] && [ "$PUID" != "$(id -u stirlingpdfuser)" ]; then
|
||||||
|
usermod -o -u "$PUID" stirlingpdfuser
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ! -z "$PGID" ] && [ "$PGID" != "$(id -g stirlingpdfgroup)" ]; then
|
||||||
|
groupmod -o -g "$PGID" stirlingpdfgroup
|
||||||
|
fi
|
||||||
|
umask "$UMASK"
|
||||||
|
|
||||||
echo "Setting permissions and ownership for necessary directories..."
|
echo "Setting permissions and ownership for necessary directories..."
|
||||||
chown -R stirlingpdfuser:stirlingpdfgroup /logs /scripts /usr/share/fonts/opentype/noto /usr/share/tessdata /configs /customFiles
|
chown -R stirlingpdfuser:stirlingpdfgroup /logs /scripts /usr/share/fonts/opentype/noto /usr/share/tessdata /configs /customFiles
|
||||||
chmod -R 755 /logs /scripts /usr/share/fonts/opentype/noto /usr/share/tessdata /configs /customFiles
|
chmod -R 755 /logs /scripts /usr/share/fonts/opentype/noto /usr/share/tessdata /configs /customFiles
|
||||||
|
@ -60,5 +60,6 @@ public class CustomAuthenticationFailureHandler extends SimpleUrlAuthenticationF
|
|||||||
return user.isPresent()
|
return user.isPresent()
|
||||||
&& user.get().getAuthorities().stream()
|
&& user.get().getAuthorities().stream()
|
||||||
.anyMatch(authority -> "ROLE_DEMO_USER".equals(authority.getAuthority()));
|
.anyMatch(authority -> "ROLE_DEMO_USER".equals(authority.getAuthority()));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,111 +73,145 @@ public class ExtractImageScansController {
|
|||||||
|
|
||||||
List<String> images = new ArrayList<>();
|
List<String> images = new ArrayList<>();
|
||||||
|
|
||||||
// Check if input file is a PDF
|
List<Path> tempImageFiles = new ArrayList<>();
|
||||||
if ("pdf".equalsIgnoreCase(extension)) {
|
Path tempInputFile = null;
|
||||||
// Load PDF document
|
Path tempZipFile = null;
|
||||||
try (PDDocument document = Loader.loadPDF(form.getFileInput().getBytes())) {
|
List<Path> tempDirs = new ArrayList<>();
|
||||||
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
|
||||||
int pageCount = document.getNumberOfPages();
|
|
||||||
images = new ArrayList<>();
|
|
||||||
|
|
||||||
// Create images of all pages
|
try {
|
||||||
for (int i = 0; i < pageCount; i++) {
|
// Check if input file is a PDF
|
||||||
// Create temp file to save the image
|
if ("pdf".equalsIgnoreCase(extension)) {
|
||||||
Path tempFile = Files.createTempFile("image_", ".png");
|
// Load PDF document
|
||||||
|
try (PDDocument document = Loader.loadPDF(form.getFileInput().getBytes())) {
|
||||||
|
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
||||||
|
int pageCount = document.getNumberOfPages();
|
||||||
|
images = new ArrayList<>();
|
||||||
|
|
||||||
// Render image and save as temp file
|
// Create images of all pages
|
||||||
BufferedImage image = pdfRenderer.renderImageWithDPI(i, 300);
|
for (int i = 0; i < pageCount; i++) {
|
||||||
ImageIO.write(image, "png", tempFile.toFile());
|
// Create temp file to save the image
|
||||||
|
Path tempFile = Files.createTempFile("image_", ".png");
|
||||||
|
|
||||||
// Add temp file path to images list
|
// Render image and save as temp file
|
||||||
images.add(tempFile.toString());
|
BufferedImage image = pdfRenderer.renderImageWithDPI(i, 300);
|
||||||
|
ImageIO.write(image, "png", tempFile.toFile());
|
||||||
|
|
||||||
|
// Add temp file path to images list
|
||||||
|
images.add(tempFile.toString());
|
||||||
|
tempImageFiles.add(tempFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
tempInputFile = Files.createTempFile("input_", "." + extension);
|
||||||
|
Files.copy(
|
||||||
|
form.getFileInput().getInputStream(),
|
||||||
|
tempInputFile,
|
||||||
|
StandardCopyOption.REPLACE_EXISTING);
|
||||||
|
// Add input file path to images list
|
||||||
|
images.add(tempInputFile.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
List<byte[]> processedImageBytes = new ArrayList<>();
|
||||||
|
|
||||||
|
// Process each image
|
||||||
|
for (int i = 0; i < images.size(); i++) {
|
||||||
|
|
||||||
|
Path tempDir = Files.createTempDirectory("openCV_output");
|
||||||
|
tempDirs.add(tempDir);
|
||||||
|
List<String> command =
|
||||||
|
new ArrayList<>(
|
||||||
|
Arrays.asList(
|
||||||
|
"python3",
|
||||||
|
"./scripts/split_photos.py",
|
||||||
|
images.get(i),
|
||||||
|
tempDir.toString(),
|
||||||
|
"--angle_threshold",
|
||||||
|
String.valueOf(form.getAngleThreshold()),
|
||||||
|
"--tolerance",
|
||||||
|
String.valueOf(form.getTolerance()),
|
||||||
|
"--min_area",
|
||||||
|
String.valueOf(form.getMinArea()),
|
||||||
|
"--min_contour_area",
|
||||||
|
String.valueOf(form.getMinContourArea()),
|
||||||
|
"--border_size",
|
||||||
|
String.valueOf(form.getBorderSize())));
|
||||||
|
|
||||||
|
// Run CLI command
|
||||||
|
ProcessExecutorResult returnCode =
|
||||||
|
ProcessExecutor.getInstance(ProcessExecutor.Processes.PYTHON_OPENCV)
|
||||||
|
.runCommandWithOutputHandling(command);
|
||||||
|
|
||||||
|
// Read the output photos in temp directory
|
||||||
|
List<Path> tempOutputFiles = Files.list(tempDir).sorted().collect(Collectors.toList());
|
||||||
|
for (Path tempOutputFile : tempOutputFiles) {
|
||||||
|
byte[] imageBytes = Files.readAllBytes(tempOutputFile);
|
||||||
|
processedImageBytes.add(imageBytes);
|
||||||
|
}
|
||||||
|
// Clean up the temporary directory
|
||||||
|
FileUtils.deleteDirectory(tempDir.toFile());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create zip file if multiple images
|
||||||
|
if (processedImageBytes.size() > 1) {
|
||||||
|
String outputZipFilename = fileName.replaceFirst("[.][^.]+$", "") + "_processed.zip";
|
||||||
|
tempZipFile = Files.createTempFile("output_", ".zip");
|
||||||
|
|
||||||
|
try (ZipOutputStream zipOut =
|
||||||
|
new ZipOutputStream(new FileOutputStream(tempZipFile.toFile()))) {
|
||||||
|
// Add processed images to the zip
|
||||||
|
for (int i = 0; i < processedImageBytes.size(); i++) {
|
||||||
|
ZipEntry entry =
|
||||||
|
new ZipEntry(
|
||||||
|
fileName.replaceFirst("[.][^.]+$", "")
|
||||||
|
+ "_"
|
||||||
|
+ (i + 1)
|
||||||
|
+ ".png");
|
||||||
|
zipOut.putNextEntry(entry);
|
||||||
|
zipOut.write(processedImageBytes.get(i));
|
||||||
|
zipOut.closeEntry();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] zipBytes = Files.readAllBytes(tempZipFile);
|
||||||
|
|
||||||
|
// Clean up the temporary zip file
|
||||||
|
Files.delete(tempZipFile);
|
||||||
|
|
||||||
|
return WebResponseUtils.bytesToWebResponse(
|
||||||
|
zipBytes, outputZipFilename, MediaType.APPLICATION_OCTET_STREAM);
|
||||||
|
} else {
|
||||||
|
// Return the processed image as a response
|
||||||
|
byte[] imageBytes = processedImageBytes.get(0);
|
||||||
|
return WebResponseUtils.bytesToWebResponse(
|
||||||
|
imageBytes,
|
||||||
|
fileName.replaceFirst("[.][^.]+$", "") + ".png",
|
||||||
|
MediaType.IMAGE_PNG);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
// Cleanup logic for all temporary files and directories
|
||||||
|
tempImageFiles.forEach(path -> {
|
||||||
|
try {
|
||||||
|
Files.deleteIfExists(path);
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.error("Failed to delete temporary image file: " + path, e);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
} else {
|
|
||||||
Path tempInputFile = Files.createTempFile("input_", "." + extension);
|
|
||||||
Files.copy(
|
|
||||||
form.getFileInput().getInputStream(),
|
|
||||||
tempInputFile,
|
|
||||||
StandardCopyOption.REPLACE_EXISTING);
|
|
||||||
// Add input file path to images list
|
|
||||||
images.add(tempInputFile.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
List<byte[]> processedImageBytes = new ArrayList<>();
|
if (tempZipFile != null && Files.exists(tempZipFile)) {
|
||||||
|
try {
|
||||||
// Process each image
|
Files.delete(tempZipFile);
|
||||||
for (int i = 0; i < images.size(); i++) {
|
} catch (IOException e) {
|
||||||
|
logger.error("Failed to delete temporary zip file: " + tempZipFile, e);
|
||||||
Path tempDir = Files.createTempDirectory("openCV_output");
|
|
||||||
List<String> command =
|
|
||||||
new ArrayList<>(
|
|
||||||
Arrays.asList(
|
|
||||||
"python3",
|
|
||||||
"./scripts/split_photos.py",
|
|
||||||
images.get(i),
|
|
||||||
tempDir.toString(),
|
|
||||||
"--angle_threshold",
|
|
||||||
String.valueOf(form.getAngleThreshold()),
|
|
||||||
"--tolerance",
|
|
||||||
String.valueOf(form.getTolerance()),
|
|
||||||
"--min_area",
|
|
||||||
String.valueOf(form.getMinArea()),
|
|
||||||
"--min_contour_area",
|
|
||||||
String.valueOf(form.getMinContourArea()),
|
|
||||||
"--border_size",
|
|
||||||
String.valueOf(form.getBorderSize())));
|
|
||||||
|
|
||||||
// Run CLI command
|
|
||||||
ProcessExecutorResult returnCode =
|
|
||||||
ProcessExecutor.getInstance(ProcessExecutor.Processes.PYTHON_OPENCV)
|
|
||||||
.runCommandWithOutputHandling(command);
|
|
||||||
|
|
||||||
// Read the output photos in temp directory
|
|
||||||
List<Path> tempOutputFiles = Files.list(tempDir).sorted().collect(Collectors.toList());
|
|
||||||
for (Path tempOutputFile : tempOutputFiles) {
|
|
||||||
byte[] imageBytes = Files.readAllBytes(tempOutputFile);
|
|
||||||
processedImageBytes.add(imageBytes);
|
|
||||||
}
|
|
||||||
// Clean up the temporary directory
|
|
||||||
FileUtils.deleteDirectory(tempDir.toFile());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create zip file if multiple images
|
|
||||||
if (processedImageBytes.size() > 1) {
|
|
||||||
String outputZipFilename = fileName.replaceFirst("[.][^.]+$", "") + "_processed.zip";
|
|
||||||
Path tempZipFile = Files.createTempFile("output_", ".zip");
|
|
||||||
|
|
||||||
try (ZipOutputStream zipOut =
|
|
||||||
new ZipOutputStream(new FileOutputStream(tempZipFile.toFile()))) {
|
|
||||||
// Add processed images to the zip
|
|
||||||
for (int i = 0; i < processedImageBytes.size(); i++) {
|
|
||||||
ZipEntry entry =
|
|
||||||
new ZipEntry(
|
|
||||||
fileName.replaceFirst("[.][^.]+$", "")
|
|
||||||
+ "_"
|
|
||||||
+ (i + 1)
|
|
||||||
+ ".png");
|
|
||||||
zipOut.putNextEntry(entry);
|
|
||||||
zipOut.write(processedImageBytes.get(i));
|
|
||||||
zipOut.closeEntry();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] zipBytes = Files.readAllBytes(tempZipFile);
|
tempDirs.forEach(dir -> {
|
||||||
|
try {
|
||||||
// Clean up the temporary zip file
|
FileUtils.deleteDirectory(dir.toFile());
|
||||||
Files.delete(tempZipFile);
|
} catch (IOException e) {
|
||||||
|
logger.error("Failed to delete temporary directory: " + dir, e);
|
||||||
return WebResponseUtils.bytesToWebResponse(
|
}
|
||||||
zipBytes, outputZipFilename, MediaType.APPLICATION_OCTET_STREAM);
|
});
|
||||||
} else {
|
|
||||||
// Return the processed image as a response
|
|
||||||
byte[] imageBytes = processedImageBytes.get(0);
|
|
||||||
return WebResponseUtils.bytesToWebResponse(
|
|
||||||
imageBytes,
|
|
||||||
fileName.replaceFirst("[.][^.]+$", "") + ".png",
|
|
||||||
MediaType.IMAGE_PNG);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user