mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2026-03-13 02:18:16 +01:00
cucumber for days (#5766)
This commit is contained in:
@@ -158,13 +158,23 @@ public class CustomPDFDocumentFactory {
|
||||
|
||||
// Since we don't know the size upfront, buffer to a temp file
|
||||
Path tempFile = createTempFile("pdf-stream-");
|
||||
|
||||
Files.copy(input, tempFile, StandardCopyOption.REPLACE_EXISTING);
|
||||
PDDocument doc = loadAdaptively(tempFile.toFile(), Files.size(tempFile));
|
||||
if (!readOnly) {
|
||||
postProcessDocument(doc);
|
||||
boolean success = false;
|
||||
try {
|
||||
Files.copy(input, tempFile, StandardCopyOption.REPLACE_EXISTING);
|
||||
PDDocument doc = loadAdaptively(tempFile.toFile(), Files.size(tempFile));
|
||||
if (!readOnly) {
|
||||
postProcessDocument(doc);
|
||||
}
|
||||
success = true;
|
||||
return doc;
|
||||
} finally {
|
||||
// On success: small files are deleted inside loadAdaptively; large files are deleted
|
||||
// by DeletingRandomAccessFile when the returned PDDocument is closed.
|
||||
// On failure: clean up the temp file ourselves since no one else will.
|
||||
if (!success) {
|
||||
Files.deleteIfExists(tempFile);
|
||||
}
|
||||
}
|
||||
return doc;
|
||||
}
|
||||
|
||||
/** Load with password from InputStream */
|
||||
@@ -181,14 +191,21 @@ public class CustomPDFDocumentFactory {
|
||||
|
||||
// Since we don't know the size upfront, buffer to a temp file
|
||||
Path tempFile = createTempFile("pdf-stream-");
|
||||
|
||||
Files.copy(input, tempFile, StandardCopyOption.REPLACE_EXISTING);
|
||||
PDDocument doc =
|
||||
loadAdaptivelyWithPassword(tempFile.toFile(), Files.size(tempFile), password);
|
||||
if (!readOnly) {
|
||||
postProcessDocument(doc);
|
||||
boolean success = false;
|
||||
try {
|
||||
Files.copy(input, tempFile, StandardCopyOption.REPLACE_EXISTING);
|
||||
PDDocument doc =
|
||||
loadAdaptivelyWithPassword(tempFile.toFile(), Files.size(tempFile), password);
|
||||
if (!readOnly) {
|
||||
postProcessDocument(doc);
|
||||
}
|
||||
success = true;
|
||||
return doc;
|
||||
} finally {
|
||||
if (!success) {
|
||||
Files.deleteIfExists(tempFile);
|
||||
}
|
||||
}
|
||||
return doc;
|
||||
}
|
||||
|
||||
/** Load from a file path string */
|
||||
@@ -358,9 +375,24 @@ public class CustomPDFDocumentFactory {
|
||||
if (size >= SMALL_FILE_THRESHOLD) {
|
||||
log.debug("Writing large byte array to temp file for password-protected PDF");
|
||||
Path tempFile = createTempFile("pdf-bytes-");
|
||||
|
||||
Files.write(tempFile, bytes);
|
||||
return Loader.loadPDF(tempFile.toFile(), password, null, null, cache);
|
||||
boolean success = false;
|
||||
try {
|
||||
Files.write(tempFile, bytes);
|
||||
// Use DeletingRandomAccessFile so the temp file is removed when the document closes
|
||||
PDDocument doc =
|
||||
Loader.loadPDF(
|
||||
new DeletingRandomAccessFile(tempFile.toFile()),
|
||||
password,
|
||||
null,
|
||||
null,
|
||||
cache);
|
||||
success = true;
|
||||
return doc;
|
||||
} finally {
|
||||
if (!success) {
|
||||
Files.deleteIfExists(tempFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
return Loader.loadPDF(bytes, password, null, null, cache);
|
||||
}
|
||||
@@ -426,9 +458,12 @@ public class CustomPDFDocumentFactory {
|
||||
}
|
||||
} else {
|
||||
Path tempFile = createTempFile("pdf-save-");
|
||||
|
||||
document.save(tempFile.toFile());
|
||||
return Files.readAllBytes(tempFile);
|
||||
try {
|
||||
document.save(tempFile.toFile());
|
||||
return Files.readAllBytes(tempFile);
|
||||
} finally {
|
||||
Files.deleteIfExists(tempFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -254,10 +254,11 @@ public class JobExecutorService {
|
||||
return ResponseEntity.internalServerError()
|
||||
.body(Map.of("error", "Job timed out after " + timeoutToUse + " ms"));
|
||||
} catch (RuntimeException e) {
|
||||
// Check if this is a wrapped typed exception that should be handled by
|
||||
// GlobalExceptionHandler
|
||||
// Check if this is a typed exception that should be handled by
|
||||
// GlobalExceptionHandler (either directly or wrapped)
|
||||
Throwable cause = e.getCause();
|
||||
if (cause instanceof stirling.software.common.util.ExceptionUtils.BaseAppException
|
||||
if (e instanceof IllegalArgumentException
|
||||
|| cause instanceof stirling.software.common.util.ExceptionUtils.BaseAppException
|
||||
|| cause
|
||||
instanceof
|
||||
stirling.software.common.util.ExceptionUtils
|
||||
|
||||
Reference in New Issue
Block a user