refactor: copy downloaded file according to torrent info

This commit is contained in:
Simon Ding
2025-01-31 19:34:18 +08:00
parent 1386626712
commit c01924ac3f
8 changed files with 92 additions and 35 deletions

View File

@@ -1,5 +1,7 @@
package pkg
import "io/fs"
type Torrent interface {
Name() (string, error)
Progress() (int, error)
@@ -11,10 +13,10 @@ type Torrent interface {
SeedRatio() (float64, error)
GetHash() string
//Reload() error
WalkFunc() func(fn func(path string, info fs.FileInfo) error) error
}
type Downloader interface {
GetAll() ([]Torrent, error)
Download(link, dir string) (Torrent, error)
}

View File

@@ -2,7 +2,9 @@ package qbittorrent
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"polaris/pkg"
"polaris/pkg/go-qbittorrent/qbt"
"polaris/pkg/utils"
@@ -203,3 +205,49 @@ func (t *Torrent) SeedRatio() (float64, error) {
}
return qb.Ratio, nil
}
func (t *Torrent) Walk(f func(string) error) error {
files, err := t.c.TorrentFiles(t.hash)
if err != nil {
return err
}
for _, file := range files {
if err := f(file.Name); err != nil {
return errors.Errorf("proccess file (%s) error: %v", file.Name, err)
}
}
return nil
}
func (t *Torrent) WalkFunc() func(fn func(path string, info fs.FileInfo) error) error {
files, err := t.c.TorrentFiles(t.hash)
if err != nil {
return func(fn func(path string, info fs.FileInfo) error) error {
return err
}
}
path, err := t.c.DefaultSavePath()
if err != nil {
return func(fn func(path string, info fs.FileInfo) error) error {
return err
}
}
return func(fn func(path string, info fs.FileInfo) error) error {
for _, file := range files {
name := filepath.Join(path, file.Name)
info, err := os.Stat(name)
if err != nil {
return err
}
if err := fn(name, info); err != nil {
return errors.Errorf("proccess file (%s) error: %v", file.Name, err)
}
}
return nil
}
}

View File

