36
36
import java .util .Iterator ;
37
37
import java .util .LinkedList ;
38
38
import java .util .List ;
39
- import java .util .Observable ;
40
- import java .util .Observer ;
41
39
42
40
import processing .app .helpers .FileUtils ;
43
41
import cc .arduino .utils .ArchiveExtractor ;
44
- import cc .arduino .utils .FileHash ;
45
42
import cc .arduino .utils .MultiStepProgress ;
46
43
import cc .arduino .utils .Progress ;
47
- import cc .arduino .utils .network .FileDownloader ;
48
44
49
45
public class ContributionInstaller {
50
46
51
47
private File stagingFolder ;
52
48
private ContributionsIndexer indexer ;
49
+ private DownloadableContributionsDownloader downloader ;
53
50
54
51
public ContributionInstaller (ContributionsIndexer contributionsIndexer ) {
55
52
stagingFolder = contributionsIndexer .getStagingFolder ();
56
53
indexer = contributionsIndexer ;
54
+ downloader = new DownloadableContributionsDownloader (stagingFolder ) {
55
+ @ Override
56
+ protected void onProgress (Progress progress ) {
57
+ ContributionInstaller .this .onProgress (progress );
58
+ };
59
+ };
57
60
}
58
61
59
62
public void install (ContributedPlatform platform ) throws Exception {
@@ -80,14 +83,17 @@ public void install(ContributedPlatform platform) throws Exception {
80
83
// Download all
81
84
try {
82
85
// Download platform
83
- download (platform , progress , _ ("Downloading boards definitions." ));
86
+ downloader .download (platform , progress ,
87
+ _ ("Downloading boards definitions." ));
88
+ progress .stepDone ();
84
89
85
90
// Download tools
86
91
int i = 1 ;
87
92
for (ContributedTool tool : tools ) {
88
93
String msg = format (_ ("Downloading tools ({0}/{1})." ), i , tools .size ());
89
- download (tool .getDownloadableContribution (), progress , msg );
90
94
i ++;
95
+ downloader .download (tool .getDownloadableContribution (), progress , msg );
96
+ progress .stepDone ();
91
97
}
92
98
} catch (InterruptedException e ) {
93
99
// Download interrupted... just exit
@@ -136,56 +142,6 @@ public void install(ContributedPlatform platform) throws Exception {
136
142
onProgress (progress );
137
143
}
138
144
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
-
189
145
public void remove (ContributedPlatform platform ) {
190
146
FileUtils .recursiveDelete (platform .getInstalledFolder ());
191
147
platform .setInstalled (false );
@@ -214,36 +170,18 @@ public void remove(ContributedPlatform platform) {
214
170
}
215
171
216
172
public void updateIndex () throws Exception {
217
- final MultiStepProgress progress = new MultiStepProgress (2 );
173
+ final MultiStepProgress progress = new MultiStepProgress (1 );
218
174
final String statusText = _ ("Downloading platforms index..." );
219
175
220
176
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 );
241
180
progress .stepDone ();
242
181
243
182
// TODO: Check downloaded index
244
183
245
184
// Replace old index with the updated one
246
- File outputFile = indexer .getIndexFile ();
247
185
if (outputFile .exists ())
248
186
outputFile .delete ();
249
187
if (!tmpFile .renameTo (outputFile ))
0 commit comments