feat: option to change file hash when update to webdav

This commit is contained in:
Simon Ding
2024-07-23 15:18:42 +08:00
parent 8d2ce9752b
commit 6826422c2b
6 changed files with 50 additions and 11 deletions

View File

@@ -311,11 +311,12 @@ type LocalDirSetting struct {
} }
type WebdavSetting struct { type WebdavSetting struct {
URL string `json:"url"` URL string `json:"url"`
TvPath string `json:"tv_path"` TvPath string `json:"tv_path"`
MoviePath string `json:"movie_path"` MoviePath string `json:"movie_path"`
User string `json:"user"` User string `json:"user"`
Password string `json:"password"` Password string `json:"password"`
ChangeFileHash string `json:"change_file_hash"`
} }
func (c *Client) AddStorage(st *StorageInfo) error { func (c *Client) AddStorage(st *StorageInfo) error {

View File

@@ -7,6 +7,7 @@ import (
"path/filepath" "path/filepath"
"polaris/log" "polaris/log"
"polaris/pkg/gowebdav" "polaris/pkg/gowebdav"
"polaris/pkg/utils"
"github.com/gabriel-vasile/mimetype" "github.com/gabriel-vasile/mimetype"
"github.com/pkg/errors" "github.com/pkg/errors"
@@ -15,9 +16,10 @@ import (
type WebdavStorage struct { type WebdavStorage struct {
fs *gowebdav.Client fs *gowebdav.Client
dir string dir string
changeMediaHash bool
} }
func NewWebdavStorage(url, user, password, path string) (*WebdavStorage, error) { func NewWebdavStorage(url, user, password, path string, changeMediaHash bool) (*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")
@@ -53,6 +55,11 @@ func (w *WebdavStorage) Move(local, remote string) error {
// } // }
} else { //is file } else { //is file
if w.changeMediaHash {
if err := utils.ChangeFileHash(path); err != nil {
log.Errorf("change file %v hash error: %v", path, err)
}
}
if f, err := os.OpenFile(path, os.O_RDONLY, 0666); err != nil { if f, err := os.OpenFile(path, os.O_RDONLY, 0666); err != nil {
return errors.Wrapf(err, "read file %v", path) return errors.Wrapf(err, "read file %v", path)
} else { //open success } else { //open success

View File

@@ -1,6 +1,7 @@
package utils package utils
import ( import (
"os"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@@ -146,3 +147,16 @@ func AvailableSpace(dir string) uint64 {
unix.Statfs(dir, &stat) unix.Statfs(dir, &stat)
return stat.Bavail * uint64(stat.Bsize) return stat.Bavail * uint64(stat.Bsize)
} }
func ChangeFileHash(name string) error {
f, err := os.OpenFile(name, os.O_APPEND|os.O_WRONLY, 0655)
if err != nil {
return errors.Wrap(err, "open file")
}
defer f.Close()
_, err = f.Write([]byte("\000"))
if err != nil {
return errors.Wrap(err, "write file")
}
return nil
}

View File

@@ -101,7 +101,7 @@ func (s *Server) moveCompletedTask(id int) (err1 error) {
if series.MediaType == media.MediaTypeMovie { if series.MediaType == media.MediaTypeMovie {
targetPath = ws.MoviePath targetPath = ws.MoviePath
} }
storageImpl, err := storage.NewWebdavStorage(ws.URL, ws.User, ws.Password, targetPath) storageImpl, err := storage.NewWebdavStorage(ws.URL, ws.User, ws.Password, targetPath, ws.ChangeFileHash == "true")
if err != nil { if err != nil {
return errors.Wrap(err, "new webdav") return errors.Wrap(err, "new webdav")
} }
@@ -162,7 +162,7 @@ func (s *Server) checkDownloadedSeriesFiles(m *ent.Media) error {
case storage1.ImplementationWebdav: case storage1.ImplementationWebdav:
ws := st.ToWebDavSetting() ws := st.ToWebDavSetting()
targetPath := ws.TvPath targetPath := ws.TvPath
storageImpl1, err := storage.NewWebdavStorage(ws.URL, ws.User, ws.Password, targetPath) storageImpl1, err := storage.NewWebdavStorage(ws.URL, ws.User, ws.Password, targetPath, ws.ChangeFileHash == "true")
if err != nil { if err != nil {
return errors.Wrap(err, "new webdav") return errors.Wrap(err, "new webdav")
} }

View File

@@ -258,7 +258,12 @@ class StorageSettingData extends AutoDisposeAsyncNotifier<List<Storage>> {
class Storage { class Storage {
Storage( Storage(
{this.id, this.name, this.implementation, this.settings, this.isDefault}); {this.id,
this.name,
this.implementation,
this.settings,
this.isDefault,
});
final int? id; final int? id;
final String? name; final String? name;

View File

@@ -386,12 +386,15 @@ class _SystemSettingsPageState extends ConsumerState<SystemSettingsPage> {
var urlController = TextEditingController(); var urlController = TextEditingController();
var userController = TextEditingController(); var userController = TextEditingController();
var passController = TextEditingController(); var passController = TextEditingController();
bool enablingChangeFileHash = false;
if (s.settings != null) { if (s.settings != null) {
tvPathController.text = s.settings!["tv_path"] ?? ""; tvPathController.text = s.settings!["tv_path"] ?? "";
moviePathController.text = s.settings!["movie_path"] ?? ""; moviePathController.text = s.settings!["movie_path"] ?? "";
urlController.text = s.settings!["url"] ?? ""; urlController.text = s.settings!["url"] ?? "";
userController.text = s.settings!["user"] ?? ""; userController.text = s.settings!["user"] ?? "";
passController.text = s.settings!["password"] ?? ""; passController.text = s.settings!["password"] ?? "";
enablingChangeFileHash =
s.settings!["change_file_hash"] == "true" ? true : false;
} }
String selectImpl = s.implementation == null ? "local" : s.implementation!; String selectImpl = s.implementation == null ? "local" : s.implementation!;
@@ -419,7 +422,7 @@ class _SystemSettingsPageState extends ConsumerState<SystemSettingsPage> {
), ),
selectImpl != "local" selectImpl != "local"
? Column( ? Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
TextField( TextField(
decoration: const InputDecoration(labelText: "Webdav地址"), decoration: const InputDecoration(labelText: "Webdav地址"),
@@ -433,6 +436,14 @@ class _SystemSettingsPageState extends ConsumerState<SystemSettingsPage> {
decoration: const InputDecoration(labelText: "密码"), decoration: const InputDecoration(labelText: "密码"),
controller: passController, controller: passController,
), ),
CheckboxListTile(
title: const Text("上传时更改文件哈希", style: TextStyle(fontSize: 14),),
value: enablingChangeFileHash,
onChanged: (v) {
setState(() {
enablingChangeFileHash = v??false;
});
}),
], ],
) )
: Container(), : Container(),
@@ -456,7 +467,8 @@ class _SystemSettingsPageState extends ConsumerState<SystemSettingsPage> {
"movie_path": moviePathController.text, "movie_path": moviePathController.text,
"url": urlController.text, "url": urlController.text,
"user": userController.text, "user": userController.text,
"password": passController.text "password": passController.text,
"change_file_hash": enablingChangeFileHash ? "true" : "false"
}, },
)); ));
} }