diff --git a/cmd/chunk/download_progress.go b/cmd/chunk/download_progress.go index 1c5a57c..dfb0221 100644 --- a/cmd/chunk/download_progress.go +++ b/cmd/chunk/download_progress.go @@ -70,11 +70,12 @@ func (p *downloadProgress) update(d chunk.DownloadStatus) { func newProgress() *downloadProgress { ctx, cancel := context.WithCancel(context.Background()) bar := downloadProgress{make(map[string]file), time.Now(), cancel} - tick := time.Tick(1 * time.Second) + t := time.NewTicker(time.Second) go func() { + defer t.Stop() for { select { - case <-tick: + case <-t.C: fmt.Printf("\r%s", bar.String()) case <-ctx.Done(): return diff --git a/cmd/chunk/main.go b/cmd/chunk/main.go index 763bca8..f547516 100644 --- a/cmd/chunk/main.go +++ b/cmd/chunk/main.go @@ -39,18 +39,18 @@ var rootCmd = &cobra.Command{ if err := checkOutputDir(); err != nil { return fmt.Errorf("error checking %s directory: %w", outputDir, err) } - chunk := chunk.DefaultDownloader() - chunk.OutputDir = outputDir - chunk.Timeout = timeoutChunk - chunk.ConcurrencyPerServer = concurrencyPerServer - chunk.MaxRetries = maxRetriesChunk - chunk.WaitRetry = waitBetweenRetries - chunk.ChunkSize = chunkSize - chunk.RestartDownloads = restartDownloads - chunk.ProgressDir = progressDir - chunk.UserAgent = userAgent + dl := chunk.DefaultDownloader() + dl.OutputDir = outputDir + dl.Timeout = timeoutChunk + dl.ConcurrencyPerServer = concurrencyPerServer + dl.MaxRetries = maxRetriesChunk + dl.WaitRetry = waitBetweenRetries + dl.ChunkSize = chunkSize + dl.RestartDownloads = restartDownloads + dl.ProgressDir = progressDir + dl.UserAgent = userAgent prog := newProgress() - for status := range chunk.Download(args...) { + for status := range dl.Download(args...) { if status.Error != nil { log.Fatal(status.Error) } diff --git a/downloader.go b/downloader.go index 19fa98d..b94674e 100644 --- a/downloader.go +++ b/downloader.go @@ -196,9 +196,8 @@ func (d *Downloader) downloadChunkWithTimeout(usrCtx context.Context, u string, } func (d *Downloader) getDownloadSize(ctx context.Context, u string) (int64, error) { - ch := make(chan *http.Response, 1) - defer close(ch) var n uint + var resp *http.Response for { if err := ctx.Err(); err != nil { return 0, err @@ -210,7 +209,7 @@ func (d *Downloader) getDownloadSize(ctx context.Context, u string) (int64, erro if d.UserAgent != "" { req.Header.Add("User-Agent", d.UserAgent) } - resp, err := d.Client.Do(req) + resp, err = d.Client.Do(req) if err != nil { if ctx.Err() != nil { return 0, ctx.Err() @@ -249,10 +248,8 @@ func (d *Downloader) getDownloadSize(ctx context.Context, u string) (int64, erro continue } } - ch <- resp break } - resp := <-ch if resp.ContentLength <= 0 { r := strings.TrimSpace(resp.Header.Get("Content-Range")) if r == "" {