feat: support reload qbit tasks

This commit is contained in:
Simon Ding
2024-10-04 10:45:31 +08:00
parent 579b010d13
commit 7dfa4eafc4
4 changed files with 50 additions and 25 deletions

View File

@@ -10,6 +10,7 @@ type Torrent interface {
Exists() bool
SeedRatio() (float64, error)
GetHash() string
Reload() error
}
type Downloader interface {
@@ -17,5 +18,3 @@ type Downloader interface {
Download(link, dir string) (Torrent, error)
}
type Storage interface {
}

View File

@@ -174,3 +174,15 @@ func (t *Torrent) SeedRatio() (float64, error) {
}
return qb.Ratio, nil
}
func (t *Torrent) Reload() error {
c, err := NewClient(t.URL, t.User, t.Password)
if err != nil {
return err
}
t.c = c.c
if !t.Exists() {
return errors.Errorf("torrent not exists: %v", t.Hash)
}
return nil
}

View File

@@ -105,12 +105,15 @@ type Torrent struct {
Config
}
func (t *Torrent) reloadClient() error {
func (t *Torrent) Reload() error {
c, err := NewClient(t.Config)
if err != nil {
return err
}
t.c = c.c
if !t.Exists() {
return errors.Errorf("torrent not exists: %v", t.Hash)
}
return nil
}
@@ -211,18 +214,3 @@ func (t *Torrent) Save() string {
func (t *Torrent) GetHash() string {
return t.Hash
}
func ReloadTorrent(s string) (*Torrent, error) {
var torrent = Torrent{}
err := json.Unmarshal([]byte(s), &torrent)
if err != nil {
return nil, err
}
err = torrent.reloadClient()
if err != nil {
return nil, errors.Wrap(err, "reload client")
}
return &torrent, nil
}

View File

@@ -1,6 +1,7 @@
package core
import (
"encoding/json"
"polaris/db"
"polaris/ent"
"polaris/ent/downloadclients"
@@ -51,19 +52,44 @@ func (c *Client) Init() {
func (c *Client) reloadTasks() {
allTasks := c.db.GetRunningHistories()
for _, t := range allTasks {
torrent, err := transmission.ReloadTorrent(t.Saved)
if err != nil {
log.Errorf("relaod task %s failed: %v", t.SourceTitle, err)
var torrent pkg.Torrent
if tt, err := c.reloadTransmiision(t.Saved); err == nil {
torrent = tt
log.Infof("load transmission task: %v", t.Saved)
} else if tt, err := c.reloadQbit(t.Saved); err == nil {
torrent = tt
log.Infof("load qbit task: %v", t.Saved)
} else {
log.Warnf("load task fail: %v", t.Saved)
continue
}
if !torrent.Exists() { //只要种子还存在于客户端中,就重新加载,有可能是还在做种中
continue
}
log.Infof("reloading task: %d %s", t.ID, t.SourceTitle)
c.tasks[t.ID] = &Task{Torrent: torrent}
}
}
func (c *Client) reloadTransmiision(s string) (pkg.Torrent, error) {
var t transmission.Torrent
if err := json.Unmarshal([]byte(s), &t); err != nil {
return nil, err
}
if err := t.Reload(); err != nil {
return nil, err
}
return &t, nil
}
func (c *Client) reloadQbit(s string) (pkg.Torrent, error) {
var t qbittorrent.Torrent
if err := json.Unmarshal([]byte(s), &t); err != nil {
return nil, err
}
if err := t.Reload(); err != nil {
return nil, err
}
return &t, nil
}
func (c *Client) GetDownloadClient() (pkg.Downloader, *ent.DownloadClients, error) {
downloaders := c.db.GetAllDonloadClients()
for _, d := range downloaders {