Skip to content

Commit 530c592

Browse files
authored
Merge pull request #200 from secvisogram/fix/#102-remove_temp_files
#102 remove the created file temporary files,
2 parents 83553de + 55e33c0 commit 530c592

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/main/java/de/bsi/secvisogram/csaf_cms_backend/mustache/JavascriptExporter.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.slf4j.LoggerFactory;
2020
import org.springframework.http.MediaType;
2121
import org.springframework.stereotype.Service;
22+
import org.springframework.util.FileSystemUtils;
2223

2324
/**
2425
* Create Html String from a mustache template file and Json Input File
@@ -44,7 +45,9 @@ public String createHtml(@Nonnull final String advisoryJson) throws IOException
4445
// It has to bbe available in the Context WorkingDirectory
4546
final String documentEntityScript = "DocumentEntity.mjs";
4647
final Path tempDir = Files.createTempDirectory("mustache");
48+
LOG.info("Creating temporary directory: {}", tempDir.toFile().getAbsolutePath());
4749
final Path tempDocFile = tempDir.resolve(documentEntityScript);
50+
LOG.info("Creating temporary doc file: {}", tempDocFile.toFile().getAbsolutePath());
4851

4952
try (final InputStream in = JavascriptExporter.class.getResourceAsStream(documentEntityScript)) {
5053
Files.write(tempDocFile, in.readAllBytes());
@@ -70,6 +73,13 @@ public String createHtml(@Nonnull final String advisoryJson) throws IOException
7073
final Value renderFunction = scriptResult.getMember("renderWithMustache");
7174
final Object result = renderFunction.execute(template, advisoryJson, createLogoJson());
7275
return result.toString();
76+
} finally {
77+
try {
78+
LOG.info("Delete temporary directory: {}", tempDir.toFile().getAbsolutePath());
79+
FileSystemUtils.deleteRecursively(tempDir);
80+
} catch (IOException ex) {
81+
LOG.warn("Failed to delete temporary directory: {}", tempDir.toFile().getAbsolutePath(), ex);
82+
}
7383
}
7484
}
7585

src/main/java/de/bsi/secvisogram/csaf_cms_backend/rest/AdvisoryController.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -714,9 +714,11 @@ public ResponseEntity<InputStreamResource> exportAdvisory(
714714
return ResponseEntity.status(ex.getRecommendedHttpState()).build();
715715
} finally {
716716
if (filePath != null) {
717-
boolean result = filePath.toFile().delete();
718-
if (!result) {
719-
LOG.error("Could not delete temporary file {} after exporting.", filePath);
717+
LOG.info(String.format("Deleting file: %s", filePath));
718+
try {
719+
Files.delete(filePath);
720+
} catch (IOException ex) {
721+
LOG.error(String.format("Error deleting file: %s", filePath), ex);
720722
}
721723
}
722724
}

0 commit comments

Comments
 (0)