diff --git a/db/db.go b/db/db.go index e18c162..b1077ef 100644 --- a/db/db.go +++ b/db/db.go @@ -483,7 +483,7 @@ func (c *Client) SaveHistoryRecord(h ent.History) (*ent.History, error) { } return c.ent.History.Create().SetMediaID(h.MediaID).SetEpisodeID(h.EpisodeID).SetDate(time.Now()). SetStatus(h.Status).SetTargetDir(h.TargetDir).SetSourceTitle(h.SourceTitle).SetIndexerID(h.IndexerID). - SetDownloadClientID(h.DownloadClientID).SetSize(h.Size).SetSaved(h.Saved).Save(context.TODO()) + SetDownloadClientID(h.DownloadClientID).SetSize(h.Size).SetSaved(h.Saved).SetLink(h.Link).Save(context.TODO()) } func (c *Client) SetHistoryStatus(id int, status history.Status) error { diff --git a/ent/history.go b/ent/history.go index 464cdf7..146c141 100644 --- a/ent/history.go +++ b/ent/history.go @@ -37,7 +37,7 @@ type History struct { Link string `json:"link,omitempty"` // Status holds the value of the "status" field. Status history.Status `json:"status,omitempty"` - // Saved holds the value of the "saved" field. + // deprecated Saved string `json:"saved,omitempty"` selectValues sql.SelectValues } diff --git a/ent/schema/history.go b/ent/schema/history.go index 8e1c10f..d0754ca 100644 --- a/ent/schema/history.go +++ b/ent/schema/history.go @@ -23,7 +23,7 @@ func (History) Fields() []ent.Field { field.Int("indexer_id").Optional(), field.String("link").Optional(), //should be magnet link field.Enum("status").Values("running", "success", "fail", "uploading", "seeding"), - field.String("saved").Optional(), + field.String("saved").Optional().Comment("deprecated"), //deprecated } } diff --git a/pkg/doc.go b/pkg/doc.go index fe72191..953eae0 100644 --- a/pkg/doc.go +++ b/pkg/doc.go @@ -6,11 +6,11 @@ type Torrent interface { Stop() error Start() error Remove() error - Save() string + //Save() string Exists() bool SeedRatio() (float64, error) GetHash() string - Reload() error + //Reload() error } type Downloader interface { diff --git a/pkg/qbittorrent/qbittorrent.go b/pkg/qbittorrent/qbittorrent.go index b13e6ab..5bc9e71 100644 --- a/pkg/qbittorrent/qbittorrent.go +++ b/pkg/qbittorrent/qbittorrent.go @@ -47,8 +47,8 @@ func (c *Client) GetAll() ([]pkg.Torrent, error) { for _, t := range tt { t1 := &Torrent{ c: c.c, - Hash: t.Hash, - Info: c.Info, + hash: t.Hash, + //Info: c.Info, } res = append(res, t1) } @@ -64,27 +64,46 @@ func (c *Client) Download(link, dir string) (pkg.Torrent, error) { if err != nil { return nil, errors.Wrap(err, "qbt download") } - return &Torrent{Hash: hash, c: c.c, Info: c.Info}, nil + return &Torrent{hash: hash, c: c.c, }, nil } + +func NewTorrent(info Info, magnet string) (*Torrent, error) { + c, err := NewClient(info.URL, info.User, info.Password) + if err != nil { + return nil, err + } + hash, err := utils.MagnetHash(magnet) + if err != nil { + return nil, err + } + t := &Torrent{ + c: c.c, + hash: hash, + } + if !t.Exists() { + return nil, errors.Errorf("torrent not exist: %v", magnet) + } + return t, nil +} type Torrent struct { c *qbt.Client - Hash string - Info + hash string + //info Info } func (t *Torrent) GetHash() string { - return t.Hash + return t.hash } func (t *Torrent) getTorrent() (*qbt.TorrentInfo, error) { - all, err := t.c.Torrents(qbt.TorrentsOptions{Hashes: []string{t.Hash}}) + all, err := t.c.Torrents(qbt.TorrentsOptions{Hashes: []string{t.hash}}) if err != nil { return nil, err } if len(all) == 0 { - return nil, fmt.Errorf("no such torrent: %v", t.Hash) + return nil, fmt.Errorf("no such torrent: %v", t.hash) } return &all[0], nil } @@ -115,11 +134,11 @@ func (t *Torrent) Progress() (int, error) { } func (t *Torrent) Stop() error { - return t.c.Pause([]string{t.Hash}) + return t.c.Pause([]string{t.hash}) } func (t *Torrent) Start() error { - ok, err := t.c.Resume([]string{t.Hash}) + ok, err := t.c.Resume([]string{t.hash}) if err != nil { return err } @@ -130,7 +149,7 @@ func (t *Torrent) Start() error { } func (t *Torrent) Remove() error { - ok, err := t.c.Delete([]string{t.Hash}, true) + ok, err := t.c.Delete([]string{t.hash}, true) if err != nil { return err } @@ -157,15 +176,3 @@ 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 -} diff --git a/pkg/transmission/transmission.go b/pkg/transmission/transmission.go index 857a643..82942aa 100644 --- a/pkg/transmission/transmission.go +++ b/pkg/transmission/transmission.go @@ -2,7 +2,6 @@ package transmission import ( "context" - "encoding/json" "fmt" "net/url" "polaris/log" @@ -53,9 +52,9 @@ func (c *Client) GetAll() ([]pkg.Torrent, error) { var torrents []pkg.Torrent for _, t := range all { torrents = append(torrents, &Torrent{ - Hash: *t.HashString, - c: c.c, - Config: c.cfg, + hash: *t.HashString, + c: c.c, + //cfg: c.cfg, }) } return torrents, nil @@ -74,33 +73,42 @@ func (c *Client) Download(link, dir string) (pkg.Torrent, error) { log.Debugf("get torrent info: %+v", t) return &Torrent{ - Hash: hash, - c: c.c, - Config: c.cfg, + hash: hash, + c: c.c, + //cfg: c.cfg, }, err } +func NewTorrent(cfg Config, magnet string) (*Torrent, error) { + c, err := NewClient(cfg) + if err != nil { + return nil, err + } + hash, err := utils.MagnetHash(magnet) + if err != nil { + return nil, err + } + + t := &Torrent{ + c: c.c, + hash: hash, + //cfg: cfg, + } + if !t.Exists() { + return nil, errors.Errorf("torrent not exist: %v", magnet) + } + return t, nil +} + type Torrent struct { //t *transmissionrpc.Torrent c *transmissionrpc.Client - Hash string `json:"hash"` - Config -} - -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 + hash string `json:"hash"` + //cfg Config } func (t *Torrent) getTorrent() (transmissionrpc.Torrent, error) { - r, err := t.c.TorrentGetAllForHashes(context.TODO(), []string{t.Hash}) + r, err := t.c.TorrentGetAllForHashes(context.TODO(), []string{t.hash}) if err != nil { log.Errorf("get torrent info for error: %v", err) } @@ -111,7 +119,7 @@ func (t *Torrent) getTorrent() (transmissionrpc.Torrent, error) { } func (t *Torrent) Exists() bool { - r, err := t.c.TorrentGetAllForHashes(context.TODO(), []string{t.Hash}) + r, err := t.c.TorrentGetAllForHashes(context.TODO(), []string{t.hash}) if err != nil { log.Errorf("get torrent info for error: %v", err) } @@ -149,7 +157,7 @@ func (t *Torrent) Progress() (int, error) { } func (t *Torrent) Stop() error { - return t.c.TorrentStopHashes(context.TODO(), []string{t.Hash}) + return t.c.TorrentStopHashes(context.TODO(), []string{t.hash}) } func (t *Torrent) SeedRatio() (float64, error) { @@ -164,7 +172,7 @@ func (t *Torrent) SeedRatio() (float64, error) { } func (t *Torrent) Start() error { - return t.c.TorrentStartHashes(context.TODO(), []string{t.Hash}) + return t.c.TorrentStartHashes(context.TODO(), []string{t.hash}) } func (t *Torrent) Remove() error { @@ -186,12 +194,6 @@ func (t *Torrent) Size() (int, error) { return int(tt.TotalSize.Byte()), nil } -func (t *Torrent) Save() string { - - d, _ := json.Marshal(*t) - return string(d) -} - func (t *Torrent) GetHash() string { - return t.Hash + return t.hash } diff --git a/server/core/client.go b/server/core/client.go index 1e2b219..2c2a4cc 100644 --- a/server/core/client.go +++ b/server/core/client.go @@ -1,7 +1,6 @@ package core import ( - "encoding/json" "polaris/db" "polaris/ent" "polaris/ent/downloadclients" @@ -27,20 +26,20 @@ func NewClient(db *db.Client, language string) *Client { type scheduler struct { cron string - f func() error + f func() error } type Client struct { - db *db.Client - cron *cron.Cron - tasks map[int]*Task - language string + db *db.Client + cron *cron.Cron + tasks map[int]*Task + language string schedulers utils.Map[string, scheduler] } func (c *Client) registerCronJob(name string, cron string, f func() error) { c.schedulers.Store(name, scheduler{ cron: cron, - f: f, + f: f, }) } @@ -52,44 +51,39 @@ func (c *Client) Init() { func (c *Client) reloadTasks() { allTasks := c.db.GetRunningHistories() for _, t := range allTasks { - 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) + dl, err := c.db.GetDownloadClient(t.DownloadClientID) + if err != nil { + log.Warnf("no download client related: %v", t.SourceTitle) continue } - c.tasks[t.ID] = &Task{Torrent: torrent} + + if dl.Implementation == downloadclients.ImplementationTransmission { + to, err := transmission.NewTorrent(transmission.Config{ + URL: dl.URL, + User: dl.User, + Password: dl.Password, + }, t.Link) + if err != nil { + log.Warnf("get task error: %v", err) + continue + } + c.tasks[t.ID] = &Task{Torrent: to} + } else if dl.Implementation == downloadclients.ImplementationQbittorrent { + to, err := qbittorrent.NewTorrent(qbittorrent.Info{ + URL: dl.URL, + User: dl.User, + Password: dl.Password, + }, t.Link) + if err != nil { + log.Warnf("get task error: %v", err) + continue + } + c.tasks[t.ID] = &Task{Torrent: to} + } + } } -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 { @@ -107,7 +101,7 @@ func (c *Client) GetDownloadClient() (pkg.Downloader, *ent.DownloadClients, erro continue } return trc, d, nil - + } else if d.Implementation == downloadclients.ImplementationQbittorrent { qbt, err := qbittorrent.NewClient(d.URL, d.User, d.Password) if err != nil { diff --git a/server/core/resources.go b/server/core/resources.go index 2ff0d38..3b81e4f 100644 --- a/server/core/resources.go +++ b/server/core/resources.go @@ -59,7 +59,7 @@ func (c *Client) DownloadEpisodeTorrent(r1 torznab.Result, seriesId, seasonNum, TargetDir: dir, Status: history.StatusRunning, Size: r1.Size, - Saved: torrent.Save(), + //Saved: torrent.Save(), Link: r1.Link, DownloadClientID: dlc.ID, IndexerID: r1.IndexerId, @@ -126,7 +126,7 @@ func (c *Client) DownloadMovie(m *ent.Media, link, name string, size int, indexe TargetDir: m.TargetDir, Status: history.StatusRunning, Size: size, - Saved: torrent.Save(), + //Saved: torrent.Save(), Link: link, DownloadClientID: dlc.ID, IndexerID: indexerID, diff --git a/server/core/scheduler.go b/server/core/scheduler.go index 25a5312..1526632 100644 --- a/server/core/scheduler.go +++ b/server/core/scheduler.go @@ -376,7 +376,7 @@ func (c *Client) downloadMovieSingleEpisode(ep *ent.Episode, targetDir string) ( TargetDir: targetDir, Status: history.StatusRunning, Size: r1.Size, - Saved: torrent.Save(), + //Saved: torrent.Save(), Link: r1.Link, DownloadClientID: dlc.ID, IndexerID: r1.IndexerId,