Skip to content

Commit

Permalink
feat: add a driver template
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Aug 31, 2022
1 parent 41edac5 commit 7d407de
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 18 deletions.
17 changes: 17 additions & 0 deletions drivers/onedrive/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ import (
"github.com/alist-org/alist/v3/internal/model"
)

type Host struct {
Oauth string
Api string
}

type TokenErr struct {
Error string `json:"error"`
ErrorDescription string `json:"error_description"`
}

type RespErr struct {
Error struct {
Code string `json:"code"`
Message string `json:"message"`
} `json:"error"`
}

type File struct {
Id string `json:"id"`
Name string `json:"name"`
Expand Down
17 changes: 0 additions & 17 deletions drivers/onedrive/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ import (
log "github.com/sirupsen/logrus"
)

type Host struct {
Oauth string
Api string
}

var onedriveHostMap = map[string]Host{
"global": {
Oauth: "https://login.microsoftonline.com",
Expand Down Expand Up @@ -76,11 +71,6 @@ func (d *Onedrive) refreshToken() error {
return err
}

type TokenErr struct {
Error string `json:"error"`
ErrorDescription string `json:"error_description"`
}

func (d *Onedrive) _refreshToken() error {
url := d.GetMetaUrl(true, "") + "/common/oauth2/v2.0/token"
var resp base.TokenResp
Expand All @@ -106,13 +96,6 @@ func (d *Onedrive) _refreshToken() error {
return nil
}

type RespErr struct {
Error struct {
Code string `json:"code"`
Message string `json:"message"`
} `json:"error"`
}

func (d *Onedrive) Request(url string, method string, callback func(*resty.Request), resp interface{}) ([]byte, error) {
req := base.RestyClient.R()
req.SetHeader("Authorization", "Bearer "+d.AccessToken)
Expand Down
90 changes: 90 additions & 0 deletions drivers/template/driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package local

import (
"context"

"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/utils"
"github.com/pkg/errors"
)

type Template struct {
model.Storage
Addition
}

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

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

func (d *Template) Init(ctx context.Context, storage model.Storage) error {
d.Storage = storage
err := utils.Json.UnmarshalFromString(d.Storage.Addition, &d.Addition)
if err != nil {
return errors.Wrap(err, "error while unmarshal addition")
}
// TODO login / refresh token
//operations.MustSaveDriverStorage(d)
return err
}

func (d *Template) Drop(ctx context.Context) error {
return nil
}

func (d *Template) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
// TODO return the files list
return nil, errs.NotImplement
}

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

func (d *Template) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
// TODO return link of file
return nil, errs.NotImplement
}

func (d *Template) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
// TODO create folder
return errs.NotImplement
}

func (d *Template) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
// TODO move obj
return errs.NotImplement
}

func (d *Template) Rename(ctx context.Context, srcObj model.Obj, newName string) error {
// TODO rename obj
return errs.NotImplement
}

func (d *Template) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
// TODO copy obj
return errs.NotImplement
}

func (d *Template) Remove(ctx context.Context, obj model.Obj) error {
// TODO remove obj
return errs.NotImplement
}

func (d *Template) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
// TODO upload file
return errs.NotImplement
}

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

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

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

type Addition struct {
// Usually one of two
driver.RootFolderPath
driver.RootFolderId
// define other
Field string `json:"field" type:"select" required:"true" options:"a,b,c" default:"a"`
}

var config = driver.Config{
Name: "template",
LocalSort: false,
OnlyLocal: false,
OnlyProxy: false,
NoCache: false,
NoUpload: false,
NeedMs: false,
DefaultRoot: "root, / or other",
}

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

func init() {
operations.RegisterDriver(config, New)
}
1 change: 1 addition & 0 deletions drivers/template/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package local
3 changes: 3 additions & 0 deletions drivers/template/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package local

// do others that not defined in Driver interface
7 changes: 6 additions & 1 deletion internal/operations/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ func Get(ctx context.Context, storage driver.Driver, path string) (model.Obj, er
path = utils.StandardizePath(path)
log.Debugf("operations.Get %s", path)
if g, ok := storage.(driver.Getter); ok {
return g.Get(ctx, path)
obj, err := g.Get(ctx, path)
if err == nil {
return obj, nil
}
}
// is root folder
if r, ok := storage.GetAddition().(driver.IRootFolderId); ok && utils.PathEqual(path, "/") {
Expand Down Expand Up @@ -275,6 +278,8 @@ func Put(ctx context.Context, storage driver.Driver, dstDirPath string, file mod
err = storage.Put(ctx, parentDir, file, up)
log.Debugf("put file [%s] done", file.GetName())
if err == nil {
// set as complete
up(100)
// clear cache
key := stdpath.Join(storage.GetStorage().MountPath, dstDirPath)
filesCache.Del(key)
Expand Down

0 comments on commit 7d407de

Please sign in to comment.