35
35
36
36
import java .io .File ;
37
37
import java .net .URL ;
38
+ import java .nio .file .Files ;
39
+ import java .nio .file .LinkOption ;
40
+ import java .nio .file .Path ;
41
+ import java .nio .file .Paths ;
38
42
import java .util .Observable ;
39
43
import java .util .Observer ;
40
44
@@ -49,36 +53,50 @@ public DownloadableContributionsDownloader(File _stagingFolder) {
49
53
stagingFolder = _stagingFolder ;
50
54
}
51
55
52
- public File download (DownloadableContribution contribution ,
53
- final Progress progress , final String statusText )
54
- throws Exception {
56
+ public File download (DownloadableContribution contribution , Progress progress , final String statusText ) throws Exception {
55
57
URL url = new URL (contribution .getUrl ());
56
- final File outputFile = new File (stagingFolder , contribution .getArchiveFileName ());
58
+ Path outputFile = Paths . get (stagingFolder . getAbsolutePath () , contribution .getArchiveFileName ());
57
59
58
60
// Ensure the existence of staging folder
59
- stagingFolder .mkdirs ();
61
+ Files .createDirectories (stagingFolder .toPath ());
62
+
63
+ if (!hasChecksum (contribution ) && Files .exists (outputFile )) {
64
+ Files .delete (outputFile );
65
+ }
60
66
61
67
// Need to download or resume downloading?
62
- if (!outputFile . isFile ( ) || (outputFile . length ( ) < contribution .getSize ())) {
63
- download (url , outputFile , progress , statusText );
68
+ if (!Files . isRegularFile ( outputFile , LinkOption . NOFOLLOW_LINKS ) || (Files . size ( outputFile ) < contribution .getSize ())) {
69
+ download (url , outputFile . toFile () , progress , statusText );
64
70
}
65
71
66
72
// Test checksum
67
73
progress .setStatus (_ ("Verifying archive integrity..." ));
68
74
onProgress (progress );
69
75
String checksum = contribution .getChecksum ();
70
- String algo = checksum .split (":" )[0 ];
71
- if (!FileHash .hash (outputFile , algo ).equalsIgnoreCase (checksum )) {
72
- throw new Exception (_ ("CRC doesn't match. File is corrupted." ));
76
+ if (hasChecksum (contribution )) {
77
+ String algo = checksum .split (":" )[0 ];
78
+ if (!FileHash .hash (outputFile .toFile (), algo ).equalsIgnoreCase (checksum )) {
79
+ throw new Exception (_ ("CRC doesn't match. File is corrupted." ));
80
+ }
73
81
}
74
82
75
83
contribution .setDownloaded (true );
76
- contribution .setDownloadedFile (outputFile );
77
- return outputFile ;
84
+ contribution .setDownloadedFile (outputFile .toFile ());
85
+ return outputFile .toFile ();
86
+ }
87
+
88
+ private boolean hasChecksum (DownloadableContribution contribution ) {
89
+ String checksum = contribution .getChecksum ();
90
+ if (checksum == null || checksum .isEmpty ()) {
91
+ return false ;
92
+ }
93
+
94
+ String algo = checksum .split (":" )[0 ];
95
+
96
+ return algo != null && algo .isEmpty ();
78
97
}
79
98
80
- public void download (URL url , File tmpFile , final Progress progress ,
81
- final String statusText ) throws Exception {
99
+ public void download (URL url , File tmpFile , Progress progress , String statusText ) throws Exception {
82
100
FileDownloader downloader = new FileDownloader (url , tmpFile );
83
101
downloader .addObserver (new Observer () {
84
102
@ Override
0 commit comments