✨ bootstrap
This commit is contained in:
@ -1,11 +1,25 @@
|
||||
package drivers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/go-resty/resty/v2"
|
||||
"github.com/robfig/cron/v3"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"time"
|
||||
)
|
||||
|
||||
var aliClient = resty.New()
|
||||
|
||||
func init() {
|
||||
aliClient.
|
||||
SetRetryCount(3).
|
||||
SetHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36").
|
||||
SetHeader("content-type", "application/json").
|
||||
SetHeader("origin", "https://aliyundrive.com")
|
||||
}
|
||||
|
||||
type AliDrive struct {
|
||||
}
|
||||
|
||||
@ -43,11 +57,11 @@ type AliFile struct {
|
||||
}
|
||||
|
||||
func (a AliDrive) Path(path string, account *model.Account) (*model.File, []*model.File, error) {
|
||||
_,err := conf.Cache.Get(conf.Ctx,path)
|
||||
_, err := conf.Cache.Get(conf.Ctx, path)
|
||||
if err == nil {
|
||||
// return
|
||||
}
|
||||
|
||||
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
@ -55,11 +69,61 @@ func (a AliDrive) Link(path string, account *model.Account) (string, error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
type AliTokenResp struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
}
|
||||
|
||||
func AliRefreshToken(refresh string) (string, string, error) {
|
||||
url := "https://auth.aliyundrive.com/v2/account/token"
|
||||
var resp AliTokenResp
|
||||
var e AliRespError
|
||||
_, err := aliClient.R().
|
||||
//ForceContentType("application/json").
|
||||
SetBody(JsonStr(Json{"refresh_token": refresh, "grant_type": "refresh_token"})).
|
||||
SetResult(&resp).
|
||||
SetError(&e).
|
||||
Post(url)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
log.Debugf("%+v,%+v", resp, e)
|
||||
if e.Code != "" {
|
||||
return "", "", fmt.Errorf("failed to refresh token: %s", e.Message)
|
||||
}
|
||||
return resp.RefreshToken, resp.AccessToken, nil
|
||||
}
|
||||
|
||||
func (a AliDrive) Save(account *model.Account, old *model.Account) error {
|
||||
if old != nil {
|
||||
// TODO clear something
|
||||
conf.Cron.Remove(cron.EntryID(old.CronId))
|
||||
}
|
||||
panic("implement me")
|
||||
refresh, access, err := AliRefreshToken(account.RefreshToken)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
account.RefreshToken, account.AccessToken = refresh, access
|
||||
cronId, err := conf.Cron.AddFunc("@every 2h", func() {
|
||||
name := account.Name
|
||||
newAccount, ok := model.GetAccount(name)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
newAccount.RefreshToken, newAccount.AccessToken, err = AliRefreshToken(newAccount.RefreshToken)
|
||||
if err != nil {
|
||||
newAccount.Status = err.Error()
|
||||
}
|
||||
_ = model.SaveAccount(newAccount)
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
account.CronId = int(cronId)
|
||||
err = model.SaveAccount(*account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var _ Driver = (*AliDrive)(nil)
|
||||
|
Reference in New Issue
Block a user