From 8e0ab8f7803bfdf81718016303e071c798ab7ffc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=AE=E5=87=89?= Date: Tue, 1 Feb 2022 15:20:17 +0800 Subject: [PATCH] :fire: remove onedrive refresh token cron --- drivers/onedrive/driver.go | 42 ++++++++++++++++-------------------- drivers/onedrive/onedrive.go | 36 +++++++++++++++---------------- 2 files changed, 37 insertions(+), 41 deletions(-) diff --git a/drivers/onedrive/driver.go b/drivers/onedrive/driver.go index e0b80a8a..353e03e2 100644 --- a/drivers/onedrive/driver.go +++ b/drivers/onedrive/driver.go @@ -6,7 +6,6 @@ import ( "github.com/Xhofe/alist/drivers/base" "github.com/Xhofe/alist/model" "github.com/Xhofe/alist/utils" - "github.com/robfig/cron/v3" log "github.com/sirupsen/logrus" "net/http" "path/filepath" @@ -92,9 +91,9 @@ func (driver Onedrive) Items() []base.Item { } func (driver Onedrive) Save(account *model.Account, old *model.Account) error { - if old != nil { - conf.Cron.Remove(cron.EntryID(old.CronId)) - } + //if old != nil { + // conf.Cron.Remove(cron.EntryID(old.CronId)) + //} if account == nil { return nil } @@ -104,28 +103,25 @@ func (driver Onedrive) Save(account *model.Account, old *model.Account) error { } account.RootFolder = utils.ParsePath(account.RootFolder) err := driver.RefreshToken(account) + _ = model.SaveAccount(account) if err != nil { return err } - cronId, err := conf.Cron.AddFunc("@every 1h", func() { - name := account.Name - log.Debugf("onedrive account name: %s", name) - newAccount, ok := model.GetAccount(name) - log.Debugf("onedrive account: %+v", newAccount) - if !ok { - return - } - err = driver.RefreshToken(&newAccount) - _ = model.SaveAccount(&newAccount) - }) - if err != nil { - return err - } - account.CronId = int(cronId) - err = model.SaveAccount(account) - if err != nil { - return err - } + //cronId, err := conf.Cron.AddFunc("@every 1h", func() { + // name := account.Name + // log.Debugf("onedrive account name: %s", name) + // newAccount, ok := model.GetAccount(name) + // log.Debugf("onedrive account: %+v", newAccount) + // if !ok { + // return + // } + // err = driver.RefreshToken(&newAccount) + // _ = model.SaveAccount(&newAccount) + //}) + //if err != nil { + // return err + //} + //account.CronId = int(cronId) return nil } diff --git a/drivers/onedrive/onedrive.go b/drivers/onedrive/onedrive.go index df46c5cc..322e45c1 100644 --- a/drivers/onedrive/onedrive.go +++ b/drivers/onedrive/onedrive.go @@ -19,8 +19,6 @@ import ( "time" ) -var oneClient = resty.New() - type Host struct { Oauth string Api string @@ -91,7 +89,7 @@ func (driver Onedrive) refreshToken(account *model.Account) error { url := driver.GetMetaUrl(account, true, "") + "/common/oauth2/v2.0/token" var resp base.TokenResp var e OneTokenErr - _, err := oneClient.R().SetResult(&resp).SetError(&e).SetFormData(map[string]string{ + _, err := base.RestyClient.R().SetResult(&resp).SetError(&e).SetFormData(map[string]string{ "grant_type": "refresh_token", "client_id": account.ClientId, "client_secret": account.ClientSecret, @@ -178,16 +176,17 @@ func (driver Onedrive) GetFiles(account *model.Account, path string) ([]OneFile, } for nextLink != "" { var files OneFiles - var e OneRespErr - _, err := oneClient.R().SetResult(&files).SetError(&e). - SetHeader("Authorization", "Bearer "+account.AccessToken). - Get(nextLink) + _, err := driver.Request(nextLink, base.Get, nil, nil, nil, nil, &files, account) + //var e OneRespErr + //_, err := oneClient.R().SetResult(&files).SetError(&e). + // SetHeader("Authorization", "Bearer "+account.AccessToken). + // Get(nextLink) if err != nil { return nil, err } - if e.Error.Code != "" { - return nil, fmt.Errorf("%s", e.Error.Message) - } + //if e.Error.Code != "" { + // return nil, fmt.Errorf("%s", e.Error.Message) + //} res = append(res, files.Value...) nextLink = files.NextLink } @@ -196,16 +195,18 @@ func (driver Onedrive) GetFiles(account *model.Account, path string) ([]OneFile, func (driver Onedrive) GetFile(account *model.Account, path string) (*OneFile, error) { var file OneFile - var e OneRespErr - _, err := oneClient.R().SetResult(&file).SetError(&e). - SetHeader("Authorization", "Bearer "+account.AccessToken). - Get(driver.GetMetaUrl(account, false, path)) + //var e OneRespErr + u := driver.GetMetaUrl(account, false, path) + _, err := driver.Request(u, base.Get, nil, nil, nil, nil, &file, account) + //_, err := oneClient.R().SetResult(&file).SetError(&e). + // SetHeader("Authorization", "Bearer "+account.AccessToken). + // Get(driver.GetMetaUrl(account, false, path)) if err != nil { return nil, err } - if e.Error.Code != "" { - return nil, fmt.Errorf("%s", e.Error.Message) - } + //if e.Error.Code != "" { + // return nil, fmt.Errorf("%s", e.Error.Message) + //} return &file, nil } @@ -314,5 +315,4 @@ func (driver Onedrive) UploadBig(file *model.FileStream, account *model.Account) func init() { base.RegisterDriver(&Onedrive{}) - oneClient.SetRetryCount(3) }