From e680866d543ebcafe334bc717f2f6ca6aab8aa34 Mon Sep 17 00:00:00 2001 From: Simon Ding Date: Mon, 31 Mar 2025 16:08:13 +0800 Subject: [PATCH] chore: buildin downloader updates --- engine/resources.go | 2 +- pkg/buildin/torrent.go | 48 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/engine/resources.go b/engine/resources.go index 1b6af05..679f06f 100644 --- a/engine/resources.go +++ b/engine/resources.go @@ -122,7 +122,7 @@ func (c *Engine) DownloadMovie(m *ent.Media, r1 torznab.Result) (*string, error) func (c *Engine) downloadTorrent(m *ent.Media, r1 torznab.Result, seasonNum int, episodeNums ...int) (*string, error) { trc, dlc, err := c.GetDownloadClient() if err != nil { - return nil, errors.Wrap(err, "connect transmission") + return nil, errors.Wrap(err, "get download client") } //check space available diff --git a/pkg/buildin/torrent.go b/pkg/buildin/torrent.go index ff92e00..4018c71 100644 --- a/pkg/buildin/torrent.go +++ b/pkg/buildin/torrent.go @@ -2,28 +2,33 @@ package buildin import ( "fmt" - "github.com/anacrolix/torrent" - "github.com/anacrolix/torrent/metainfo" - "github.com/pkg/errors" "io/fs" "net/http" "os" + "path/filepath" + "polaris/log" "polaris/pkg" "strings" + + "github.com/anacrolix/torrent" + "github.com/anacrolix/torrent/metainfo" + "github.com/pkg/errors" ) func NewDownloader(downloadDir string) (*Downloader, error) { cfg := torrent.NewDefaultClientConfig() cfg.DataDir = downloadDir + cfg.ListenPort = 51243 t, err := torrent.NewClient(cfg) if err != nil { return nil, errors.Wrapf(err, "create torrent client") } - return &Downloader{cl: t}, nil + return &Downloader{cl: t, dir: downloadDir}, nil } type Downloader struct { - cl *torrent.Client + cl *torrent.Client + dir string } func (d *Downloader) GetAll() ([]pkg.Torrent, error) { @@ -51,6 +56,7 @@ func (d *Downloader) Download(link, hash, dir string) (pkg.Torrent, error) { t: t, cl: d.cl, hash: hash, + dir: d.dir, }, nil } client := &http.Client{ @@ -82,6 +88,27 @@ func (d *Downloader) Download(link, hash, dir string) (pkg.Torrent, error) { t: t, cl: d.cl, hash: hash, + dir: d.dir, + }, nil +} + +func NewTorrentFromHash(hash string, downloadDir string) (*Torrent, error) { + cl, err := NewDownloader(downloadDir) + if err != nil { + return nil, errors.Wrap(err, "create downloader") + } + ttt := cl.cl.Torrents() + log.Infof("all torrents: %+v", ttt) + t, _ := cl.cl.AddTorrentInfoHash(metainfo.NewHashFromHex(hash)) + // if new { + // return nil, fmt.Errorf("torrent not found") + // } + <-t.GotInfo() + return &Torrent{ + t: t, + cl: cl.cl, + hash: hash, + dir: downloadDir, }, nil } @@ -89,6 +116,7 @@ type Torrent struct { t *torrent.Torrent cl *torrent.Client hash string + dir string } func (t *Torrent) Name() (string, error) { @@ -120,8 +148,14 @@ func (t *Torrent) Start() error { return nil } -// TODO delete local data func (t *Torrent) Remove() error { + files := t.t.Files() + for _, file := range files { + name := file.Path() + if err := os.RemoveAll(filepath.Join(t.dir, name)); err != nil { + return errors.Errorf("remove file (%s) error: %v", file.Path(), err) + } + } t.t.Drop() return nil } @@ -149,7 +183,7 @@ func (t *Torrent) WalkFunc() func(fn func(path string, info fs.FileInfo) error) return func(fn func(path string, info fs.FileInfo) error) error { for _, file := range files { - name := file.Path() + name := filepath.Join(t.dir, file.Path()) info, err := os.Stat(name) if err != nil { return err