fix: moving file after complete

This commit is contained in:
Simon Ding
2024-07-15 10:30:41 +08:00
parent 3104c9fe6a
commit f37a8eeb5f
4 changed files with 38 additions and 36 deletions

View File

@@ -73,5 +73,5 @@ func (l *LocalStorage) Move(src, dest string) error {
func (l *LocalStorage) ReadDir(dir string) ([]fs.FileInfo, error) { func (l *LocalStorage) ReadDir(dir string) ([]fs.FileInfo, error) {
return ioutil.ReadDir(dir) return ioutil.ReadDir(filepath.Join(l.dir, dir))
} }

View File

@@ -14,21 +14,23 @@ import (
type WebdavStorage struct { type WebdavStorage struct {
fs *gowebdav.Client fs *gowebdav.Client
dir string
} }
func NewWebdavStorage(url, user, password string) (*WebdavStorage, error) { func NewWebdavStorage(url, user, password, path string) (*WebdavStorage, error) {
c := gowebdav.NewClient(url, user, password) c := gowebdav.NewClient(url, user, password)
if err := c.Connect(); err != nil { if err := c.Connect(); err != nil {
return nil, errors.Wrap(err, "connect webdav") return nil, errors.Wrap(err, "connect webdav")
} }
return &WebdavStorage{ return &WebdavStorage{
fs: c, fs: c,
dir: path,
}, nil }, nil
} }
func (w *WebdavStorage) Move(local, remote string) error { func (w *WebdavStorage) Move(local, remote string) error {
baseLocal := filepath.Base(local) baseLocal := filepath.Base(local)
remoteBase := filepath.Join(remote, baseLocal) remoteBase := filepath.Join(w.dir,remote, baseLocal)
log.Infof("remove all content in %s", remoteBase) log.Infof("remove all content in %s", remoteBase)
w.fs.RemoveAll(remoteBase) w.fs.RemoveAll(remoteBase)
@@ -79,5 +81,5 @@ func (w *WebdavStorage) Move(local, remote string) error {
} }
func (w *WebdavStorage) ReadDir(dir string) ([]fs.FileInfo, error) { func (w *WebdavStorage) ReadDir(dir string) ([]fs.FileInfo, error) {
return w.fs.ReadDir(dir) return w.fs.ReadDir(filepath.Join(w.dir, dir))
} }

View File

@@ -61,27 +61,28 @@ func (s *Server) moveCompletedTask(id int) error {
} }
st := s.db.GetStorage(series.StorageID) st := s.db.GetStorage(series.StorageID)
log.Infof("move task files to target dir: %v", r.TargetDir) log.Infof("move task files to target dir: %v", r.TargetDir)
var stImpl storage.Storage
if st.Implementation == storage1.ImplementationWebdav { if st.Implementation == storage1.ImplementationWebdav {
ws := st.ToWebDavSetting() ws := st.ToWebDavSetting()
storageImpl, err := storage.NewWebdavStorage(ws.Path, ws.User, ws.Password) storageImpl, err := storage.NewWebdavStorage(ws.URL, ws.User, ws.Password, ws.Path)
if err != nil { if err != nil {
return errors.Wrap(err, "new webdav") return errors.Wrap(err, "new webdav")
} }
if err := storageImpl.Move(filepath.Join(s.db.GetDownloadDir(), torrent.Name()), r.TargetDir); err != nil { stImpl = storageImpl
return errors.Wrap(err, "move webdav")
}
} else if st.Implementation == storage1.ImplementationLocal { } else if st.Implementation == storage1.ImplementationLocal {
ls := st.ToLocalSetting() ls := st.ToLocalSetting()
storageImpl, err := storage.NewLocalStorage(ls.Path) storageImpl, err := storage.NewLocalStorage(ls.Path)
if err != nil { if err != nil {
return errors.Wrap(err, "new storage") return errors.Wrap(err, "new storage")
} }
stImpl = storageImpl
if err := storageImpl.Move(filepath.Join(s.db.GetDownloadDir(), torrent.Name()), r.TargetDir); err != nil {
return errors.Wrap(err, "move webdav")
}
} }
if err := stImpl.Move(filepath.Join(s.db.GetDownloadDir(), torrent.Name()), r.TargetDir); err != nil {
return errors.Wrap(err, "move file")
}
log.Infof("move downloaded files to target dir success, file: %v, target dir: %v", torrent.Name(), r.TargetDir) log.Infof("move downloaded files to target dir success, file: %v, target dir: %v", torrent.Name(), r.TargetDir)
torrent.Remove() torrent.Remove()
delete(s.tasks, r.ID) delete(s.tasks, r.ID)
@@ -102,11 +103,10 @@ func (s *Server) checkAllFiles() {
} }
} }
func (s *Server) checkFileExists(series *ent.Series) error{ func (s *Server) checkFileExists(series *ent.Series) error {
log.Infof("check files in directory: %s", series.TargetDir) log.Infof("check files in directory: %s", series.TargetDir)
st := s.db.GetStorage(series.StorageID) st := s.db.GetStorage(series.StorageID)
targetDir := filepath.Join(st.GetPath(), series.TargetDir)
var storageImpl storage.Storage var storageImpl storage.Storage
switch st.Implementation { switch st.Implementation {
@@ -120,20 +120,20 @@ func (s *Server) checkFileExists(series *ent.Series) error{
case storage1.ImplementationWebdav: case storage1.ImplementationWebdav:
ws := st.ToWebDavSetting() ws := st.ToWebDavSetting()
storageImpl1, err := storage.NewWebdavStorage(ws.Path, ws.User, ws.Password) storageImpl1, err := storage.NewWebdavStorage(ws.URL, ws.User, ws.Password, ws.Path)
if err != nil { if err != nil {
return errors.Wrap(err, "new webdav") return errors.Wrap(err, "new webdav")
} }
storageImpl = storageImpl1 storageImpl = storageImpl1
} }
files, err := storageImpl.ReadDir(targetDir) files, err := storageImpl.ReadDir(series.TargetDir)
if err != nil { if err != nil {
return errors.Wrapf(err, "read dir %s", targetDir) return errors.Wrapf(err, "read dir %s", series.TargetDir)
} }
numRe := regexp.MustCompile("[0-9]+") numRe := regexp.MustCompile("[0-9]+")
epRe := regexp.MustCompile("E[0-9]+") epRe := regexp.MustCompile("E[0-9]+")
for _, in := range files { for _, in := range files {
if !in.IsDir() {//season dir, ignore file if !in.IsDir() { //season dir, ignore file
continue continue
} }
nums := numRe.FindAllString(in.Name(), -1) nums := numRe.FindAllString(in.Name(), -1)
@@ -170,4 +170,4 @@ func (s *Server) checkFileExists(series *ent.Series) error{
type Task struct { type Task struct {
Processing bool Processing bool
pkg.Torrent pkg.Torrent
} }

View File

@@ -55,7 +55,9 @@ class MyApp extends StatelessWidget {
final SharedPreferences prefs = final SharedPreferences prefs =
await SharedPreferences.getInstance(); await SharedPreferences.getInstance();
await prefs.remove('token'); await prefs.remove('token');
context.go(LoginScreen.route); if (context.mounted) {
context.go(LoginScreen.route);
}
}, },
icon: const Icon(Icons.exit_to_app)) icon: const Icon(Icons.exit_to_app))
: Container() : Container()
@@ -115,22 +117,20 @@ class MyApp extends StatelessWidget {
return ProviderScope( return ProviderScope(
child: MaterialApp.router( child: MaterialApp.router(
title: 'Polaris', title: 'Polaris',
theme: ThemeData( theme: ThemeData(
// Define the default TextTheme. Use this to specify the default
// Define the default TextTheme. Use this to specify the default // text styling for headlines, titles, bodies of text, and more.
// text styling for headlines, titles, bodies of text, and more. // textTheme: const TextTheme(
// textTheme: const TextTheme( // bodyLarge: TextStyle(fontFamilyFallback: ["PingFang SC", "Heiti SC"]),
// bodyLarge: TextStyle(fontFamilyFallback: ["PingFang SC", "Heiti SC"]), // bodyMedium: TextStyle(fontFamilyFallback: ["PingFang SC", "Heiti SC"]),
// bodyMedium: TextStyle(fontFamilyFallback: ["PingFang SC", "Heiti SC"]), // bodySmall: TextStyle(fontFamilyFallback: ["PingFang SC", "Heiti SC"]),
// bodySmall: TextStyle(fontFamilyFallback: ["PingFang SC", "Heiti SC"]), // titleLarge: TextStyle(fontFamilyFallback: ["PingFang SC", "Heiti SC"]),
// titleLarge: TextStyle(fontFamilyFallback: ["PingFang SC", "Heiti SC"]), // titleMedium: TextStyle(fontFamilyFallback: ["PingFang SC", "Heiti SC"]),
// titleMedium: TextStyle(fontFamilyFallback: ["PingFang SC", "Heiti SC"]), // titleSmall: TextStyle(fontFamilyFallback: ["PingFang SC", "Heiti SC"]),
// titleSmall: TextStyle(fontFamilyFallback: ["PingFang SC", "Heiti SC"]), // labelLarge: TextStyle(fontFamilyFallback: ["PingFang SC", "Heiti SC"]),
// labelLarge: TextStyle(fontFamilyFallback: ["PingFang SC", "Heiti SC"]), // labelMedium: TextStyle(fontFamilyFallback: ["PingFang SC", "Heiti SC"]),
// labelMedium: TextStyle(fontFamilyFallback: ["PingFang SC", "Heiti SC"]), // labelSmall: TextStyle(fontFamilyFallback: ["PingFang SC", "Heiti SC"]),
// labelSmall: TextStyle(fontFamilyFallback: ["PingFang SC", "Heiti SC"]), // ),
// ),
colorScheme: ColorScheme.fromSeed( colorScheme: ColorScheme.fromSeed(
seedColor: Colors.blue, brightness: Brightness.dark), seedColor: Colors.blue, brightness: Brightness.dark),