Skip to content

Commit 288f8af

Browse files
committed
using temp file for downloads to not end with corrupted files
1 parent bee5bd2 commit 288f8af

3 files changed

Lines changed: 37 additions & 8 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group 'ch.bildspur'
7-
version '0.6.2'
7+
version '0.6.3'
88

99
sourceCompatibility = 1.8
1010

src/main/java/ch/bildspur/vision/dependency/Dependency.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@
22

33
import ch.bildspur.vision.web.NetworkUtility;
44

5+
import java.io.IOException;
6+
import java.nio.file.CopyOption;
57
import java.nio.file.Files;
68
import java.nio.file.Path;
79
import java.nio.file.Paths;
810
import java.util.concurrent.atomic.AtomicReference;
911

12+
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
13+
1014
public class Dependency {
1115
private String name;
1216
private String url;
1317
private Path path;
18+
private String tempSuffix = "_tmp";
1419

1520
public Dependency(String name) {
1621
this(name, Repository.repositoryRootUrl + name);
@@ -30,11 +35,19 @@ public boolean resolve() {
3035
return true;
3136
}
3237

38+
// check temp file
39+
Path tempPath = Paths.get(path.toString() + tempSuffix);
40+
try {
41+
Files.deleteIfExists(tempPath);
42+
} catch (IOException e) {
43+
System.err.println("Could not delete " + tempPath.toString());
44+
return false;
45+
}
46+
3347
// try to download
34-
// todo: download it as temp file to not brake it on hard processing exit!
3548
System.out.print("downloading " + name + ": ");
3649
AtomicReference<Integer> lastProgress = new AtomicReference<>(0);
37-
NetworkUtility.downloadFile(url, path, (source, p) -> {
50+
NetworkUtility.downloadFile(url, tempPath, (source, p) -> {
3851
int last = lastProgress.get();
3952
int progress = Math.round((float) p);
4053
int delta = progress - last;
@@ -44,13 +57,20 @@ public boolean resolve() {
4457
System.out.print(".");
4558
}
4659
});
60+
61+
// switch name
62+
try {
63+
Files.move(tempPath, path, REPLACE_EXISTING);
64+
} catch (IOException e) {
65+
System.err.println("Could not move " + tempPath.toString() + " to " + path.toString());
66+
e.printStackTrace();
67+
return false;
68+
}
69+
4770
System.out.println(" done!");
4871

4972
// second check after download
50-
if (Files.exists(path)) {
51-
return true;
52-
}
53-
return false;
73+
return Files.exists(path);
5474
}
5575

5676
public String getName() {

src/main/java/ch/bildspur/vision/web/NetworkUtility.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ch.bildspur.vision.web;
22

33
import java.io.FileOutputStream;
4+
import java.io.IOException;
45
import java.net.HttpURLConnection;
56
import java.net.URL;
67
import java.nio.channels.Channels;
@@ -9,7 +10,7 @@
910

1011
public class NetworkUtility {
1112
public static void downloadFile(String remoteURL, Path localPath, ProgressCallBack callback) {
12-
FileOutputStream fos;
13+
FileOutputStream fos = null;
1314
ReadableByteChannel rbc;
1415
URL url;
1516
try {
@@ -20,6 +21,14 @@ public static void downloadFile(String remoteURL, Path localPath, ProgressCallBa
2021
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
2122
} catch (Exception e) {
2223
e.printStackTrace();
24+
} finally {
25+
if (fos != null) {
26+
try {
27+
fos.close();
28+
} catch (IOException e) {
29+
e.printStackTrace();
30+
}
31+
}
2332
}
2433
}
2534

0 commit comments

Comments
 (0)