@@ -45,8 +45,9 @@ type HTTPDownloader struct {
45
45
Timeout int
46
46
MaxAttempts int
47
47
48
- Debug bool
49
- RoundTripper http.RoundTripper
48
+ Debug bool
49
+ RoundTripper http.RoundTripper
50
+ progressIndicator * ProgressIndicator
50
51
}
51
52
52
53
// SetProxy set the proxy for a http
@@ -153,14 +154,14 @@ func (h *HTTPDownloader) DownloadFile() error {
153
154
if h .Title == "" {
154
155
h .Title = "Downloading"
155
156
}
156
- writer : = & ProgressIndicator {
157
+ h . progressIndicator = & ProgressIndicator {
157
158
Title : h .Title ,
158
159
}
159
160
if showProgress {
160
161
if total , ok := resp .Header ["Content-Length" ]; ok && len (total ) > 0 {
161
162
fileLength , err := strconv .ParseInt (total [0 ], 10 , 64 )
162
163
if err == nil {
163
- writer .Total = float64 (fileLength )
164
+ h . progressIndicator .Total = float64 (fileLength )
164
165
}
165
166
}
166
167
}
@@ -176,14 +177,14 @@ func (h *HTTPDownloader) DownloadFile() error {
176
177
return err
177
178
}
178
179
179
- writer .Writer = out
180
+ h . progressIndicator .Writer = out
180
181
181
182
if showProgress {
182
- writer .Init ()
183
+ h . progressIndicator .Init ()
183
184
}
184
185
185
186
// Write the body to file
186
- _ , err = io .Copy (writer , resp .Body )
187
+ _ , err = io .Copy (h . progressIndicator , resp .Body )
187
188
return err
188
189
}
189
190
@@ -219,13 +220,16 @@ func DownloadFileWithMultipleThreadKeepParts(targetURL, targetFilePath string, t
219
220
}
220
221
start := unit * int64 (index )
221
222
222
- if downloadErr := DownloadWithContinue (targetURL , fmt .Sprintf ("%s-%d" , targetFilePath , index ), int64 (index ), start , end , showProgress ); downloadErr != nil {
223
+ downloader := & ContinueDownloader {}
224
+ if downloadErr := downloader .DownloadWithContinue (targetURL , fmt .Sprintf ("%s-%d" , targetFilePath , index ),
225
+ int64 (index ), start , end , showProgress ); downloadErr != nil {
223
226
fmt .Println (downloadErr )
224
227
}
225
228
}(i , & wg )
226
229
}
227
230
228
231
wg .Wait ()
232
+ ProgressIndicator {}.Close ()
229
233
230
234
// concat all these partial files
231
235
var f * os.File
@@ -251,33 +255,39 @@ func DownloadFileWithMultipleThreadKeepParts(targetURL, targetFilePath string, t
251
255
}
252
256
} else {
253
257
fmt .Println ("cannot download it using multiple threads, failed to one" )
254
- err = DownloadWithContinue (targetURL , targetFilePath , - 1 , 0 , 0 , true )
258
+ downloader := & ContinueDownloader {}
259
+ err = downloader .DownloadWithContinue (targetURL , targetFilePath , - 1 , 0 , 0 , true )
255
260
}
256
261
return
257
262
}
258
263
264
+ // ContinueDownloader is a downloader which support continuously download
265
+ type ContinueDownloader struct {
266
+ downloader * HTTPDownloader
267
+ }
268
+
259
269
// DownloadWithContinue downloads the files continuously
260
- func DownloadWithContinue (targetURL , output string , index , continueAt , end int64 , showProgress bool ) (err error ) {
261
- downloader := HTTPDownloader {
270
+ func ( c * ContinueDownloader ) DownloadWithContinue (targetURL , output string , index , continueAt , end int64 , showProgress bool ) (err error ) {
271
+ c . downloader = & HTTPDownloader {
262
272
TargetFilePath : output ,
263
273
URL : targetURL ,
264
274
ShowProgress : showProgress ,
265
275
}
266
276
if index >= 0 {
267
- downloader .Title = fmt .Sprintf ("Downloading part %d" , index )
277
+ c . downloader .Title = fmt .Sprintf ("Downloading part %d" , index )
268
278
}
269
279
270
280
if continueAt >= 0 {
271
- downloader .Header = make (map [string ]string , 1 )
281
+ c . downloader .Header = make (map [string ]string , 1 )
272
282
273
283
if end > continueAt {
274
- downloader .Header ["Range" ] = fmt .Sprintf ("bytes=%d-%d" , continueAt , end )
284
+ c . downloader .Header ["Range" ] = fmt .Sprintf ("bytes=%d-%d" , continueAt , end )
275
285
} else {
276
- downloader .Header ["Range" ] = fmt .Sprintf ("bytes=%d-" , continueAt )
286
+ c . downloader .Header ["Range" ] = fmt .Sprintf ("bytes=%d-" , continueAt )
277
287
}
278
288
}
279
289
280
- if err = downloader .DownloadFile (); err != nil {
290
+ if err = c . downloader .DownloadFile (); err != nil {
281
291
err = fmt .Errorf ("cannot download from %s, error: %v" , targetURL , err )
282
292
}
283
293
return
0 commit comments