From 7d407de22e53395b6d9aeec3a411371762263732 Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Wed, 31 Aug 2022 16:32:51 +0800 Subject: [PATCH] feat: add a driver template --- drivers/onedrive/types.go | 17 +++++++ drivers/onedrive/util.go | 17 ------- drivers/template/driver.go | 90 ++++++++++++++++++++++++++++++++++++++ drivers/template/meta.go | 33 ++++++++++++++ drivers/template/types.go | 1 + drivers/template/util.go | 3 ++ internal/operations/fs.go | 7 ++- 7 files changed, 150 insertions(+), 18 deletions(-) create mode 100644 drivers/template/driver.go create mode 100644 drivers/template/meta.go create mode 100644 drivers/template/types.go create mode 100644 drivers/template/util.go diff --git a/drivers/onedrive/types.go b/drivers/onedrive/types.go index 111564cc43b..170d8eafbd6 100644 --- a/drivers/onedrive/types.go +++ b/drivers/onedrive/types.go @@ -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"` diff --git a/drivers/onedrive/util.go b/drivers/onedrive/util.go index 5411eedab64..bf519be41b8 100644 --- a/drivers/onedrive/util.go +++ b/drivers/onedrive/util.go @@ -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", @@ -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 @@ -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) diff --git a/drivers/template/driver.go b/drivers/template/driver.go new file mode 100644 index 00000000000..ceb3cc02408 --- /dev/null +++ b/drivers/template/driver.go @@ -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) diff --git a/drivers/template/meta.go b/drivers/template/meta.go new file mode 100644 index 00000000000..86ff7eba0ce --- /dev/null +++ b/drivers/template/meta.go @@ -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) +} diff --git a/drivers/template/types.go b/drivers/template/types.go new file mode 100644 index 00000000000..469c3dc0d8c --- /dev/null +++ b/drivers/template/types.go @@ -0,0 +1 @@ +package local diff --git a/drivers/template/util.go b/drivers/template/util.go new file mode 100644 index 00000000000..01f11c23bd1 --- /dev/null +++ b/drivers/template/util.go @@ -0,0 +1,3 @@ +package local + +// do others that not defined in Driver interface diff --git a/internal/operations/fs.go b/internal/operations/fs.go index 0d58d971857..0845a36175b 100644 --- a/internal/operations/fs.go +++ b/internal/operations/fs.go @@ -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, "/") { @@ -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)