Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

refactor(api): move DeletingRandomAccessFile to CustomPDFDocumentFactory as a private static class (#7344)

brioscd49daf5c691
diff --git a/app/common/src/main/java/stirling/software/common/service/CustomPDFDocumentFactory.java b/app/common/src/main/java/stirling/software/common/service/CustomPDFDocumentFactory.java
index 178052ebf6..9a7caefc7d 100644
--- a/app/common/src/main/java/stirling/software/common/service/CustomPDFDocumentFactory.java
+++ b/app/common/src/main/java/stirling/software/common/service/CustomPDFDocumentFactory.java
@@ -729,4 +729,32 @@ public class CustomPDFDocumentFactory {
p.toFile().deleteOnExit();
return p;
}
+
+ /** A custom RandomAccessRead implementation that deletes the file when closed */
+ private static class DeletingRandomAccessFile extends RandomAccessReadBufferedFile {
+ private final Path tempFilePath;
+
+ public DeletingRandomAccessFile(File file) throws IOException {
+ super(file);
+ this.tempFilePath = file.toPath();
+ }
+
+ @Override
+ public void close() throws IOException {
+ try {
+ super.close();
+ } finally {
+ try {
+ boolean deleted = Files.deleteIfExists(tempFilePath);
+ if (deleted) {
+ log.info("Successfully deleted temp file: {}", tempFilePath);
+ } else {
+ log.warn("Failed to delete temp file (may not exist): {}", tempFilePath);
+ }
+ } catch (IOException e) {
+ log.error("Error deleting temp file: {}", tempFilePath, e);
+ }
+ }
+ }
+ }
}