From 2e5fd3017c1fb0809a82bc8c5c42999481136525 Mon Sep 17 00:00:00 2001 From: liwei Date: Mon, 6 Feb 2023 22:21:25 +0800 Subject: [PATCH] fix: bt fetcher restore --- internal/protocol/bt/fetcher.go | 8 ++++++-- pkg/download/downloader.go | 4 +++- pkg/rest/util/util.go | 4 +++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/internal/protocol/bt/fetcher.go b/internal/protocol/bt/fetcher.go index 9db89ca2..f22a1dc8 100644 --- a/internal/protocol/bt/fetcher.go +++ b/internal/protocol/bt/fetcher.go @@ -110,7 +110,7 @@ func (f *Fetcher) Create(opts *base.Options) (err error) { } func (f *Fetcher) Start() (err error) { - if f.torrent == nil { + if !f.ready.Load() { if err = f.addTorrent(f.meta.Req.URL); err != nil { return } @@ -236,5 +236,9 @@ func (fb *FetcherBuilder) Store(f fetcher.Fetcher) (data any, err error) { } func (fb *FetcherBuilder) Restore() (v any, f func(meta *fetcher.FetcherMeta, v any) fetcher.Fetcher) { - return nil, nil + return nil, func(meta *fetcher.FetcherMeta, v any) fetcher.Fetcher { + return &Fetcher{ + meta: meta, + } + } } diff --git a/pkg/download/downloader.go b/pkg/download/downloader.go index 2cabc2b9..0dcd8615 100644 --- a/pkg/download/downloader.go +++ b/pkg/download/downloader.go @@ -281,6 +281,7 @@ func (d *Downloader) Continue(id string) (err error) { defer task.lock.Unlock() if task.Status == base.DownloadStatusPause || task.Status == base.DownloadStatusError { task.Status = base.DownloadStatusRunning + task.Progress.Speed = 0 err = d.restoreFetcher(task) if err != nil { return @@ -418,6 +419,7 @@ func (d *Downloader) getProtocolConfig(name string, v any) (bool, error) { return true, nil } +// wait task done func (d *Downloader) watch(task *Task) { err := task.fetcher.Wait() if err != nil { @@ -456,8 +458,8 @@ func (d *Downloader) restoreFetcher(task *Task) error { if err != nil { return err } - task.fetcher = f(task.Meta, v) } + task.fetcher = f(task.Meta, v) return nil }() if err != nil { diff --git a/pkg/rest/util/util.go b/pkg/rest/util/util.go index 8097df64..b9dbecfd 100644 --- a/pkg/rest/util/util.go +++ b/pkg/rest/util/util.go @@ -21,5 +21,7 @@ func WriteJsonOk(w http.ResponseWriter, v any) { func WriteJson(w http.ResponseWriter, code int, v any) { w.Header().Set("Content-Type", "application/json; charset=utf-8") w.WriteHeader(code) - json.NewEncoder(w).Encode(v) + if v != nil { + json.NewEncoder(w).Encode(v) + } }