From 288f8af7815ec27a4edb891e7735375b1753e6bf Mon Sep 17 00:00:00 2001 From: Florian Bruggisser Date: Tue, 20 Apr 2021 09:39:42 +0200 Subject: [PATCH] using temp file for downloads to not end with corrupted files --- build.gradle | 2 +- .../vision/dependency/Dependency.java | 32 +++++++++++++++---- .../bildspur/vision/web/NetworkUtility.java | 11 ++++++- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/build.gradle b/build.gradle index 5a4049f..44fb741 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group 'ch.bildspur' -version '0.6.2' +version '0.6.3' sourceCompatibility = 1.8 diff --git a/src/main/java/ch/bildspur/vision/dependency/Dependency.java b/src/main/java/ch/bildspur/vision/dependency/Dependency.java index 7ebd3a4..dbaf89f 100644 --- a/src/main/java/ch/bildspur/vision/dependency/Dependency.java +++ b/src/main/java/ch/bildspur/vision/dependency/Dependency.java @@ -2,15 +2,20 @@ import ch.bildspur.vision.web.NetworkUtility; +import java.io.IOException; +import java.nio.file.CopyOption; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.concurrent.atomic.AtomicReference; +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; + public class Dependency { private String name; private String url; private Path path; + private String tempSuffix = "_tmp"; public Dependency(String name) { this(name, Repository.repositoryRootUrl + name); @@ -30,11 +35,19 @@ public boolean resolve() { return true; } + // check temp file + Path tempPath = Paths.get(path.toString() + tempSuffix); + try { + Files.deleteIfExists(tempPath); + } catch (IOException e) { + System.err.println("Could not delete " + tempPath.toString()); + return false; + } + // try to download - // todo: download it as temp file to not brake it on hard processing exit! System.out.print("downloading " + name + ": "); AtomicReference lastProgress = new AtomicReference<>(0); - NetworkUtility.downloadFile(url, path, (source, p) -> { + NetworkUtility.downloadFile(url, tempPath, (source, p) -> { int last = lastProgress.get(); int progress = Math.round((float) p); int delta = progress - last; @@ -44,13 +57,20 @@ public boolean resolve() { System.out.print("."); } }); + + // switch name + try { + Files.move(tempPath, path, REPLACE_EXISTING); + } catch (IOException e) { + System.err.println("Could not move " + tempPath.toString() + " to " + path.toString()); + e.printStackTrace(); + return false; + } + System.out.println(" done!"); // second check after download - if (Files.exists(path)) { - return true; - } - return false; + return Files.exists(path); } public String getName() { diff --git a/src/main/java/ch/bildspur/vision/web/NetworkUtility.java b/src/main/java/ch/bildspur/vision/web/NetworkUtility.java index 95e6d04..708efa4 100644 --- a/src/main/java/ch/bildspur/vision/web/NetworkUtility.java +++ b/src/main/java/ch/bildspur/vision/web/NetworkUtility.java @@ -1,6 +1,7 @@ package ch.bildspur.vision.web; import java.io.FileOutputStream; +import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; import java.nio.channels.Channels; @@ -9,7 +10,7 @@ public class NetworkUtility { public static void downloadFile(String remoteURL, Path localPath, ProgressCallBack callback) { - FileOutputStream fos; + FileOutputStream fos = null; ReadableByteChannel rbc; URL url; try { @@ -20,6 +21,14 @@ public static void downloadFile(String remoteURL, Path localPath, ProgressCallBa fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); } catch (Exception e) { e.printStackTrace(); + } finally { + if (fos != null) { + try { + fos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } }