Skip to content

Commit

Permalink
fix: only refresh token while do request (close #3591)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Feb 24, 2023
1 parent c8f3e8a commit ec54831
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
3 changes: 2 additions & 1 deletion drivers/aliyundrive/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
30 changes: 13 additions & 17 deletions drivers/aliyundrive_open/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
10 changes: 9 additions & 1 deletion drivers/aliyundrive_open/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit ec54831

Please sign in to comment.