@@ -3,7 +3,6 @@ package storage
import (
"io"
"io/fs"
"os"
"path/filepath"
"polaris/pkg/alist"
@@ -28,14 +27,7 @@ type Alist struct {
subtitleFormats []string
}
func (a *Alist) Move(src, dest string) error {
if err := a.Copy(src, dest); err != nil {
return err
}
return os.RemoveAll(src)
}
func (a *Alist) Copy(src, dest string) error {
func (a *Alist) Copy(src, dest string, walkFn WalkFn) error {
b, err := NewBase(src, a.videoFormats, a.subtitleFormats)
if err != nil {
return err
@@ -51,7 +43,7 @@ func (a *Alist) Copy(src, dest string) error {
}
baseDest := filepath.Join(a.baseDir, dest)
return b.Upload(baseDest, false, false, false, uploadFunc, mkdirFunc)
return b.Upload(baseDest, false, false, false, uploadFunc, mkdirFunc, walkFn)
}
func (a *Alist) ReadDir(dir string) ([]fs.FileInfo, error) {
@@ -75,4 +67,4 @@ func (a *Alist) UploadProgress() float64 {
func (a *Alist) RemoveAll(path string) error {
return nil
}
}

View File

@@ -13,9 +13,10 @@ import (
"github.com/pkg/errors"
)
type WalkFn func(fn func(path string, info fs.FileInfo) error) error
type Storage interface {
Move(src, dest string) error
Copy(src, dest string) error
//Move(src, dest string) error
Copy(src, dest string, walkFn WalkFn) error
ReadDir(dir string) ([]fs.FileInfo, error)
ReadFile(string) ([]byte, error)
WriteFile(string, []byte) error
@@ -79,7 +80,7 @@ func (b *Base) isFileNeeded(name string) bool {
}
func (b *Base) Upload(destDir string, tryLink, detectMime, changeMediaHash bool, upload uploadFunc, mkdir func(string) error) error {
func (b *Base) Upload(destDir string, tryLink, detectMime, changeMediaHash bool, upload uploadFunc, mkdir func(string) error, walkFn WalkFn) error {
if !b.checkVideoFilesExist() {
return errors.Errorf("torrent has no video file(s)")
}
@@ -95,7 +96,7 @@ func (b *Base) Upload(destDir string, tryLink, detectMime, changeMediaHash bool,
}
log.Debugf("local storage target base dir is: %v", targetBase)
err = filepath.Walk(b.src, func(path string, info fs.FileInfo, err error) error {
err = walkFn(func(path string, info fs.FileInfo) error {
if err != nil {
return err
}

View File

@@ -22,7 +22,7 @@ type LocalStorage struct {
subtitleFormats []string
}
func (l *LocalStorage) Copy(src, destDir string) error {
func (l *LocalStorage) Copy(src, destDir string,walkFn WalkFn) error {
b, err := NewBase(src, l.videoFormats, l.subtitleFormats)
if err != nil {
return err
@@ -44,14 +44,7 @@ func (l *LocalStorage) Copy(src, destDir string) error {
}
return b.Upload(baseDest, true, false, false, uploadFunc, func(s string) error {
return os.Mkdir(s, os.ModePerm)
})
}
func (l *LocalStorage) Move(src, destDir string) error {
if err := l.Copy(src, destDir); err != nil {
return err
}
return os.RemoveAll(src)
}, walkFn)
}
func (l *LocalStorage) ReadDir(dir string) ([]fs.FileInfo, error) {

View File

@@ -34,7 +34,7 @@ func NewWebdavStorage(url, user, password, path string, changeMediaHash bool, vi
}, nil
}
func (w *WebdavStorage) Copy(local, remoteDir string) error {
func (w *WebdavStorage) Copy(local, remoteDir string, walkFn WalkFn) error {
b, err := NewBase(local, w.videoFormats, w.subtitleFormats)
if err != nil {
return err
@@ -57,14 +57,7 @@ func (w *WebdavStorage) Copy(local, remoteDir string) error {
return b.Upload(filepath.Join(w.dir, remoteDir), false, true, w.changeMediaHash, uploadFunc, func(s string) error {
return nil
})
}
func (w *WebdavStorage) Move(local, remoteDir string) error {
if err := w.Copy(local, remoteDir); err != nil {
return err
}
return os.RemoveAll(local)
}, walkFn)
}
func (w *WebdavStorage) ReadDir(dir string) ([]fs.FileInfo, error) {

View File

@@ -3,7 +3,10 @@ package transmission
import (
"context"
"fmt"
"io/fs"
"net/url"
"os"
"path/filepath"
"polaris/log"
"polaris/pkg"
"polaris/pkg/utils"
@@ -212,3 +215,28 @@ func (t *Torrent) Size() (int, error) {
func (t *Torrent) GetHash() string {
return t.hash
}
func (t *Torrent) WalkFunc() func(fn func(path string, info fs.FileInfo) error) error {
tt, err := t.getTorrent()
if err != nil {
return func(fn func(path string, info fs.FileInfo) error) error {
return errors.Wrap(err, "get torrent info")
}
}
return func(fn func(path string, info fs.FileInfo) error) error {
for _, file := range tt.Files {
name := filepath.Join(*tt.DownloadDir, file.Name)
info, err := os.Stat(name)
if err != nil {
return err
}
if err := fn(name, info); err != nil {
return err
}
}
return nil
}
}

View File

@@ -212,7 +212,7 @@ func (c *Client) moveCompletedTask(id int) (err1 error) {
}
//如果种子是路径,则会把路径展开,只移动文件,类似 move dir/* dir2/, 如果种子是文件,则会直接移动文件,类似 move file dir/
if err := stImpl.Copy(filepath.Join(c.db.GetDownloadDir(), torrentName), r.TargetDir); err != nil {
if err := stImpl.Copy(filepath.Join(c.db.GetDownloadDir(), torrentName), r.TargetDir, torrent.WalkFunc()); err != nil {
return errors.Wrap(err, "move file")
}
torrent.UploadProgresser = stImpl.UploadProgress