mirror of https://github.com/Xhofe/alist
feat: add a driver template
parent
41edac5826
commit
7d407de22e
|
@ -6,6 +6,23 @@ import (
|
||||||
"github.com/alist-org/alist/v3/internal/model"
|
"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 {
|
type File struct {
|
||||||
Id string `json:"id"`
|
Id string `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
|
|
@ -21,11 +21,6 @@ import (
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Host struct {
|
|
||||||
Oauth string
|
|
||||||
Api string
|
|
||||||
}
|
|
||||||
|
|
||||||
var onedriveHostMap = map[string]Host{
|
var onedriveHostMap = map[string]Host{
|
||||||
"global": {
|
"global": {
|
||||||
Oauth: "https://login.microsoftonline.com",
|
Oauth: "https://login.microsoftonline.com",
|
||||||
|
@ -76,11 +71,6 @@ func (d *Onedrive) refreshToken() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
type TokenErr struct {
|
|
||||||
Error string `json:"error"`
|
|
||||||
ErrorDescription string `json:"error_description"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Onedrive) _refreshToken() error {
|
func (d *Onedrive) _refreshToken() error {
|
||||||
url := d.GetMetaUrl(true, "") + "/common/oauth2/v2.0/token"
|
url := d.GetMetaUrl(true, "") + "/common/oauth2/v2.0/token"
|
||||||
var resp base.TokenResp
|
var resp base.TokenResp
|
||||||
|
@ -106,13 +96,6 @@ func (d *Onedrive) _refreshToken() error {
|
||||||
return nil
|
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) {
|
func (d *Onedrive) Request(url string, method string, callback func(*resty.Request), resp interface{}) ([]byte, error) {
|
||||||
req := base.RestyClient.R()
|
req := base.RestyClient.R()
|
||||||
req.SetHeader("Authorization", "Bearer "+d.AccessToken)
|
req.SetHeader("Authorization", "Bearer "+d.AccessToken)
|
||||||
|
|
|
@ -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)
|
|
@ -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)
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
package local
|
|
@ -0,0 +1,3 @@
|
||||||
|
package local
|
||||||
|
|
||||||
|
// do others that not defined in Driver interface
|
|
@ -76,7 +76,10 @@ func Get(ctx context.Context, storage driver.Driver, path string) (model.Obj, er
|
||||||
path = utils.StandardizePath(path)
|
path = utils.StandardizePath(path)
|
||||||
log.Debugf("operations.Get %s", path)
|
log.Debugf("operations.Get %s", path)
|
||||||
if g, ok := storage.(driver.Getter); ok {
|
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
|
// is root folder
|
||||||
if r, ok := storage.GetAddition().(driver.IRootFolderId); ok && utils.PathEqual(path, "/") {
|
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)
|
err = storage.Put(ctx, parentDir, file, up)
|
||||||
log.Debugf("put file [%s] done", file.GetName())
|
log.Debugf("put file [%s] done", file.GetName())
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
// set as complete
|
||||||
|
up(100)
|
||||||
// clear cache
|
// clear cache
|
||||||
key := stdpath.Join(storage.GetStorage().MountPath, dstDirPath)
|
key := stdpath.Join(storage.GetStorage().MountPath, dstDirPath)
|
||||||
filesCache.Del(key)
|
filesCache.Del(key)
|
||||||
|
|
Loading…
Reference in New Issue