Skip to content

Commit f21584b

Browse files
author
Roberto Sora
committed
Add lazy MD5 calculation for downloaded file by FileDownloader
1 parent 18d6af5 commit f21584b

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

Diff for: arduino-core/src/cc/arduino/utils/network/FileDownloader.java

+35-9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
package cc.arduino.utils.network;
3131

32+
import cc.arduino.utils.FileHash;
3233
import org.apache.commons.compress.utils.IOUtils;
3334
import org.slf4j.Logger;
3435
import org.slf4j.LoggerFactory;
@@ -44,6 +45,7 @@
4445
import java.net.URL;
4546
import java.nio.file.Files;
4647
import java.nio.file.Paths;
48+
import java.security.NoSuchAlgorithmException;
4749
import java.util.Observable;
4850
import java.util.Optional;
4951

@@ -65,6 +67,8 @@ public enum Status {
6567
private long downloaded;
6668
private final URL downloadUrl;
6769

70+
private String downloadedContentMD5;
71+
6872
private final File outputFile;
6973
private InputStream stream = null;
7074
private Exception error;
@@ -74,6 +78,20 @@ public FileDownloader(URL url, File file) {
7478
outputFile = file;
7579
downloaded = 0;
7680
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;
7795
}
7896

7997
public long getInitialSize() {
@@ -132,7 +150,8 @@ public void download(boolean noResume) throws InterruptedException {
132150

133151
private void saveLocalFile() {
134152
try {
135-
Files.write(outputFile.toPath(), Files.readAllBytes(Paths.get(downloadUrl.getPath())));
153+
Files.write(outputFile.toPath(),
154+
Files.readAllBytes(Paths.get(downloadUrl.getPath())));
136155
setStatus(Status.COMPLETE);
137156
} catch (Exception e) {
138157
setStatus(Status.ERROR);
@@ -147,21 +166,24 @@ private void downloadFile(boolean noResume) throws InterruptedException {
147166
setStatus(Status.CONNECTING);
148167

149168
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);
152173

153174
if (!fileDownloaderCache.isChange()) {
154175
try {
155-
final Optional<File> fileFromCache =
156-
fileDownloaderCache.getFileFromCache();
176+
final Optional<File> fileFromCache = fileDownloaderCache
177+
.getFileFromCache();
157178
if (fileFromCache.isPresent()) {
158179
FileUtils.copyFile(fileFromCache.get(), outputFile);
159180
setStatus(Status.COMPLETE);
160181
return;
161182
}
162183
} catch (Exception e) {
163184
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());
165187
}
166188
}
167189

@@ -175,14 +197,15 @@ private void downloadFile(boolean noResume) throws InterruptedException {
175197
randomAccessOutputFile = new RandomAccessFile(outputFile, "rw");
176198
randomAccessOutputFile.seek(initialSize);
177199

178-
final HttpURLConnection connection = new HttpConnectionManager(downloadUrl)
179-
.makeConnection((c) -> setDownloaded(0));
200+
final HttpURLConnection connection = new HttpConnectionManager(
201+
downloadUrl).makeConnection((c) -> setDownloaded(0));
180202
final int resp = connection.getResponseCode();
181203

182204
if (resp < 200 || resp >= 300) {
183205
IOUtils.closeQuietly(randomAccessOutputFile);
184206
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);
186209
}
187210

188211
// Check for valid content length.
@@ -218,6 +241,9 @@ private void downloadFile(boolean noResume) throws InterruptedException {
218241
IOUtils.closeQuietly(randomAccessOutputFile);
219242
fileDownloaderCache.fillCache(outputFile);
220243
setStatus(Status.COMPLETE);
244+
log.info("Downloaded Successfully {}, file MD5: {}", downloadUrl,
245+
this.getDownloadedContentMD5());
246+
221247
} catch (InterruptedException e) {
222248
setStatus(Status.CANCELLED);
223249
// lets InterruptedException go up to the caller

0 commit comments

Comments
 (0)