Skip to content

Commit

Permalink
fix: gopeed cli BT download (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie authored Oct 31, 2023
1 parent 74b08b3 commit 4690107
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
15 changes: 14 additions & 1 deletion cmd/gopeed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions internal/protocol/bt/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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]
Expand Down
9 changes: 4 additions & 5 deletions pkg/download/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 4690107

Please sign in to comment.