Skip to content

Commit

Permalink
feat: add webdav driver
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Sep 4, 2022
1 parent 778cee4 commit 3dd4fbd
Show file tree
Hide file tree
Showing 25 changed files with 2,675 additions and 1 deletion.
1 change: 1 addition & 0 deletions drivers/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
_ "github.com/alist-org/alist/v3/drivers/teambition"
_ "github.com/alist-org/alist/v3/drivers/uss"
_ "github.com/alist-org/alist/v3/drivers/virtual"
_ "github.com/alist-org/alist/v3/drivers/webdav"
)

// All do nothing,just for import
Expand Down
130 changes: 130 additions & 0 deletions drivers/webdav/driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package webdav

import (
"context"
"net/http"
"os"
"path"
"time"

"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/errs"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/cron"
"github.com/alist-org/alist/v3/pkg/gowebdav"
"github.com/alist-org/alist/v3/pkg/utils"
)

type WebDav struct {
model.Storage
Addition
client *gowebdav.Client
cron *cron.Cron
}

func (d *WebDav) Config() driver.Config {
return config
}

func (d *WebDav) GetAddition() driver.Additional {
return d.Addition
}

func (d *WebDav) Init(ctx context.Context, storage model.Storage) error {
d.Storage = storage
err := utils.Json.UnmarshalFromString(d.Storage.Addition, &d.Addition)
if err != nil {
return err
}
err = d.setClient()
if err == nil {
d.cron = cron.NewCron(time.Hour * 12)
d.cron.Do(func() {
_ = d.setClient()
})
}
return err
}

func (d *WebDav) Drop(ctx context.Context) error {
if d.cron != nil {
d.cron.Stop()
}
return nil
}

func (d *WebDav) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
files, err := d.client.ReadDir(dir.GetPath())
if err != nil {
return nil, err
}
return utils.SliceConvert(files, func(src os.FileInfo) (model.Obj, error) {
return &model.Object{
Name: src.Name(),
Size: src.Size(),
Modified: src.ModTime(),
IsFolder: src.IsDir(),
}, nil
})
}

//func (d *WebDav) Get(ctx context.Context, path string) (model.Obj, error) {
// // this is optional
// return nil, errs.NotImplement
//}

func (d *WebDav) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
callback := func(r *http.Request) {
if args.Header.Get("Range") != "" {
r.Header.Set("Range", args.Header.Get("Range"))
}
if args.Header.Get("If-Range") != "" {
r.Header.Set("If-Range", args.Header.Get("If-Range"))
}
}
reader, header, err := d.client.ReadStream(file.GetPath(), callback)
if err != nil {
return nil, err
}
link := &model.Link{Data: reader}
if header.Get("Content-Range") != "" {
link.Status = 206
link.Header = header
}
return link, nil
}

func (d *WebDav) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
return d.client.MkdirAll(path.Join(parentDir.GetPath(), dirName), 0644)
}

func (d *WebDav) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
return d.client.Rename(srcObj.GetPath(), path.Join(dstDir.GetPath(), srcObj.GetName()), true)
}

func (d *WebDav) Rename(ctx context.Context, srcObj model.Obj, newName string) error {
return d.client.Rename(srcObj.GetPath(), path.Join(path.Dir(srcObj.GetPath()), newName), true)
}

func (d *WebDav) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
return d.client.Copy(srcObj.GetPath(), path.Join(dstDir.GetPath(), srcObj.GetName()), true)
}

func (d *WebDav) Remove(ctx context.Context, obj model.Obj) error {
return d.client.RemoveAll(obj.GetPath())
}

func (d *WebDav) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
callback := func(r *http.Request) {
r.Header.Set("Content-Type", stream.GetMimetype())
r.ContentLength = stream.GetSize()
}
err := d.client.WriteStream(path.Join(dstDir.GetPath(), stream.GetName()), stream, 0644, callback)
return err
}

func (d *WebDav) Other(ctx context.Context, args model.OtherArgs) (interface{}, error) {
return nil, errs.NotSupport
}

var _ driver.Driver = (*WebDav)(nil)
29 changes: 29 additions & 0 deletions drivers/webdav/meta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package webdav

import (
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/op"
)

type Addition struct {
Vendor string `json:"vendor" type:"select" options:"sharepoint,other" default:"other"`
Address string `json:"address" required:"true"`
Username string `json:"username" required:"true"`
Password string `json:"password" required:"true"`
driver.RootPath
}

var config = driver.Config{
Name: "WebDav",
LocalSort: true,
OnlyLocal: true,
DefaultRoot: "/",
}

func New() driver.Driver {
return &WebDav{}
}

func init() {
op.RegisterDriver(config, New)
}
46 changes: 46 additions & 0 deletions drivers/webdav/odrvcookie/cookie.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package odrvcookie

import (
"net/http"

"github.com/alist-org/alist/v3/pkg/cookie"
)

//type SpCookie struct {
// Cookie string
// expire time.Time
//}
//
//func (sp SpCookie) IsExpire() bool {
// return time.Now().After(sp.expire)
//}
//
//var cookiesMap = struct {
// sync.Mutex
// m map[string]*SpCookie
//}{m: make(map[string]*SpCookie)}

func GetCookie(username, password, siteUrl string) (string, error) {
//cookiesMap.Lock()
//defer cookiesMap.Unlock()
//spCookie, ok := cookiesMap.m[username]
//if ok {
// if !spCookie.IsExpire() {
// log.Debugln("sp use old cookie.")
// return spCookie.Cookie, nil
// }
//}
//log.Debugln("fetch new cookie")
ca := New(username, password, siteUrl)
tokenConf, err := ca.Cookies()
if err != nil {
return "", err
}
return cookie.ToString([]*http.Cookie{&tokenConf.RtFa, &tokenConf.FedAuth}), nil
//spCookie = &SpCookie{
// Cookie: cookie.ToString([]*http.Cookie{&tokenConf.RtFa, &tokenConf.FedAuth}),
// expire: time.Now().Add(time.Hour * 12),
//}
//cookiesMap.m[username] = spCookie
//return spCookie.Cookie, nil
}
Loading

0 comments on commit 3dd4fbd

Please sign in to comment.