Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.FileSystemUtils;

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

try (final InputStream in = JavascriptExporter.class.getResourceAsStream(documentEntityScript)) {
Files.write(tempDocFile, in.readAllBytes());
Expand All @@ -70,6 +73,13 @@ public String createHtml(@Nonnull final String advisoryJson) throws IOException
final Value renderFunction = scriptResult.getMember("renderWithMustache");
final Object result = renderFunction.execute(template, advisoryJson, createLogoJson());
return result.toString();
} finally {
try {
LOG.info("Delete temporary directory: {}", tempDir.toFile().getAbsolutePath());
FileSystemUtils.deleteRecursively(tempDir);
} catch (IOException ex) {
LOG.warn("Failed to delete temporary directory: {}", tempDir.toFile().getAbsolutePath(), ex);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,9 +714,11 @@ public ResponseEntity<InputStreamResource> exportAdvisory(
return ResponseEntity.status(ex.getRecommendedHttpState()).build();
} finally {
if (filePath != null) {
boolean result = filePath.toFile().delete();
if (!result) {
LOG.error("Could not delete temporary file {} after exporting.", filePath);
LOG.info(String.format("Deleting file: %s", filePath));
try {
Files.delete(filePath);
} catch (IOException ex) {
LOG.error(String.format("Error deleting file: %s", filePath), ex);
}
}
}
Expand Down
Loading