Skip to content

Commit 98fec72

Browse files
cmaglieFederico Fissore
authored and
Federico Fissore
committed
Use Downloader* helper classes on ContributionInstaller.
This rationalize and simplify a lot of code.
1 parent 0755c7c commit 98fec72

File tree

1 file changed

+16
-78
lines changed

1 file changed

+16
-78
lines changed

Diff for: arduino-core/src/cc/arduino/packages/contributions/ContributionInstaller.java

+16-78
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,27 @@
3636
import java.util.Iterator;
3737
import java.util.LinkedList;
3838
import java.util.List;
39-
import java.util.Observable;
40-
import java.util.Observer;
4139

4240
import processing.app.helpers.FileUtils;
4341
import cc.arduino.utils.ArchiveExtractor;
44-
import cc.arduino.utils.FileHash;
4542
import cc.arduino.utils.MultiStepProgress;
4643
import cc.arduino.utils.Progress;
47-
import cc.arduino.utils.network.FileDownloader;
4844

4945
public class ContributionInstaller {
5046

5147
private File stagingFolder;
5248
private ContributionsIndexer indexer;
49+
private DownloadableContributionsDownloader downloader;
5350

5451
public ContributionInstaller(ContributionsIndexer contributionsIndexer) {
5552
stagingFolder = contributionsIndexer.getStagingFolder();
5653
indexer = contributionsIndexer;
54+
downloader = new DownloadableContributionsDownloader(stagingFolder) {
55+
@Override
56+
protected void onProgress(Progress progress) {
57+
ContributionInstaller.this.onProgress(progress);
58+
};
59+
};
5760
}
5861

5962
public void install(ContributedPlatform platform) throws Exception {
@@ -80,14 +83,17 @@ public void install(ContributedPlatform platform) throws Exception {
8083
// Download all
8184
try {
8285
// Download platform
83-
download(platform, progress, _("Downloading boards definitions."));
86+
downloader.download(platform, progress,
87+
_("Downloading boards definitions."));
88+
progress.stepDone();
8489

8590
// Download tools
8691
int i = 1;
8792
for (ContributedTool tool : tools) {
8893
String msg = format(_("Downloading tools ({0}/{1})."), i, tools.size());
89-
download(tool.getDownloadableContribution(), progress, msg);
9094
i++;
95+
downloader.download(tool.getDownloadableContribution(), progress, msg);
96+
progress.stepDone();
9197
}
9298
} catch (InterruptedException e) {
9399
// Download interrupted... just exit
@@ -136,56 +142,6 @@ public void install(ContributedPlatform platform) throws Exception {
136142
onProgress(progress);
137143
}
138144

139-
public File download(DownloadableContribution contribution,
140-
final MultiStepProgress progress, final String statusText)
141-
throws Exception {
142-
URL url = new URL(contribution.getUrl());
143-
String path = url.getPath();
144-
String fileName = path.substring(path.lastIndexOf('/') + 1);
145-
final File outputFile = new File(stagingFolder, fileName);
146-
147-
// Ensure the existence of staging folder
148-
stagingFolder.mkdirs();
149-
150-
// Need to download or resume downloading?
151-
if (!outputFile.isFile() || (outputFile.length() < contribution.getSize())) {
152-
153-
// Use FileDownloader to retrieve the file
154-
FileDownloader downloader = new FileDownloader(url, outputFile);
155-
downloader.addObserver(new Observer() {
156-
@Override
157-
public void update(Observable o, Object arg) {
158-
FileDownloader me = (FileDownloader) o;
159-
String msg = "";
160-
if (me.getDownloadSize() != null) {
161-
long downloaded = me.getInitialSize() + me.getDownloaded() / 1000;
162-
long total = me.getInitialSize() + me.getDownloadSize() / 1000;
163-
msg = format(_("Downloaded {0}kb of {1}kb."), downloaded, total);
164-
}
165-
progress.setStatus(statusText + " " + msg);
166-
progress.setProgress(me.getProgress());
167-
onProgress(progress);
168-
}
169-
});
170-
downloader.download();
171-
if (!downloader.isCompleted())
172-
throw new Exception("Error dowloading " + url, downloader.getError());
173-
}
174-
progress.stepDone();
175-
176-
// Test checksum
177-
progress.setStatus(_("Verifying archive integrity..."));
178-
onProgress(progress);
179-
String checksum = contribution.getChecksum();
180-
String algo = checksum.split(":")[0];
181-
if (!FileHash.hash(outputFile, algo).equals(checksum))
182-
throw new Exception("CRC doesn't match. File is corrupted.");
183-
184-
contribution.setDownloaded(true);
185-
contribution.setDownloadedFile(outputFile);
186-
return outputFile;
187-
}
188-
189145
public void remove(ContributedPlatform platform) {
190146
FileUtils.recursiveDelete(platform.getInstalledFolder());
191147
platform.setInstalled(false);
@@ -214,36 +170,18 @@ public void remove(ContributedPlatform platform) {
214170
}
215171

216172
public void updateIndex() throws Exception {
217-
final MultiStepProgress progress = new MultiStepProgress(2);
173+
final MultiStepProgress progress = new MultiStepProgress(1);
218174
final String statusText = _("Downloading platforms index...");
219175

220176
URL url = new URL("http://arduino.cc/package_index.json");
221-
File tmpFile = File.createTempFile("package_index", ".json");
222-
FileDownloader downloader = new FileDownloader(url, tmpFile);
223-
downloader.addObserver(new Observer() {
224-
@Override
225-
public void update(Observable o, Object arg) {
226-
FileDownloader me = (FileDownloader) o;
227-
String msg = "";
228-
if (me.getDownloadSize() != null) {
229-
long downloaded = me.getInitialSize() + me.getDownloaded() / 1000;
230-
long total = me.getInitialSize() + me.getDownloadSize() / 1000;
231-
msg = format(_("Downloaded {0}kb of {1}kb."), downloaded, total);
232-
}
233-
progress.setStatus(statusText + " " + msg);
234-
progress.setProgress(me.getProgress());
235-
onProgress(progress);
236-
}
237-
});
238-
downloader.download();
239-
if (!downloader.isCompleted())
240-
throw new Exception("Error dowloading " + url, downloader.getError());
177+
File outputFile = indexer.getIndexFile();
178+
File tmpFile = new File(outputFile.getAbsolutePath() + ".tmp");
179+
downloader.download(url, tmpFile, progress, statusText);
241180
progress.stepDone();
242181

243182
// TODO: Check downloaded index
244183

245184
// Replace old index with the updated one
246-
File outputFile = indexer.getIndexFile();
247185
if (outputFile.exists())
248186
outputFile.delete();
249187
if (!tmpFile.renameTo(outputFile))

0 commit comments

Comments
 (0)