fix: qbit torrent name mismatch file name

This commit is contained in:
Simon Ding
2024-10-11 11:39:41 +08:00
parent b281c6febb
commit f2faefa837

View File

@@ -1,11 +1,12 @@
package qbittorrent package qbittorrent
import ( import (
"encoding/json"
"fmt" "fmt"
"os"
"polaris/pkg" "polaris/pkg"
"polaris/pkg/go-qbittorrent/qbt" "polaris/pkg/go-qbittorrent/qbt"
"polaris/pkg/utils" "polaris/pkg/utils"
"strings"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@@ -69,11 +70,10 @@ func (c *Client) Download(link, dir string) (pkg.Torrent, error) {
if err != nil { if err != nil {
return nil, errors.Wrap(err, "qbt download") return nil, errors.Wrap(err, "qbt download")
} }
return &Torrent{hash: hash, c: c.c, }, nil return &Torrent{hash: hash, c: c.c}, nil
} }
func NewTorrent(info Info, link string) (*Torrent, error) { func NewTorrent(info Info, link string) (*Torrent, error) {
c, err := NewClient(info.URL, info.User, info.Password) c, err := NewClient(info.URL, info.User, info.Password)
if err != nil { if err != nil {
@@ -97,6 +97,7 @@ func NewTorrent(info Info, link string) (*Torrent, error) {
} }
return t, nil return t, nil
} }
type Torrent struct { type Torrent struct {
c *qbt.Client c *qbt.Client
hash string hash string
@@ -119,12 +120,29 @@ func (t *Torrent) getTorrent() (*qbt.TorrentInfo, error) {
} }
func (t *Torrent) Name() (string, error) { func (t *Torrent) Name() (string, error) {
dir, err := t.getTorrentBaseNameOrDir()
if err != nil { //use torrent name
qb, err := t.getTorrent() qb, err := t.getTorrent()
if err != nil { if err != nil {
return "", err return "", err
} }
return qb.Name, nil return qb.Name, nil
}
return dir, nil
}
// https://github.com/qbittorrent/qBittorrent/issues/13572
func (t *Torrent) getTorrentBaseNameOrDir() (string, error) {
files, err := t.c.TorrentFiles(t.hash)
if err != nil {
return "", err
}
if len(files) == 0 {
return "", errors.Wrap(err, "no file")
}
name := files[0].Name
dir := strings.Split(name, string(os.PathSeparator))[0]
return dir, nil
} }
func (t *Torrent) Progress() (int, error) { func (t *Torrent) Progress() (int, error) {
@@ -169,11 +187,6 @@ func (t *Torrent) Remove() error {
return nil return nil
} }
func (t *Torrent) Save() string {
data, _ := json.Marshal(t)
return string(data)
}
func (t *Torrent) Exists() bool { func (t *Torrent) Exists() bool {
_, err := t.getTorrent() _, err := t.getTorrent()
return err == nil return err == nil