22
33import ch .bildspur .vision .web .NetworkUtility ;
44
5+ import java .io .IOException ;
6+ import java .nio .file .CopyOption ;
57import java .nio .file .Files ;
68import java .nio .file .Path ;
79import java .nio .file .Paths ;
810import java .util .concurrent .atomic .AtomicReference ;
911
12+ import static java .nio .file .StandardCopyOption .REPLACE_EXISTING ;
13+
1014public 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 () {
0 commit comments