This commit is contained in:
Anthony Stirling
2024-05-27 16:31:00 +01:00
parent b93bff5cad
commit 6ffa80c386
21 changed files with 526 additions and 147 deletions

View File

@@ -135,7 +135,9 @@ public class MergeController {
throw ex;
} finally {
for (File file : filesToDelete) {
file.delete();
if (file != null) {
Files.deleteIfExists(file.toPath());
}
}
}
}

View File

@@ -87,12 +87,12 @@ public class PdfOverlayController {
} finally {
for (File overlayPdfFile : overlayPdfFiles) {
if (overlayPdfFile != null) {
overlayPdfFile.delete();
Files.deleteIfExists(overlayPdfFile.toPath());
}
}
for (File tempFile : tempFiles) { // Delete temporary files
if (tempFile != null) {
tempFile.delete();
Files.deleteIfExists(tempFile.toPath());
}
}
}

View File

@@ -121,7 +121,7 @@ public class SplitPDFController {
logger.info("Successfully created zip file with split documents: {}", zipFile.toString());
byte[] data = Files.readAllBytes(zipFile);
Files.delete(zipFile);
Files.deleteIfExists(zipFile);
// return the Resource in the response
return WebResponseUtils.bytesToWebResponse(

View File

@@ -63,10 +63,7 @@ public class SplitPdfBySectionsController {
MergeController mergeController = new MergeController();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
mergeController.mergeDocuments(splitDocuments).save(baos);
return WebResponseUtils.bytesToWebResponse(
baos.toByteArray(),
filename + "_split.pdf",
MediaType.APPLICATION_OCTET_STREAM);
return WebResponseUtils.bytesToWebResponse(baos.toByteArray(), filename + "_split.pdf");
}
for (PDDocument doc : splitDocuments) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -98,7 +95,7 @@ public class SplitPdfBySectionsController {
e.printStackTrace();
} finally {
data = Files.readAllBytes(zipFile);
Files.delete(zipFile);
Files.deleteIfExists(zipFile);
}
return WebResponseUtils.bytesToWebResponse(

View File

@@ -59,7 +59,7 @@ public class ConvertWebsiteToPDF {
pdfBytes = Files.readAllBytes(tempOutputFile);
} finally {
// Clean up the temporary files
Files.delete(tempOutputFile);
Files.deleteIfExists(tempOutputFile);
}
// Convert URL to a safe filename
String outputFilename = convertURLToFileName(URL);

View File

@@ -118,7 +118,7 @@ public class AutoSplitPdfController {
e.printStackTrace();
} finally {
data = Files.readAllBytes(zipFile);
Files.delete(zipFile);
Files.deleteIfExists(zipFile);
}
return WebResponseUtils.bytesToWebResponse(

View File

@@ -185,10 +185,12 @@ public class StampController {
try (InputStream is = classPathResource.getInputStream();
FileOutputStream os = new FileOutputStream(tempFile)) {
IOUtils.copy(is, os);
font = PDType0Font.load(document, tempFile);
} finally {
if (tempFile != null) {
Files.deleteIfExists(tempFile.toPath());
}
}
font = PDType0Font.load(document, tempFile);
tempFile.deleteOnExit();
}
contentStream.setFont(font, fontSize);

View File

@@ -150,10 +150,10 @@ public class WatermarkController {
try (InputStream is = classPathResource.getInputStream();
FileOutputStream os = new FileOutputStream(tempFile)) {
IOUtils.copy(is, os);
font = PDType0Font.load(document, tempFile);
} finally {
if (tempFile != null) Files.deleteIfExists(tempFile.toPath());
}
font = PDType0Font.load(document, tempFile);
tempFile.deleteOnExit();
}
contentStream.setFont(font, fontSize);

View File

@@ -38,14 +38,14 @@ public class GeneralUtils {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
Files.delete(file);
Files.deleteIfExists(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc)
throws IOException {
Files.delete(dir);
Files.deleteIfExists(dir);
return FileVisitResult.CONTINUE;
}
});