mirror of
https://github.com/simon-ding/polaris.git
synced 2026-06-09 19:47:47 +08:00
refactor: change method to restore tasks for better maintains
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user