fix: only refresh token while do request (close #3591)

This commit is contained in:
Andy Hsu 2023-02-24 20:31:12 +08:00
parent c8f3e8ab4d
commit ec54831162
3 changed files with 24 additions and 19 deletions

View File

@ -18,7 +18,8 @@ type Addition struct {
var config = driver.Config{ var config = driver.Config{
Name: "Aliyundrive", Name: "Aliyundrive",
DefaultRoot: "root", DefaultRoot: "root",
Alert: "warning|Deprecated, no longer maintained and will be removed in a future version", Alert: `warning|Deprecated, no longer maintained and will be removed in a future version.
We recommend using the official driver AliyundriveOpen.`,
} }
func init() { func init() {

View File

@ -5,24 +5,20 @@ import (
"io" "io"
"math" "math"
"net/http" "net/http"
"time"
"github.com/alist-org/alist/v3/drivers/base" "github.com/alist-org/alist/v3/drivers/base"
"github.com/alist-org/alist/v3/internal/driver" "github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/errs" "github.com/alist-org/alist/v3/internal/errs"
"github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op"
"github.com/alist-org/alist/v3/pkg/cron"
"github.com/alist-org/alist/v3/pkg/utils" "github.com/alist-org/alist/v3/pkg/utils"
"github.com/go-resty/resty/v2" "github.com/go-resty/resty/v2"
log "github.com/sirupsen/logrus"
) )
type AliyundriveOpen struct { type AliyundriveOpen struct {
model.Storage model.Storage
Addition Addition
base string base string
cron *cron.Cron //cron *cron.Cron
AccessToken string AccessToken string
DriveId string DriveId string
@ -46,22 +42,22 @@ func (d *AliyundriveOpen) Init(ctx context.Context) error {
return err return err
} }
d.DriveId = utils.Json.Get(res, "default_drive_id").ToString() d.DriveId = utils.Json.Get(res, "default_drive_id").ToString()
d.cron = cron.NewCron(time.Hour * 2) //d.cron = cron.NewCron(time.Hour * 2)
d.cron.Do(func() { //d.cron.Do(func() {
err := d.refreshToken() // err := d.refreshToken()
d.Status = err.Error() // d.Status = err.Error()
op.MustSaveDriverStorage(d) // op.MustSaveDriverStorage(d)
if err != nil { // if err != nil {
log.Errorf("%+v", err) // log.Errorf("%+v", err)
} // }
}) //})
return nil return nil
} }
func (d *AliyundriveOpen) Drop(ctx context.Context) error { func (d *AliyundriveOpen) Drop(ctx context.Context) error {
if d.cron != nil { //if d.cron != nil {
d.cron.Stop() // d.cron.Stop()
} //}
return nil return nil
} }

View File

@ -44,7 +44,7 @@ func (d *AliyundriveOpen) refreshToken() error {
return nil return nil
} }
func (d *AliyundriveOpen) request(uri, method string, callback base.ReqCallback) ([]byte, error) { func (d *AliyundriveOpen) request(uri, method string, callback base.ReqCallback, retry ...bool) ([]byte, error) {
req := base.RestyClient.R() req := base.RestyClient.R()
// TODO check whether access_token is expired // TODO check whether access_token is expired
req.SetHeader("Authorization", "Bearer "+d.AccessToken) req.SetHeader("Authorization", "Bearer "+d.AccessToken)
@ -60,7 +60,15 @@ func (d *AliyundriveOpen) request(uri, method string, callback base.ReqCallback)
if err != nil { if err != nil {
return nil, err return nil, err
} }
isRetry := len(retry) > 0 && retry[0]
if e.Code != "" { if e.Code != "" {
if !isRetry && e.Code == "AccessTokenInvalid" {
err = d.refreshToken()
if err != nil {
return nil, err
}
return d.request(uri, method, callback, true)
}
return nil, fmt.Errorf("%s:%s", e.Code, e.Message) return nil, fmt.Errorf("%s:%s", e.Code, e.Message)
} }
return res.Body(), nil return res.Body(), nil