From 4690107e323a0f16d6c74af04930dba57b103909 Mon Sep 17 00:00:00 2001 From: Levi Date: Tue, 31 Oct 2023 10:16:28 +0800 Subject: [PATCH] fix: gopeed cli BT download (#258) --- cmd/gopeed/main.go | 15 ++++++++++++++- internal/protocol/bt/fetcher.go | 11 +++++++++-- pkg/download/downloader.go | 9 ++++----- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/cmd/gopeed/main.go b/cmd/gopeed/main.go index 5c017a9a..327a6217 100644 --- a/cmd/gopeed/main.go +++ b/cmd/gopeed/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "github.com/GopeedLab/gopeed/internal/fetcher" "github.com/GopeedLab/gopeed/pkg/base" "github.com/GopeedLab/gopeed/pkg/download" "github.com/GopeedLab/gopeed/pkg/protocol/http" @@ -47,16 +48,28 @@ func main() { if err != nil { panic(err) } + printProgress(emptyTask, "downloading...") wg.Wait() } var ( lastLineLen = 0 sb = new(strings.Builder) + emptyTask = &download.Task{ + Progress: &download.Progress{}, + Meta: &fetcher.FetcherMeta{ + Res: &base.Resource{}, + }, + } ) func printProgress(task *download.Task, title string) { - rate := float64(task.Progress.Downloaded) / float64(task.Meta.Res.Size) + var rate float64 + if task.Meta.Res.Size <= 0 { + rate = 0 + } else { + rate = float64(task.Progress.Downloaded) / float64(task.Meta.Res.Size) + } completeWidth := int(progressWidth * rate) speed := util.ByteFmt(task.Progress.Speed) totalSize := util.ByteFmt(task.Meta.Res.Size) diff --git a/internal/protocol/bt/fetcher.go b/internal/protocol/bt/fetcher.go index 8a804cab..43a9beb8 100644 --- a/internal/protocol/bt/fetcher.go +++ b/internal/protocol/bt/fetcher.go @@ -87,7 +87,6 @@ func (f *Fetcher) Create(opts *base.Options) (err error) { if f.meta.Res != nil { torrentDirMap[f.meta.Res.Hash] = f.meta.FolderPath() } - f.progress = make(fetcher.Progress, len(f.meta.Opts.SelectFiles)) return nil } @@ -101,6 +100,14 @@ func (f *Fetcher) Start() (err error) { ft.setTorrentDir(f.meta.FolderPath()) } files := f.torrent.Files() + // If the user does not specify the file to download, all files will be downloaded by default + if len(f.meta.Opts.SelectFiles) == 0 { + f.meta.Opts.SelectFiles = make([]int, len(files)) + for i := range files { + f.meta.Opts.SelectFiles[i] = i + } + } + f.progress = make(fetcher.Progress, len(f.meta.Opts.SelectFiles)) if len(f.meta.Opts.SelectFiles) == len(files) { f.torrent.DownloadAll() } else { @@ -134,7 +141,7 @@ func (f *Fetcher) safeDrop() { func (f *Fetcher) Wait() (err error) { for { - if f.torrentReady.Load() { + if f.torrentReady.Load() && len(f.meta.Opts.SelectFiles) > 0 { done := true for _, selectIndex := range f.meta.Opts.SelectFiles { file := f.torrent.Files()[selectIndex] diff --git a/pkg/download/downloader.go b/pkg/download/downloader.go index cd59f95b..c2380e43 100644 --- a/pkg/download/downloader.go +++ b/pkg/download/downloader.go @@ -848,12 +848,11 @@ func (b *boot) Listener(listener Listener) *boot { } func (b *boot) Create(opts *base.Options) (string, error) { - rr, err := b.Resolve() - if err != nil { - return "", err - } defaultDownloader.Listener(b.listener) - return defaultDownloader.Create(rr.ID, opts) + return defaultDownloader.CreateDirect(&base.Request{ + URL: b.url, + Extra: b.extra, + }, opts) } func Boot() *boot {