Skip to content

Commit

Permalink
using temp file for downloads to not end with corrupted files
Browse files Browse the repository at this point in the history
  • Loading branch information
cansik committed Apr 20, 2021
1 parent bee5bd2 commit 288f8af
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'ch.bildspur'
version '0.6.2'
version '0.6.3'

sourceCompatibility = 1.8

Expand Down
32 changes: 26 additions & 6 deletions src/main/java/ch/bildspur/vision/dependency/Dependency.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<Integer> 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;
Expand All @@ -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() {
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/ch/bildspur/vision/web/NetworkUtility.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -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();
}
}
}
}

Expand Down

0 comments on commit 288f8af

Please sign in to comment.