Skip to content

Commit

Permalink
Merge pull request #9637 from MPDL/IQSS/3818_delete_thumbnail_tmp_files
Browse files Browse the repository at this point in the history
3818 Delete temporary thumbnail files
  • Loading branch information
pdurbin authored Feb 14, 2024
2 parents c273382 + 31225e9 commit f456e51
Showing 1 changed file with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ private static boolean generatePDFThumbnail(StorageIO<DataFile> storageIO, int s
// will run the ImageMagick on it, and will save its output in another temp
// file, and will save it as an "auxiliary" file via the driver.
boolean tempFilesRequired = false;
File tempFile = null;

try {
Path pdfFilePath = storageIO.getFileSystemPath();
Expand All @@ -225,7 +226,7 @@ private static boolean generatePDFThumbnail(StorageIO<DataFile> storageIO, int s
}

if (tempFilesRequired) {
InputStream inputStream = null;
InputStream inputStream = null;
try {
storageIO.open();
inputStream = storageIO.getInputStream();
Expand All @@ -234,12 +235,11 @@ private static boolean generatePDFThumbnail(StorageIO<DataFile> storageIO, int s
return false;
}

File tempFile;
OutputStream outputStream = null;
try {
tempFile = File.createTempFile("tempFileToRescale", ".tmp");
outputStream = new FileOutputStream(tempFile);
//Reads/transfers all bytes from the input stream to the output stream.
//Reads/transfers all bytes from the input stream to the output stream.
inputStream.transferTo(outputStream);
} catch (IOException ioex) {
logger.warning("GenerateImageThumb: failed to save pdf bytes in a temporary file.");
Expand Down Expand Up @@ -270,6 +270,12 @@ private static boolean generatePDFThumbnail(StorageIO<DataFile> storageIO, int s
logger.warning("failed to save generated pdf thumbnail, as AUX file " + THUMBNAIL_SUFFIX + size + "!");
return false;
}
finally {
try {
tempFile.delete();
}
catch (Exception e) {}
}
}

return true;
Expand Down Expand Up @@ -371,6 +377,14 @@ private static boolean generateImageThumbnailFromInputStream(StorageIO<DataFile>
logger.warning("Failed to rescale and/or save the image: " + ioex.getMessage());
return false;
}
finally {
if(tempFileRequired) {
try {
tempFile.delete();
}
catch (Exception e) {}
}
}

return true;

Expand Down

0 comments on commit f456e51

Please sign in to comment.