🎇 support webdav driver

pull/548/head
微凉 2021-12-30 21:39:17 +08:00
parent 0af3e95f1f
commit d71ed4d775
13 changed files with 255 additions and 20 deletions

View File

@ -31,6 +31,7 @@ English | [中文](./README_cn.md)
- [x] [PikPak](https://www.mypikpak.com/) - [x] [PikPak](https://www.mypikpak.com/)
- [x] [ShandianPan](https://shandianpan.com/) - [x] [ShandianPan](https://shandianpan.com/)
- [x] [S3](https://aws.amazon.com/s3/) - [x] [S3](https://aws.amazon.com/s3/)
- [x] WebDav
- [x] File preview (PDF, markdown, code, plain text, ...) - [x] File preview (PDF, markdown, code, plain text, ...)
- [x] Image preview in gallery mode - [x] Image preview in gallery mode
- [x] Video and audio preview (mp4, mp3, ...) - [x] Video and audio preview (mp4, mp3, ...)

View File

@ -30,6 +30,7 @@
- [x] [PikPak](https://www.mypikpak.com/) - [x] [PikPak](https://www.mypikpak.com/)
- [x] [闪电盘](https://shandianpan.com/) - [x] [闪电盘](https://shandianpan.com/)
- [x] [S3](https://aws.amazon.com/cn/s3/) - [x] [S3](https://aws.amazon.com/cn/s3/)
- [x] WebDav
- [x] 文件预览PDF、markdown、代码、纯文本…… - [x] 文件预览PDF、markdown、代码、纯文本……
- [x] 画廊模式下的图像预览 - [x] 画廊模式下的图像预览
- [x] 视频和音频预览mp4、mp3 等) - [x] 视频和音频预览mp4、mp3 等)

View File

@ -13,4 +13,5 @@ import (
_ "github.com/Xhofe/alist/drivers/pikpak" _ "github.com/Xhofe/alist/drivers/pikpak"
_ "github.com/Xhofe/alist/drivers/s3" _ "github.com/Xhofe/alist/drivers/s3"
_ "github.com/Xhofe/alist/drivers/shandian" _ "github.com/Xhofe/alist/drivers/shandian"
_ "github.com/Xhofe/alist/drivers/webdav"
) )

View File

@ -11,8 +11,8 @@ import (
type DriverConfig struct { type DriverConfig struct {
Name string Name string
OnlyProxy bool OnlyProxy bool // 必须使用代理(本机或者其他机器)
NoLink bool // 必须本机返回的 OnlyLocal bool // 必须本机返回的
ApiProxy bool // 使用API中转的 ApiProxy bool // 使用API中转的
NoNeedSetLink bool // 不需要设置链接的 NoNeedSetLink bool // 不需要设置链接的
NoCors bool // 不可以跨域 NoCors bool // 不可以跨域

View File

@ -2,6 +2,7 @@ package base
import ( import (
"errors" "errors"
"io"
) )
var ( var (
@ -42,5 +43,5 @@ type Header struct{
type Link struct { type Link struct {
Url string `json:"url"` Url string `json:"url"`
Headers []Header `json:"headers"` Headers []Header `json:"headers"`
Data []byte Data io.ReadCloser
} }

View File

@ -8,7 +8,6 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/jlaffaye/ftp" "github.com/jlaffaye/ftp"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"io/ioutil"
"path/filepath" "path/filepath"
) )
@ -18,7 +17,7 @@ func (driver FTP) Config() base.DriverConfig {
return base.DriverConfig{ return base.DriverConfig{
Name: "FTP", Name: "FTP",
OnlyProxy: true, OnlyProxy: true,
NoLink: true, OnlyLocal: true,
NoNeedSetLink: true, NoNeedSetLink: true,
LocalSort: true, LocalSort: true,
} }
@ -150,13 +149,13 @@ func (driver FTP) Link(args base.Args, account *model.Account) (*base.Link, erro
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer func() { _ = resp.Close() }() //defer func() { _ = resp.Close() }()
data, err := ioutil.ReadAll(resp) //data, err := ioutil.ReadAll(resp)
if err != nil { //if err != nil {
return nil, err // return nil, err
} //}
return &base.Link{ return &base.Link{
Data: data, Data: resp,
}, nil }, nil
} }
@ -214,8 +213,10 @@ func (driver FTP) Move(src string, dst string, account *model.Account) error {
err = conn.Rename(realSrc, realDst) err = conn.Rename(realSrc, realDst)
if err != nil { if err != nil {
_ = base.DeleteCache(utils.Dir(src), account) _ = base.DeleteCache(utils.Dir(src), account)
if utils.Dir(src) != utils.Dir(dst) {
_ = base.DeleteCache(utils.Dir(dst), account) _ = base.DeleteCache(utils.Dir(dst), account)
} }
}
return err return err
} }

View File

@ -21,7 +21,7 @@ func (driver Native) Config() base.DriverConfig {
return base.DriverConfig{ return base.DriverConfig{
Name: "Native", Name: "Native",
OnlyProxy: true, OnlyProxy: true,
NoLink: true, OnlyLocal: true,
NoNeedSetLink: true, NoNeedSetLink: true,
LocalSort: true, LocalSort: true,
} }

194
drivers/webdav/driver.go Normal file
View File

@ -0,0 +1,194 @@
package webdav
import (
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
"path/filepath"
)
type WebDav struct{}
func (driver WebDav) Config() base.DriverConfig {
return base.DriverConfig{
Name: "WebDav",
OnlyProxy: true,
OnlyLocal: true,
NoNeedSetLink: true,
LocalSort: true,
}
}
func (driver WebDav) Items() []base.Item {
return []base.Item{
{
Name: "site_url",
Label: "webdav root url",
Type: base.TypeString,
Required: true,
},
{
Name: "username",
Label: "username",
Type: base.TypeString,
Required: true,
},
{
Name: "password",
Label: "password",
Type: base.TypeString,
Required: true,
},
}
}
func (driver WebDav) Save(account *model.Account, old *model.Account) error {
account.Status = "work"
_ = model.SaveAccount(account)
return nil
}
func (driver WebDav) File(path string, account *model.Account) (*model.File, error) {
if path == "/" {
return &model.File{
Id: "/",
Name: account.Name,
Size: 0,
Type: conf.FOLDER,
Driver: driver.Config().Name,
UpdatedAt: account.UpdatedAt,
}, nil
}
dir, name := filepath.Split(path)
files, err := driver.Files(dir, account)
if err != nil {
return nil, err
}
for _, file := range files {
if file.Name == name {
return &file, nil
}
}
return nil, base.ErrPathNotFound
}
func (driver WebDav) Files(path string, account *model.Account) ([]model.File, error) {
path = utils.ParsePath(path)
cache, err := base.GetCache(path, account)
if err == nil {
files, _ := cache.([]model.File)
return files, nil
}
c := driver.NewClient(account)
rawFiles, err := c.ReadDir(driver.WebDavPath(path))
if err != nil {
return nil, err
}
files := make([]model.File, 0)
if len(rawFiles) == 0 {
return files, nil
}
for _, f := range rawFiles {
t := f.ModTime()
file := model.File{
Name: f.Name(),
Size: f.Size(),
Driver: driver.Config().Name,
UpdatedAt: &t,
}
if f.IsDir() {
file.Type = conf.FOLDER
} else {
file.Type = utils.GetFileType(filepath.Ext(f.Name()))
}
files = append(files, file)
}
_ = base.SetCache(path, files, account)
return files, nil
}
func (driver WebDav) Link(args base.Args, account *model.Account) (*base.Link, error) {
path := args.Path
c := driver.NewClient(account)
reader, err := c.ReadStream(driver.WebDavPath(path))
if err != nil {
return nil, err
}
return &base.Link{Data: reader}, nil
}
func (driver WebDav) Path(path string, account *model.Account) (*model.File, []model.File, error) {
file, err := driver.File(path, account)
if err != nil {
return nil, nil, err
}
if !file.IsDir() {
return file, nil, nil
}
files, err := driver.Files(path, account)
if err != nil {
return nil, nil, err
}
return nil, files, nil
}
func (driver WebDav) Proxy(c *gin.Context, account *model.Account) {
}
func (driver WebDav) Preview(path string, account *model.Account) (interface{}, error) {
return nil, base.ErrNotSupport
}
func (driver WebDav) MakeDir(path string, account *model.Account) error {
c := driver.NewClient(account)
err := c.MkdirAll(driver.WebDavPath(path), 0644)
if err == nil {
_ = base.DeleteCache(utils.Dir(path), account)
}
return err
}
func (driver WebDav) Move(src string, dst string, account *model.Account) error {
c := driver.NewClient(account)
err := c.Rename(driver.WebDavPath(src), driver.WebDavPath(dst), true)
if err == nil {
_ = base.DeleteCache(utils.Dir(src), account)
if utils.Dir(src) != utils.Dir(dst) {
_ = base.DeleteCache(utils.Dir(dst), account)
}
}
return err
}
func (driver WebDav) Copy(src string, dst string, account *model.Account) error {
c := driver.NewClient(account)
err := c.Copy(driver.WebDavPath(src), driver.WebDavPath(dst), true)
if err == nil {
_ = base.DeleteCache(utils.Dir(dst), account)
}
return err
}
func (driver WebDav) Delete(path string, account *model.Account) error {
c := driver.NewClient(account)
err := c.RemoveAll(driver.WebDavPath(path))
if err == nil {
_ = base.DeleteCache(utils.Dir(path), account)
}
return err
}
func (driver WebDav) Upload(file *model.FileStream, account *model.Account) error {
c := driver.NewClient(account)
path := utils.Join(file.ParentPath, file.Name)
err := c.WriteStream(driver.WebDavPath(path), file, 0644)
if err == nil {
_ = base.DeleteCache(utils.Dir(file.ParentPath), account)
}
return err
}
var _ base.Driver = (*WebDav)(nil)

23
drivers/webdav/webdav.go Normal file
View File

@ -0,0 +1,23 @@
package webdav
import (
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/studio-b12/gowebdav"
"strings"
)
func (driver WebDav) NewClient(account *model.Account) *gowebdav.Client {
return gowebdav.NewClient(account.SiteUrl, account.Username, account.Password)
}
func (driver WebDav) WebDavPath(path string) string {
path = utils.ParsePath(path)
path = strings.TrimPrefix(path, "/")
return path
}
func init() {
base.RegisterDriver(&WebDav{})
}

1
go.mod
View File

@ -57,6 +57,7 @@ require (
github.com/prometheus/common v0.18.0 // indirect github.com/prometheus/common v0.18.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect github.com/prometheus/procfs v0.6.0 // indirect
github.com/spf13/cast v1.3.1 // indirect github.com/spf13/cast v1.3.1 // indirect
github.com/studio-b12/gowebdav v0.0.0-20211109083228-3f8721cd4b6f // indirect
github.com/ugorji/go/codec v1.2.6 // indirect github.com/ugorji/go/codec v1.2.6 // indirect
go.opentelemetry.io/otel v0.20.0 // indirect go.opentelemetry.io/otel v0.20.0 // indirect
go.opentelemetry.io/otel/metric v0.20.0 // indirect go.opentelemetry.io/otel/metric v0.20.0 // indirect

2
go.sum
View File

@ -486,6 +486,8 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/studio-b12/gowebdav v0.0.0-20211109083228-3f8721cd4b6f h1:L2NE7BXnSlSLoNYZ0lCwZDjdnYjCNYC71k9ClZUTFTs=
github.com/studio-b12/gowebdav v0.0.0-20211109083228-3f8721cd4b6f/go.mod h1:bHA7t77X/QFExdeAnDzK6vKM34kEZAcE1OX4MfiwjkE=
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go v1.2.6 h1:tGiWC9HENWE2tqYycIqFTNorMmFRVhNwCpDOpWqnk8E= github.com/ugorji/go v1.2.6 h1:tGiWC9HENWE2tqYycIqFTNorMmFRVhNwCpDOpWqnk8E=

View File

@ -9,6 +9,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/go-resty/resty/v2" "github.com/go-resty/resty/v2"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"io"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"net/url" "net/url"
@ -78,8 +79,17 @@ func Proxy(c *gin.Context) {
return return
} }
// 本机读取数据 // 本机读取数据
if account.Type == "FTP" { if link.Data != nil {
c.Data(http.StatusOK, "application/octet-stream", link.Data) //c.Data(http.StatusOK, "application/octet-stream", link.Data)
defer func() {
_ = link.Data.Close()
}()
c.Status(http.StatusOK)
c.Header("content", "application/octet-stream")
_, err = io.Copy(c.Writer, link.Data)
if err != nil {
_, _ = c.Writer.WriteString(err.Error())
}
return return
} }
// 本机文件直接返回文件 // 本机文件直接返回文件

View File

@ -104,7 +104,7 @@ func Link(c *gin.Context) {
common.ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return return
} }
if driver.Config().NoLink { if driver.Config().OnlyLocal {
common.SuccessResp(c, base.Link{ common.SuccessResp(c, base.Link{
Url: fmt.Sprintf("//%s/p%s?d=1&sign=%s", c.Request.Host, req.Path, utils.SignWithToken(utils.Base(rawPath), conf.Token)), Url: fmt.Sprintf("//%s/p%s?d=1&sign=%s", c.Request.Host, req.Path, utils.SignWithToken(utils.Base(rawPath), conf.Token)),
}) })