mirror of
https://github.com/simon-ding/polaris.git
synced 2026-06-06 01:57:40 +08:00
feat: option to change file hash when update to webdav
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"path/filepath"
|
||||
"polaris/log"
|
||||
"polaris/pkg/gowebdav"
|
||||
"polaris/pkg/utils"
|
||||
|
||||
"github.com/gabriel-vasile/mimetype"
|
||||
"github.com/pkg/errors"
|
||||
@@ -15,9 +16,10 @@ import (
|
||||
type WebdavStorage struct {
|
||||
fs *gowebdav.Client
|
||||
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)
|
||||
if err := c.Connect(); err != nil {
|
||||
return nil, errors.Wrap(err, "connect webdav")
|
||||
@@ -53,6 +55,11 @@ func (w *WebdavStorage) Move(local, remote string) error {
|
||||
// }
|
||||
|
||||
} 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 {
|
||||
return errors.Wrapf(err, "read file %v", path)
|
||||
} else { //open success
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -146,3 +147,16 @@ func AvailableSpace(dir string) uint64 {
|
||||
unix.Statfs(dir, &stat)
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user