From 380028376a921f16ec586ce96cfed4b5938bf5e8 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Sun, 20 Feb 2022 02:16:47 +0100 Subject: [PATCH] Improved temporary file removing. --- src/fr/devinsy/statoolinfos/crawl/Crawler.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/fr/devinsy/statoolinfos/crawl/Crawler.java b/src/fr/devinsy/statoolinfos/crawl/Crawler.java index dcd240f..9b2d5c5 100644 --- a/src/fr/devinsy/statoolinfos/crawl/Crawler.java +++ b/src/fr/devinsy/statoolinfos/crawl/Crawler.java @@ -105,9 +105,9 @@ public class Crawler { logger.info("Crawling {}", url); + File downloadFile = null; try { - File downloadFile; try { downloadFile = download(url); @@ -158,6 +158,7 @@ public class Crawler { logger.error("ERROR: bad child class [{}][{}].", downloadClass, parent); this.journal.add(url, parentURL, CrawlStatus.BADCHILDCLASS); + downloadFile.delete(); } else { @@ -177,6 +178,7 @@ public class Crawler if (StringUtils.equals(downloadSha, storedSha)) { this.journal.add(url, parentURL, CrawlStatus.SUCCESS); + downloadFile.delete(); } else { @@ -234,6 +236,13 @@ public class Crawler { this.journal.add(url, parentURL, CrawlStatus.IOERROR); } + finally + { + if ((downloadFile != null) && (downloadFile.exists())) + { + downloadFile.delete(); + } + } } /** @@ -325,6 +334,7 @@ public class Crawler { final int TIMEOUT = 5000; result = Files.createTempFile("tmp-", ".statoolsinfos").toFile(); + result.deleteOnExit(); FileUtils.copyURLToFile(url, result, TIMEOUT, TIMEOUT); }