29
29
30
30
package cc .arduino .utils .network ;
31
31
32
+ import cc .arduino .utils .FileHash ;
32
33
import org .apache .commons .compress .utils .IOUtils ;
33
34
import org .slf4j .Logger ;
34
35
import org .slf4j .LoggerFactory ;
44
45
import java .net .URL ;
45
46
import java .nio .file .Files ;
46
47
import java .nio .file .Paths ;
48
+ import java .security .NoSuchAlgorithmException ;
47
49
import java .util .Observable ;
48
50
import java .util .Optional ;
49
51
@@ -65,6 +67,8 @@ public enum Status {
65
67
private long downloaded ;
66
68
private final URL downloadUrl ;
67
69
70
+ private String downloadedContentMD5 ;
71
+
68
72
private final File outputFile ;
69
73
private InputStream stream = null ;
70
74
private Exception error ;
@@ -74,6 +78,20 @@ public FileDownloader(URL url, File file) {
74
78
outputFile = file ;
75
79
downloaded = 0 ;
76
80
initialSize = 0 ;
81
+
82
+ }
83
+
84
+ public String getDownloadedContentMD5 () {
85
+ if (this .downloadedContentMD5 == null && this .status == Status .COMPLETE ) {
86
+ try {
87
+ downloadedContentMD5 = FileHash .hash (this .outputFile , "MD5" );
88
+ } catch (NoSuchAlgorithmException e ) {
89
+ log .warn ("Cannot calculate the downloaded file MD5: " , e .getCause ());
90
+ } catch (IOException e ) {
91
+ log .warn ("Cannot open the downloaded file: " , e .getCause ());
92
+ }
93
+ }
94
+ return downloadedContentMD5 ;
77
95
}
78
96
79
97
public long getInitialSize () {
@@ -132,7 +150,8 @@ public void download(boolean noResume) throws InterruptedException {
132
150
133
151
private void saveLocalFile () {
134
152
try {
135
- Files .write (outputFile .toPath (), Files .readAllBytes (Paths .get (downloadUrl .getPath ())));
153
+ Files .write (outputFile .toPath (),
154
+ Files .readAllBytes (Paths .get (downloadUrl .getPath ())));
136
155
setStatus (Status .COMPLETE );
137
156
} catch (Exception e ) {
138
157
setStatus (Status .ERROR );
@@ -147,21 +166,24 @@ private void downloadFile(boolean noResume) throws InterruptedException {
147
166
setStatus (Status .CONNECTING );
148
167
149
168
final File settingsFolder = BaseNoGui .getPlatform ().getSettingsFolder ();
150
- final String cacheFolder = Paths .get (settingsFolder .getPath (), "cache" ).toString ();
151
- final FileDownloaderCache fileDownloaderCache = FileDownloaderCache .getFileCached (cacheFolder , downloadUrl );
169
+ final String cacheFolder = Paths .get (settingsFolder .getPath (), "cache" )
170
+ .toString ();
171
+ final FileDownloaderCache fileDownloaderCache = FileDownloaderCache
172
+ .getFileCached (cacheFolder , downloadUrl );
152
173
153
174
if (!fileDownloaderCache .isChange ()) {
154
175
try {
155
- final Optional <File > fileFromCache =
156
- fileDownloaderCache .getFileFromCache ();
176
+ final Optional <File > fileFromCache = fileDownloaderCache
177
+ .getFileFromCache ();
157
178
if (fileFromCache .isPresent ()) {
158
179
FileUtils .copyFile (fileFromCache .get (), outputFile );
159
180
setStatus (Status .COMPLETE );
160
181
return ;
161
182
}
162
183
} catch (Exception e ) {
163
184
log .warn (
164
- "Cannot get the file from the cache, will be downloaded a new one " , e .getCause ());
185
+ "Cannot get the file from the cache, will be downloaded a new one " ,
186
+ e .getCause ());
165
187
}
166
188
}
167
189
@@ -175,14 +197,15 @@ private void downloadFile(boolean noResume) throws InterruptedException {
175
197
randomAccessOutputFile = new RandomAccessFile (outputFile , "rw" );
176
198
randomAccessOutputFile .seek (initialSize );
177
199
178
- final HttpURLConnection connection = new HttpConnectionManager (downloadUrl )
179
- .makeConnection ((c ) -> setDownloaded (0 ));
200
+ final HttpURLConnection connection = new HttpConnectionManager (
201
+ downloadUrl ) .makeConnection ((c ) -> setDownloaded (0 ));
180
202
final int resp = connection .getResponseCode ();
181
203
182
204
if (resp < 200 || resp >= 300 ) {
183
205
IOUtils .closeQuietly (randomAccessOutputFile );
184
206
Files .deleteIfExists (outputFile .toPath ());
185
- throw new IOException ("Received invalid http status code from server: " + resp );
207
+ throw new IOException (
208
+ "Received invalid http status code from server: " + resp );
186
209
}
187
210
188
211
// Check for valid content length.
@@ -218,6 +241,9 @@ private void downloadFile(boolean noResume) throws InterruptedException {
218
241
IOUtils .closeQuietly (randomAccessOutputFile );
219
242
fileDownloaderCache .fillCache (outputFile );
220
243
setStatus (Status .COMPLETE );
244
+ log .info ("Downloaded Successfully {}, file MD5: {}" , downloadUrl ,
245
+ this .getDownloadedContentMD5 ());
246
+
221
247
} catch (InterruptedException e ) {
222
248
setStatus (Status .CANCELLED );
223
249
// lets InterruptedException go up to the caller
0 commit comments