wip: dropbox surpport

This commit is contained in:
Andy Hsu
2023-06-23 14:06:32 +08:00
parent 84e23c397d
commit be0d61e926
7 changed files with 526 additions and 0 deletions

84
drivers/dropbox/driver.go Normal file
View File

@ -0,0 +1,84 @@
package template
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/dropbox/dropbox-sdk-go-unofficial/v6/dropbox"
"github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox/files"
)
type Dropbox struct {
model.Storage
Addition
dbx files.Client
}
func (d *Dropbox) Config() driver.Config {
return config
}
func (d *Dropbox) GetAddition() driver.Additional {
return &d.Addition
}
func (d *Dropbox) Init(ctx context.Context) error {
cfg := dropbox.Config{
Token: d.AccessToken,
}
d.dbx = files.New(cfg)
return nil
}
func (d *Dropbox) Drop(ctx context.Context) error {
return nil
}
func (d *Dropbox) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
// TODO return the files list, required
return nil, errs.NotImplement
}
func (d *Dropbox) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
// TODO return link of file, required
return nil, errs.NotImplement
}
func (d *Dropbox) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
// TODO create folder, optional
return errs.NotImplement
}
func (d *Dropbox) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
// TODO move obj, optional
return errs.NotImplement
}
func (d *Dropbox) Rename(ctx context.Context, srcObj model.Obj, newName string) error {
// TODO rename obj, optional
return errs.NotImplement
}
func (d *Dropbox) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
// TODO copy obj, optional
return errs.NotImplement
}
func (d *Dropbox) Remove(ctx context.Context, obj model.Obj) error {
// TODO remove obj, optional
return errs.NotImplement
}
func (d *Dropbox) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
// TODO upload file, optional
return errs.NotImplement
}
//func (d *Dropbox) Other(ctx context.Context, args model.OtherArgs) (interface{}, error) {
// return nil, errs.NotSupport
//}
var _ driver.Driver = (*Dropbox)(nil)

39
drivers/dropbox/meta.go Normal file
View File

@ -0,0 +1,39 @@
package template
import (
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/op"
)
type Addition struct {
// Usually one of two
driver.RootPath
driver.RootID
// define other
RefreshToken string `json:"refresh_token" required:"true"`
OauthTokenURL string `json:"oauth_token_url" default:"https://api.xhofe.top/alist/dropbox/token"`
ClientID string `json:"client_id" required:"false" help:"Keep it empty if you don't have one"`
ClientSecret string `json:"client_secret" required:"false" help:"Keep it empty if you don't have one"`
AccessToken string
}
var config = driver.Config{
Name: "Dropbox",
LocalSort: false,
OnlyLocal: false,
OnlyProxy: false,
NoCache: false,
NoUpload: false,
NeedMs: false,
DefaultRoot: "",
CheckStatus: false,
Alert: "",
NoOverwriteUpload: false,
}
func init() {
op.RegisterDriver(func() driver.Driver {
return &Dropbox{}
})
}

25
drivers/dropbox/retry.go Normal file
View File

@ -0,0 +1,25 @@
package template
import (
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox/auth"
)
type f[A any, R any] func(a A) (R, error)
type retry[A any, R any] struct {
f f[A, R]
beforeRetry func()
}
func (r retry[A, R]) call(arg A) (R, error) {
res, err := r.f(arg)
if err == nil || r.beforeRetry == nil || !utils.SliceContains([]string{
auth.AuthErrorExpiredAccessToken,
auth.AuthErrorInvalidAccessToken,
}, err.Error()) {
return res, nil
}
r.beforeRetry()
return r.f(arg)
}

1
drivers/dropbox/types.go Normal file
View File

@ -0,0 +1 @@
package template

50
drivers/dropbox/util.go Normal file
View File

@ -0,0 +1,50 @@
package template
import (
"errors"
"fmt"
"github.com/alist-org/alist/v3/drivers/base"
"github.com/alist-org/alist/v3/internal/op"
"github.com/alist-org/alist/v3/pkg/utils"
)
// do others that not defined in Driver interface
const (
DefaultClient = "76lrwrklhdn1icb"
)
func (d *Dropbox) refreshToken() error {
url := "https://api.dropboxapi.com" + "/oauth/access_token"
if d.OauthTokenURL != "" && utils.SliceContains([]string{"", "76lrwrklhdn1icb"}, d.ClientID) {
url = d.OauthTokenURL
}
var resp base.TokenResp
var e struct {
Error string `json:"error"`
}
_, err := base.RestyClient.R().
ForceContentType("application/json").
SetBody(base.Json{
"client_id": d.ClientID,
"client_secret": d.ClientSecret,
"grant_type": "refresh_token",
"refresh_token": d.RefreshToken,
}).
SetResult(&resp).
SetError(&e).
Post(url)
if err != nil {
return err
}
if e.Error != "" {
return fmt.Errorf("failed to refresh token: %s", e.Error)
}
if resp.RefreshToken == "" {
return errors.New("failed to refresh token: refresh token is empty")
}
d.RefreshToken, d.AccessToken = resp.RefreshToken, resp.AccessToken
op.MustSaveDriverStorage(d)
return nil
}