deletion changes
This commit is contained in:
parent
6ffa80c386
commit
65f9438639
9 changed files with 198 additions and 201 deletions
8
.github/workflows/build.yml
vendored
8
.github/workflows/build.yml
vendored
|
@ -3,14 +3,8 @@ name: "Build repo"
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: ["main"]
|
branches: ["main"]
|
||||||
paths-ignore:
|
|
||||||
- ".github/**"
|
|
||||||
- "**/*.md"
|
|
||||||
pull_request:
|
pull_request:
|
||||||
branches: ["main"]
|
branches: ["main"]
|
||||||
paths-ignore:
|
|
||||||
- ".github/**"
|
|
||||||
- "**/*.md"
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
|
@ -36,7 +30,7 @@ jobs:
|
||||||
|
|
||||||
- uses: gradle/actions/setup-gradle@v3
|
- uses: gradle/actions/setup-gradle@v3
|
||||||
with:
|
with:
|
||||||
gradle-version: 7.6
|
gradle-version: 8.7
|
||||||
|
|
||||||
- name: Build with Gradle
|
- name: Build with Gradle
|
||||||
run: ./gradlew build --no-build-cache
|
run: ./gradlew build --no-build-cache
|
||||||
|
|
|
@ -3,7 +3,6 @@ package stirling.software.SPDF.controller.api.converters;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.StandardCopyOption;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -41,11 +40,12 @@ public class ConvertOfficeController {
|
||||||
// Save the uploaded file to a temporary location
|
// Save the uploaded file to a temporary location
|
||||||
Path tempInputFile =
|
Path tempInputFile =
|
||||||
Files.createTempFile("input_", "." + FilenameUtils.getExtension(originalFilename));
|
Files.createTempFile("input_", "." + FilenameUtils.getExtension(originalFilename));
|
||||||
Files.copy(inputFile.getInputStream(), tempInputFile, StandardCopyOption.REPLACE_EXISTING);
|
inputFile.transferTo(tempInputFile);
|
||||||
|
|
||||||
// Prepare the output file path
|
// Prepare the output file path
|
||||||
Path tempOutputFile = Files.createTempFile("output_", ".pdf");
|
Path tempOutputFile = Files.createTempFile("output_", ".pdf");
|
||||||
|
|
||||||
|
try {
|
||||||
// Run the LibreOffice command
|
// Run the LibreOffice command
|
||||||
List<String> command =
|
List<String> command =
|
||||||
new ArrayList<>(
|
new ArrayList<>(
|
||||||
|
@ -63,12 +63,12 @@ public class ConvertOfficeController {
|
||||||
|
|
||||||
// Read the converted PDF file
|
// Read the converted PDF file
|
||||||
byte[] pdfBytes = Files.readAllBytes(tempOutputFile);
|
byte[] pdfBytes = Files.readAllBytes(tempOutputFile);
|
||||||
|
|
||||||
// Clean up the temporary files
|
|
||||||
Files.delete(tempInputFile);
|
|
||||||
Files.delete(tempOutputFile);
|
|
||||||
|
|
||||||
return pdfBytes;
|
return pdfBytes;
|
||||||
|
} finally {
|
||||||
|
// Clean up the temporary files
|
||||||
|
if (tempInputFile != null) Files.deleteIfExists(tempInputFile);
|
||||||
|
Files.deleteIfExists(tempOutputFile);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isValidFileExtension(String fileExtension) {
|
private boolean isValidFileExtension(String fileExtension) {
|
||||||
|
|
|
@ -61,8 +61,8 @@ public class ConvertPDFToPDFA {
|
||||||
byte[] pdfBytes = Files.readAllBytes(tempOutputFile);
|
byte[] pdfBytes = Files.readAllBytes(tempOutputFile);
|
||||||
|
|
||||||
// Clean up the temporary files
|
// Clean up the temporary files
|
||||||
Files.delete(tempInputFile);
|
Files.deleteIfExists(tempInputFile);
|
||||||
Files.delete(tempOutputFile);
|
Files.deleteIfExists(tempOutputFile);
|
||||||
|
|
||||||
// Return the optimized PDF as a response
|
// Return the optimized PDF as a response
|
||||||
String outputFilename =
|
String outputFilename =
|
||||||
|
|
|
@ -136,10 +136,10 @@ public class CompressController {
|
||||||
// Increase optimization level for next iteration
|
// Increase optimization level for next iteration
|
||||||
optimizeLevel++;
|
optimizeLevel++;
|
||||||
if (autoMode && optimizeLevel > 4) {
|
if (autoMode && optimizeLevel > 4) {
|
||||||
System.out.println("Skipping level 5 due to bad results in auto mode");
|
logger.info("Skipping level 5 due to bad results in auto mode");
|
||||||
sizeMet = true;
|
sizeMet = true;
|
||||||
} else {
|
} else {
|
||||||
System.out.println(
|
logger.info(
|
||||||
"Increasing ghostscript optimisation level to " + optimizeLevel);
|
"Increasing ghostscript optimisation level to " + optimizeLevel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -230,10 +230,10 @@ public class CompressController {
|
||||||
if (currentSize > expectedOutputSize) {
|
if (currentSize > expectedOutputSize) {
|
||||||
// Log the current file size and scaleFactor
|
// Log the current file size and scaleFactor
|
||||||
|
|
||||||
System.out.println(
|
logger.info(
|
||||||
"Current file size: "
|
"Current file size: "
|
||||||
+ FileUtils.byteCountToDisplaySize(currentSize));
|
+ FileUtils.byteCountToDisplaySize(currentSize));
|
||||||
System.out.println("Current scale factor: " + scaleFactor);
|
logger.info("Current scale factor: " + scaleFactor);
|
||||||
|
|
||||||
// The file is still too large, reduce scaleFactor and try again
|
// The file is still too large, reduce scaleFactor and try again
|
||||||
scaleFactor *= 0.9f; // reduce scaleFactor by 10%
|
scaleFactor *= 0.9f; // reduce scaleFactor by 10%
|
||||||
|
@ -256,7 +256,6 @@ public class CompressController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the optimized PDF file
|
// Read the optimized PDF file
|
||||||
pdfBytes = Files.readAllBytes(tempOutputFile);
|
pdfBytes = Files.readAllBytes(tempOutputFile);
|
||||||
|
|
||||||
|
@ -269,17 +268,18 @@ public class CompressController {
|
||||||
// Read the original file again
|
// Read the original file again
|
||||||
pdfBytes = Files.readAllBytes(tempInputFile);
|
pdfBytes = Files.readAllBytes(tempInputFile);
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
// Clean up the temporary files
|
|
||||||
Files.delete(tempInputFile);
|
|
||||||
Files.delete(tempOutputFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return the optimized PDF as a response
|
// Return the optimized PDF as a response
|
||||||
String outputFilename =
|
String outputFilename =
|
||||||
Filenames.toSimpleFileName(inputFile.getOriginalFilename())
|
Filenames.toSimpleFileName(inputFile.getOriginalFilename())
|
||||||
.replaceFirst("[.][^.]+$", "")
|
.replaceFirst("[.][^.]+$", "")
|
||||||
+ "_Optimized.pdf";
|
+ "_Optimized.pdf";
|
||||||
return WebResponseUtils.bytesToWebResponse(pdfBytes, outputFilename);
|
return WebResponseUtils.bytesToWebResponse(pdfBytes, outputFilename);
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
// Clean up the temporary files
|
||||||
|
// deleted by multipart file handler deu to transferTo?
|
||||||
|
// Files.deleteIfExists(tempInputFile);
|
||||||
|
Files.deleteIfExists(tempOutputFile);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.StandardCopyOption;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -103,10 +102,7 @@ public class ExtractImageScansController {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
tempInputFile = Files.createTempFile("input_", "." + extension);
|
tempInputFile = Files.createTempFile("input_", "." + extension);
|
||||||
Files.copy(
|
form.getFileInput().transferTo(tempInputFile);
|
||||||
form.getFileInput().getInputStream(),
|
|
||||||
tempInputFile,
|
|
||||||
StandardCopyOption.REPLACE_EXISTING);
|
|
||||||
// Add input file path to images list
|
// Add input file path to images list
|
||||||
images.add(tempInputFile.toString());
|
images.add(tempInputFile.toString());
|
||||||
}
|
}
|
||||||
|
@ -176,11 +172,14 @@ public class ExtractImageScansController {
|
||||||
byte[] zipBytes = Files.readAllBytes(tempZipFile);
|
byte[] zipBytes = Files.readAllBytes(tempZipFile);
|
||||||
|
|
||||||
// Clean up the temporary zip file
|
// Clean up the temporary zip file
|
||||||
Files.delete(tempZipFile);
|
Files.deleteIfExists(tempZipFile);
|
||||||
|
|
||||||
return WebResponseUtils.bytesToWebResponse(
|
return WebResponseUtils.bytesToWebResponse(
|
||||||
zipBytes, outputZipFilename, MediaType.APPLICATION_OCTET_STREAM);
|
zipBytes, outputZipFilename, MediaType.APPLICATION_OCTET_STREAM);
|
||||||
|
} if (processedImageBytes.size() == 0) {
|
||||||
|
throw new IllegalArgumentException("No images detected");
|
||||||
}else {
|
}else {
|
||||||
|
|
||||||
// Return the processed image as a response
|
// Return the processed image as a response
|
||||||
byte[] imageBytes = processedImageBytes.get(0);
|
byte[] imageBytes = processedImageBytes.get(0);
|
||||||
return WebResponseUtils.bytesToWebResponse(
|
return WebResponseUtils.bytesToWebResponse(
|
||||||
|
@ -201,7 +200,7 @@ public class ExtractImageScansController {
|
||||||
|
|
||||||
if (tempZipFile != null && Files.exists(tempZipFile)) {
|
if (tempZipFile != null && Files.exists(tempZipFile)) {
|
||||||
try {
|
try {
|
||||||
Files.delete(tempZipFile);
|
Files.deleteIfExists(tempZipFile);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.error("Failed to delete temporary zip file: " + tempZipFile, e);
|
logger.error("Failed to delete temporary zip file: " + tempZipFile, e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.StandardCopyOption;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -91,14 +90,12 @@ public class OCRController {
|
||||||
}
|
}
|
||||||
// Save the uploaded file to a temporary location
|
// Save the uploaded file to a temporary location
|
||||||
Path tempInputFile = Files.createTempFile("input_", ".pdf");
|
Path tempInputFile = Files.createTempFile("input_", ".pdf");
|
||||||
Files.copy(inputFile.getInputStream(), tempInputFile, StandardCopyOption.REPLACE_EXISTING);
|
|
||||||
|
|
||||||
// Prepare the output file path
|
|
||||||
Path tempOutputFile = Files.createTempFile("output_", ".pdf");
|
Path tempOutputFile = Files.createTempFile("output_", ".pdf");
|
||||||
|
|
||||||
// Prepare the output file path
|
|
||||||
Path sidecarTextPath = null;
|
Path sidecarTextPath = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
inputFile.transferTo(tempInputFile.toFile());
|
||||||
|
|
||||||
// Run OCR Command
|
// Run OCR Command
|
||||||
String languageOption = String.join("+", selectedLanguages);
|
String languageOption = String.join("+", selectedLanguages);
|
||||||
|
|
||||||
|
@ -151,7 +148,8 @@ public class OCRController {
|
||||||
.runCommandWithOutputHandling(command);
|
.runCommandWithOutputHandling(command);
|
||||||
if (result.getRc() != 0
|
if (result.getRc() != 0
|
||||||
&& result.getMessages().contains("multiprocessing/synchronize.py")
|
&& result.getMessages().contains("multiprocessing/synchronize.py")
|
||||||
&& result.getMessages().contains("OSError: [Errno 38] Function not implemented")) {
|
&& result.getMessages()
|
||||||
|
.contains("OSError: [Errno 38] Function not implemented")) {
|
||||||
command.add("--jobs");
|
command.add("--jobs");
|
||||||
command.add("1");
|
command.add("1");
|
||||||
result =
|
result =
|
||||||
|
@ -178,8 +176,6 @@ public class OCRController {
|
||||||
}
|
}
|
||||||
// Read the OCR processed PDF file
|
// Read the OCR processed PDF file
|
||||||
byte[] pdfBytes = Files.readAllBytes(tempOutputFile);
|
byte[] pdfBytes = Files.readAllBytes(tempOutputFile);
|
||||||
// Clean up the temporary files
|
|
||||||
Files.delete(tempInputFile);
|
|
||||||
|
|
||||||
// Return the OCR processed PDF as a response
|
// Return the OCR processed PDF as a response
|
||||||
String outputFilename =
|
String outputFilename =
|
||||||
|
@ -213,17 +209,26 @@ public class OCRController {
|
||||||
byte[] zipBytes = Files.readAllBytes(tempZipFile);
|
byte[] zipBytes = Files.readAllBytes(tempZipFile);
|
||||||
|
|
||||||
// Clean up the temporary zip file
|
// Clean up the temporary zip file
|
||||||
Files.delete(tempZipFile);
|
Files.deleteIfExists(tempZipFile);
|
||||||
Files.delete(tempOutputFile);
|
Files.deleteIfExists(tempOutputFile);
|
||||||
Files.delete(sidecarTextPath);
|
Files.deleteIfExists(sidecarTextPath);
|
||||||
|
|
||||||
// Return the zip file containing both the PDF and the text file
|
// Return the zip file containing both the PDF and the text file
|
||||||
return WebResponseUtils.bytesToWebResponse(
|
return WebResponseUtils.bytesToWebResponse(
|
||||||
zipBytes, outputZipFilename, MediaType.APPLICATION_OCTET_STREAM);
|
zipBytes, outputZipFilename, MediaType.APPLICATION_OCTET_STREAM);
|
||||||
} else {
|
} else {
|
||||||
// Return the OCR processed PDF as a response
|
// Return the OCR processed PDF as a response
|
||||||
Files.delete(tempOutputFile);
|
Files.deleteIfExists(tempOutputFile);
|
||||||
return WebResponseUtils.bytesToWebResponse(pdfBytes, outputFilename);
|
return WebResponseUtils.bytesToWebResponse(pdfBytes, outputFilename);
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
// Clean up the temporary files
|
||||||
|
Files.deleteIfExists(tempOutputFile);
|
||||||
|
// Comment out as transferTo makes multipart handle cleanup
|
||||||
|
// Files.deleteIfExists(tempInputFile);
|
||||||
|
if (sidecarTextPath != null) {
|
||||||
|
Files.deleteIfExists(sidecarTextPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,10 +41,10 @@ public class RepairController {
|
||||||
MultipartFile inputFile = request.getFileInput();
|
MultipartFile inputFile = request.getFileInput();
|
||||||
// Save the uploaded file to a temporary location
|
// Save the uploaded file to a temporary location
|
||||||
Path tempInputFile = Files.createTempFile("input_", ".pdf");
|
Path tempInputFile = Files.createTempFile("input_", ".pdf");
|
||||||
inputFile.transferTo(tempInputFile.toFile());
|
|
||||||
|
|
||||||
// Prepare the output file path
|
|
||||||
Path tempOutputFile = Files.createTempFile("output_", ".pdf");
|
Path tempOutputFile = Files.createTempFile("output_", ".pdf");
|
||||||
|
byte[] pdfBytes = null;
|
||||||
|
inputFile.transferTo(tempInputFile.toFile());
|
||||||
|
try {
|
||||||
|
|
||||||
List<String> command = new ArrayList<>();
|
List<String> command = new ArrayList<>();
|
||||||
command.add("gs");
|
command.add("gs");
|
||||||
|
@ -58,11 +58,7 @@ public class RepairController {
|
||||||
.runCommandWithOutputHandling(command);
|
.runCommandWithOutputHandling(command);
|
||||||
|
|
||||||
// Read the optimized PDF file
|
// Read the optimized PDF file
|
||||||
byte[] pdfBytes = Files.readAllBytes(tempOutputFile);
|
pdfBytes = Files.readAllBytes(tempOutputFile);
|
||||||
|
|
||||||
// Clean up the temporary files
|
|
||||||
Files.delete(tempInputFile);
|
|
||||||
Files.delete(tempOutputFile);
|
|
||||||
|
|
||||||
// Return the optimized PDF as a response
|
// Return the optimized PDF as a response
|
||||||
String outputFilename =
|
String outputFilename =
|
||||||
|
@ -70,5 +66,11 @@ public class RepairController {
|
||||||
.replaceFirst("[.][^.]+$", "")
|
.replaceFirst("[.][^.]+$", "")
|
||||||
+ "_repaired.pdf";
|
+ "_repaired.pdf";
|
||||||
return WebResponseUtils.bytesToWebResponse(pdfBytes, outputFilename);
|
return WebResponseUtils.bytesToWebResponse(pdfBytes, outputFilename);
|
||||||
|
} finally {
|
||||||
|
// Clean up the temporary files
|
||||||
|
Files.deleteIfExists(tempInputFile);
|
||||||
|
Files.deleteIfExists(tempOutputFile);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,8 +79,8 @@ public class FileToPdf {
|
||||||
} finally {
|
} finally {
|
||||||
|
|
||||||
// Clean up temporary files
|
// Clean up temporary files
|
||||||
Files.delete(tempOutputFile);
|
Files.deleteIfExists(tempOutputFile);
|
||||||
Files.delete(tempInputFile);
|
Files.deleteIfExists(tempInputFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
return pdfBytes;
|
return pdfBytes;
|
||||||
|
|
|
@ -6,7 +6,6 @@ import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.StandardCopyOption;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -47,8 +46,7 @@ public class PDFToFile {
|
||||||
try {
|
try {
|
||||||
// Save the uploaded file to a temporary location
|
// Save the uploaded file to a temporary location
|
||||||
tempInputFile = Files.createTempFile("input_", ".pdf");
|
tempInputFile = Files.createTempFile("input_", ".pdf");
|
||||||
Files.copy(
|
inputFile.transferTo(tempInputFile);
|
||||||
inputFile.getInputStream(), tempInputFile, StandardCopyOption.REPLACE_EXISTING);
|
|
||||||
|
|
||||||
// Prepare the output directory
|
// Prepare the output directory
|
||||||
tempOutputDir = Files.createTempDirectory("output_");
|
tempOutputDir = Files.createTempDirectory("output_");
|
||||||
|
@ -85,7 +83,7 @@ public class PDFToFile {
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
// Clean up the temporary files
|
// Clean up the temporary files
|
||||||
if (tempInputFile != null) Files.delete(tempInputFile);
|
if (tempInputFile != null) Files.deleteIfExists(tempInputFile);
|
||||||
if (tempOutputDir != null) FileUtils.deleteDirectory(tempOutputDir.toFile());
|
if (tempOutputDir != null) FileUtils.deleteDirectory(tempOutputDir.toFile());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,8 +125,7 @@ public class PDFToFile {
|
||||||
try {
|
try {
|
||||||
// Save the uploaded file to a temporary location
|
// Save the uploaded file to a temporary location
|
||||||
tempInputFile = Files.createTempFile("input_", ".pdf");
|
tempInputFile = Files.createTempFile("input_", ".pdf");
|
||||||
Files.copy(
|
inputFile.transferTo(tempInputFile);
|
||||||
inputFile.getInputStream(), tempInputFile, StandardCopyOption.REPLACE_EXISTING);
|
|
||||||
|
|
||||||
// Prepare the output directory
|
// Prepare the output directory
|
||||||
tempOutputDir = Files.createTempDirectory("output_");
|
tempOutputDir = Files.createTempDirectory("output_");
|
||||||
|
@ -180,7 +177,7 @@ public class PDFToFile {
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
// Clean up the temporary files
|
// Clean up the temporary files
|
||||||
if (tempInputFile != null) Files.delete(tempInputFile);
|
Files.deleteIfExists(tempInputFile);
|
||||||
if (tempOutputDir != null) FileUtils.deleteDirectory(tempOutputDir.toFile());
|
if (tempOutputDir != null) FileUtils.deleteDirectory(tempOutputDir.toFile());
|
||||||
}
|
}
|
||||||
System.out.println("fileBytes=" + fileBytes.length);
|
System.out.println("fileBytes=" + fileBytes.length);
|
||||||
|
|
Loading…
Reference in a new issue