mirror of
https://github.com/AlistGo/alist.git
synced 2025-04-23 13:54:04 +08:00
fix: only refresh token while do request (close #3591)
This commit is contained in:
parent
c8f3e8ab4d
commit
ec54831162
@ -18,7 +18,8 @@ type Addition struct {
|
||||
var config = driver.Config{
|
||||
Name: "Aliyundrive",
|
||||
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() {
|
||||
|
@ -5,24 +5,20 @@ import (
|
||||
"io"
|
||||
"math"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/alist-org/alist/v3/drivers/base"
|
||||
"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/internal/op"
|
||||
"github.com/alist-org/alist/v3/pkg/cron"
|
||||
"github.com/alist-org/alist/v3/pkg/utils"
|
||||
"github.com/go-resty/resty/v2"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type AliyundriveOpen struct {
|
||||
model.Storage
|
||||
Addition
|
||||
base string
|
||||
cron *cron.Cron
|
||||
//cron *cron.Cron
|
||||
|
||||
AccessToken string
|
||||
DriveId string
|
||||
@ -46,22 +42,22 @@ func (d *AliyundriveOpen) Init(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
d.DriveId = utils.Json.Get(res, "default_drive_id").ToString()
|
||||
d.cron = cron.NewCron(time.Hour * 2)
|
||||
d.cron.Do(func() {
|
||||
err := d.refreshToken()
|
||||
d.Status = err.Error()
|
||||
op.MustSaveDriverStorage(d)
|
||||
if err != nil {
|
||||
log.Errorf("%+v", err)
|
||||
}
|
||||
})
|
||||
//d.cron = cron.NewCron(time.Hour * 2)
|
||||
//d.cron.Do(func() {
|
||||
// err := d.refreshToken()
|
||||
// d.Status = err.Error()
|
||||
// op.MustSaveDriverStorage(d)
|
||||
// if err != nil {
|
||||
// log.Errorf("%+v", err)
|
||||
// }
|
||||
//})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *AliyundriveOpen) Drop(ctx context.Context) error {
|
||||
if d.cron != nil {
|
||||
d.cron.Stop()
|
||||
}
|
||||
//if d.cron != nil {
|
||||
// d.cron.Stop()
|
||||
//}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -44,7 +44,7 @@ func (d *AliyundriveOpen) refreshToken() error {
|
||||
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()
|
||||
// TODO check whether access_token is expired
|
||||
req.SetHeader("Authorization", "Bearer "+d.AccessToken)
|
||||
@ -60,7 +60,15 @@ func (d *AliyundriveOpen) request(uri, method string, callback base.ReqCallback)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
isRetry := len(retry) > 0 && retry[0]
|
||||
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 res.Body(), nil
|
||||
|
Loading…
x
Reference in New Issue
Block a user