mirror of
https://github.com/simon-ding/polaris.git
synced 2026-02-06 23:21:00 +08:00
feat: change webdav impl
This commit is contained in:
7
go.mod
7
go.mod
@@ -4,14 +4,14 @@ go 1.22.4
|
||||
|
||||
require (
|
||||
entgo.io/ent v0.13.1
|
||||
github.com/golang-jwt/jwt/v5 v5.2.1
|
||||
github.com/mattn/go-sqlite3 v1.14.16
|
||||
github.com/robfig/cron v1.2.0
|
||||
github.com/studio-b12/gowebdav v0.9.0
|
||||
go.uber.org/zap v1.27.0
|
||||
golang.org/x/net v0.25.0
|
||||
)
|
||||
|
||||
require github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
|
||||
|
||||
require (
|
||||
ariga.io/atlas v0.19.1-0.20240203083654-5948b60a8e43 // indirect
|
||||
github.com/agext/levenshtein v1.2.1 // indirect
|
||||
@@ -20,7 +20,6 @@ require (
|
||||
github.com/bytedance/sonic/loader v0.1.1 // indirect
|
||||
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||
github.com/cloudwego/iasm v0.2.0 // indirect
|
||||
github.com/emersion/go-webdav v0.5.0
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
@@ -55,7 +54,7 @@ require (
|
||||
github.com/ugorji/go/codec v1.2.12 // indirect
|
||||
github.com/zclconf/go-cty v1.8.0 // indirect
|
||||
golang.org/x/arch v0.8.0 // indirect
|
||||
golang.org/x/crypto v0.23.0 // indirect
|
||||
golang.org/x/crypto v0.23.0
|
||||
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
|
||||
golang.org/x/mod v0.17.0 // indirect
|
||||
golang.org/x/sys v0.21.0 // indirect
|
||||
|
||||
8
go.sum
8
go.sum
@@ -22,10 +22,6 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/emersion/go-ical v0.0.0-20220601085725-0864dccc089f/go.mod h1:2MKFUgfNMULRxqZkadG1Vh44we3y5gJAtTBlVsx1BKQ=
|
||||
github.com/emersion/go-vcard v0.0.0-20230815062825-8fda7d206ec9/go.mod h1:HMJKR5wlh/ziNp+sHEDV2ltblO4JD2+IdDOWtGcQBTM=
|
||||
github.com/emersion/go-webdav v0.5.0 h1:Ak/BQLgAihJt/UxJbCsEXDPxS5Uw4nZzgIMOq3rkKjc=
|
||||
github.com/emersion/go-webdav v0.5.0/go.mod h1:ycyIzTelG5pHln4t+Y32/zBvmrM7+mV7x+V+Gx4ZQno=
|
||||
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
|
||||
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
|
||||
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
||||
@@ -143,10 +139,10 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/studio-b12/gowebdav v0.9.0 h1:1j1sc9gQnNxbXXM4M/CebPOX4aXYtr7MojAVcN4dHjU=
|
||||
github.com/studio-b12/gowebdav v0.9.0/go.mod h1:bHA7t77X/QFExdeAnDzK6vKM34kEZAcE1OX4MfiwjkE=
|
||||
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
|
||||
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
|
||||
github.com/teambition/rrule-go v1.7.2/go.mod h1:mBJ1Ht5uboJ6jexKdNUJg2NcwP8uUMNvStWXlJD3MvU=
|
||||
github.com/teambition/rrule-go v1.8.2/go.mod h1:Ieq5AbrKGciP1V//Wq8ktsTXwSwJHDD5mD/wLBGl3p4=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
|
||||
type Storage interface {
|
||||
Move(src, dest string) error
|
||||
ReadDir(dir string) ([]FileInfo, error)
|
||||
ReadDir(dir string) ([]fs.FileInfo, error)
|
||||
}
|
||||
|
||||
func NewLocalStorage(dir string) *LocalStorage {
|
||||
|
||||
@@ -1,36 +1,23 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"polaris/log"
|
||||
"time"
|
||||
|
||||
"github.com/emersion/go-webdav"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/studio-b12/gowebdav"
|
||||
)
|
||||
|
||||
type FileInfo struct {
|
||||
Path string
|
||||
Size int64
|
||||
ModTime time.Time
|
||||
IsDir bool
|
||||
MIMEType string
|
||||
ETag string
|
||||
}
|
||||
|
||||
type WebdavStorage struct {
|
||||
fs *webdav.Client
|
||||
fs *gowebdav.Client
|
||||
}
|
||||
|
||||
func NewWebdavStorage(url, user, password string) (*WebdavStorage, error) {
|
||||
c, err := webdav.NewClient(webdav.HTTPClientWithBasicAuth(http.DefaultClient, user, password), url)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "new webdav")
|
||||
c := gowebdav.NewClient(url, user, password)
|
||||
if err := c.Connect(); err != nil {
|
||||
return nil, errors.Wrap(err, "connect webdav")
|
||||
}
|
||||
return &WebdavStorage{
|
||||
fs: c,
|
||||
@@ -44,7 +31,7 @@ func (w *WebdavStorage) Move(local, remote string) error {
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "read file %v", path)
|
||||
}
|
||||
|
||||
|
||||
rel, err := filepath.Rel(local, path)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "path relation")
|
||||
@@ -53,23 +40,18 @@ func (w *WebdavStorage) Move(local, remote string) error {
|
||||
|
||||
if info.IsDir() {
|
||||
|
||||
if err := w.fs.Mkdir(context.TODO(), remoteName); err != nil {
|
||||
if err := w.fs.Mkdir(remoteName, 0666); err != nil {
|
||||
return errors.Wrapf(err, "mkdir %v", remoteName)
|
||||
}
|
||||
|
||||
} else { //is file
|
||||
if writer, err := w.fs.Create(context.TODO(), remoteName); err != nil {
|
||||
return errors.Wrapf(err, "create file %s", remoteName)
|
||||
} else {
|
||||
defer writer.Close()
|
||||
if f, err := os.OpenFile(path, os.O_RDONLY, 0666); err != nil {
|
||||
return errors.Wrapf(err, "read file %v", path)
|
||||
} else { //open success
|
||||
defer f.Close()
|
||||
_, err := io.Copy(writer, f)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "transmitting data error")
|
||||
}
|
||||
if f, err := os.OpenFile(path, os.O_RDONLY, 0666); err != nil {
|
||||
return errors.Wrapf(err, "read file %v", path)
|
||||
} else { //open success
|
||||
defer f.Close()
|
||||
|
||||
if err := w.fs.WriteStream(remoteName, f, 0666); err != nil {
|
||||
return errors.Wrap(err, "transmitting data error")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -82,21 +64,6 @@ func (w *WebdavStorage) Move(local, remote string) error {
|
||||
return os.RemoveAll(local)
|
||||
}
|
||||
|
||||
func (w *WebdavStorage) ReadDir(dir string) ([]FileInfo, error) {
|
||||
fi, err := w.fs.ReadDir(context.TODO(), dir, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var res []FileInfo = make([]FileInfo, 0, len(fi))
|
||||
for _, f := range fi {
|
||||
res = append(res, FileInfo{
|
||||
Path: f.Path,
|
||||
Size: f.Size,
|
||||
ModTime: f.ModTime,
|
||||
IsDir: f.IsDir,
|
||||
MIMEType: f.MIMEType,
|
||||
ETag: f.ETag,
|
||||
})
|
||||
}
|
||||
return res, nil
|
||||
func (w *WebdavStorage) ReadDir(dir string) ([]fs.FileInfo, error) {
|
||||
return w.fs.ReadDir(dir)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user