@@ -34,18 +34,12 @@ type (
3434 // Download file info.
3535 * Info
3636
37- // Progress func.
38- ProgressFunc
39-
4037 // URL to download.
4138 URL string
4239
4340 // File destination.
4441 Dest string
4542
46- // Progress interval in ms.
47- Interval int
48-
4943 // Split file into chunks by ChunkSize in bytes.
5044 ChunkSize int64
5145
@@ -58,6 +52,12 @@ type (
5852 // Max chunks to download at same time.
5953 Concurrency int
6054
55+ // Progress...
56+ Progress * Progress
57+
58+ // Progress interval in ms.
59+ Interval int
60+
6161 // Download file chunks.
6262 chunks []* Chunk
6363
6666
6767 // Is the URL redirected to a different location.
6868 redirected bool
69-
70- // Progress...
71- progress * Progress
7269 }
7370)
7471
@@ -95,9 +92,6 @@ func (d *Download) Init() error {
9592 },
9693 }
9794
98- // Init progress.
99- d .progress = & Progress {}
100-
10195 // Set default interval.
10296 if d .Interval == 0 {
10397 d .Interval = 20
@@ -108,6 +102,16 @@ func (d *Download) Init() error {
108102 return err
109103 }
110104
105+ // Set default progress.
106+ if d .Progress == nil {
107+
108+ d .Progress = & Progress {
109+ startedAt : time .Now (),
110+ Interval : d .Interval ,
111+ TotalSize : d .Info .Length ,
112+ }
113+ }
114+
111115 // Partial content not supported 😢!
112116 if d .Info .Rangeable == false || d .Info .Length == 0 {
113117 return nil
@@ -177,7 +181,7 @@ func (d *Download) Init() error {
177181 d .chunks = append (d .chunks , & Chunk {
178182 Start : startRange ,
179183 End : endRange ,
180- Progress : d .progress ,
184+ Progress : d .Progress ,
181185 Done : make (chan struct {}),
182186 })
183187 }
@@ -187,6 +191,7 @@ func (d *Download) Init() error {
187191
188192// Start downloading.
189193func (d * Download ) Start () (err error ) {
194+
190195 // Create a new temp dir for this download.
191196 temp , err := ioutil .TempDir ("" , "GotChunks" )
192197 if err != nil {
@@ -196,8 +201,9 @@ func (d *Download) Start() (err error) {
196201
197202 ctx , cancel := context .WithCancel (context .TODO ())
198203 defer cancel ()
204+
199205 // Run progress func.
200- go d .progress .Run (ctx , d )
206+ go d .Progress .Run (ctx , d )
201207
202208 // Partial content not supported,
203209 // just download the file in one chunk.
@@ -212,7 +218,7 @@ func (d *Download) Start() (err error) {
212218 defer file .Close ()
213219
214220 chunk := & Chunk {
215- Progress : d .progress ,
221+ Progress : d .Progress ,
216222 }
217223
218224 return chunk .Download (d .URL , d .client , file )
@@ -235,9 +241,11 @@ func (d *Download) Start() (err error) {
235241 return err
236242 }
237243
238- if d .ProgressFunc != nil {
239- d .ProgressFunc (d .progress .Size , d .Info .Length , d )
244+ // Update progress output after chunks finished.
245+ if d .Progress .ProgressFunc != nil {
246+ d .Progress .ProgressFunc (d .Progress , d )
240247 }
248+
241249 return nil
242250}
243251
@@ -283,6 +291,7 @@ func (d *Download) merge(ctx context.Context) error {
283291 defer file .Close ()
284292
285293 for i := range d .chunks {
294+
286295 select {
287296 case <- d .chunks [i ].Done :
288297 case <- ctx .Done ():
@@ -309,17 +318,24 @@ func (d *Download) merge(ctx context.Context) error {
309318
310319// Download chunks
311320func (d * Download ) dl (ctx context.Context , temp string ) error {
321+
312322 eg , ctx := errgroup .WithContext (ctx )
323+
313324 // Concurrency limit.
314325 max := make (chan int , d .Concurrency )
315326
316327 for i := 0 ; i < len (d .chunks ); i ++ {
328+
317329 max <- 1
318330 i := i
331+
319332 eg .Go (func () error {
333+
320334 defer func () {
321335 <- max
322336 }()
337+
338+ // Create chunk in temp dir.
323339 chunk , err := os .Create (filepath .Join (temp , fmt .Sprintf ("chunk-%d" , i )))
324340
325341 if err != nil {
0 commit comments