🎇 多盘支持

This commit is contained in:
微凉
2021-03-16 21:25:16 +08:00
parent 8e9ddcf81e
commit 4d0d892ce7
19 changed files with 177 additions and 173 deletions

View File

@@ -2,77 +2,44 @@ package alidrive
import (
"encoding/json"
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/utils"
log "github.com/sirupsen/logrus"
)
// use token login
func TokenLogin() (*TokenLoginResp, error) {
log.Infof("尝试使用token登录...")
url := "https://auth.aliyundrive.com/v2/oauth/token_login"
req := TokenLoginReq{Token: conf.Conf.AliDrive.LoginToken}
log.Debugf("token_login_req:%+v", req)
var tokenLogin TokenLoginResp
if body, err := DoPost(url, req, false); err != nil {
log.Errorf("tokenLogin-doPost出错:%s", err.Error())
return nil, err
} else {
if err = json.Unmarshal(body, &tokenLogin); err != nil {
log.Errorf("解析json[%s]出错:%s", string(body), err.Error())
return nil, err
}
}
if tokenLogin.IsAvailable() {
return &tokenLogin, nil
}
return nil, fmt.Errorf("登录token失效,请更换:%s", tokenLogin.Message)
}
// get access token
func GetToken(tokenLogin *TokenLoginResp) (*TokenResp, error) {
log.Infof("获取API token...")
url := "https://websv.aliyundrive.com/token/get"
code := utils.GetCode(tokenLogin.Goto)
if code == "" {
return nil, fmt.Errorf("获取code出错")
}
req := GetTokenReq{Code: code}
var token TokenResp
if body, err := DoPost(url, req, false); err != nil {
log.Errorf("tokenLogin-doPost出错:%s", err.Error())
return nil, err
} else {
if err = json.Unmarshal(body, &token); err != nil {
log.Errorf("解析json[%s]出错:%s", string(body), err.Error())
log.Errorf("此处json解析失败应该是code失效")
return nil, fmt.Errorf("code失效")
}
}
return &token, nil
}
// refresh access_token token by refresh_token
func RefreshToken() bool {
log.Infof("刷新token...")
func RefreshToken(drive *conf.Drive) bool {
log.Infof("刷新[%s]token...", drive.Name)
url := "https://websv.aliyundrive.com/token/refresh"
req := RefreshTokenReq{RefreshToken: conf.Conf.AliDrive.RefreshToken}
req := RefreshTokenReq{RefreshToken: drive.RefreshToken}
var token TokenResp
if body, err := DoPost(url, req, false); err != nil {
if body, err := DoPost(url, req, ""); err != nil {
log.Errorf("tokenLogin-doPost出错:%s", err.Error())
return false
} else {
if err = json.Unmarshal(body, &token); err != nil {
log.Errorf("解析json[%s]出错:%s", string(body), err.Error())
log.Errorf("此处json解析失败应该是refresh_token失效")
log.Errorf("此处json解析失败应该是[%s]refresh_token失效", drive.Name)
return false
}
}
//刷新成功 更新token并写入文件
conf.Conf.AliDrive.AccessToken = token.AccessToken
conf.Conf.AliDrive.RefreshToken = token.RefreshToken
conf.Authorization = token.TokenType + "\t" + token.AccessToken
utils.WriteToYml(conf.Con, conf.Conf)
//刷新成功 更新token
drive.AccessToken = token.AccessToken
drive.RefreshToken = token.RefreshToken
return true
}
func RefreshTokenAll() string {
log.Infof("刷新所有token...")
res := ""
for i, drive := range conf.Conf.AliDrive.Drives {
if !RefreshToken(&conf.Conf.AliDrive.Drives[i]) {
res = res + drive.Name + ","
}
}
utils.WriteToYml(conf.ConfigFile, conf.Conf)
if res != "" {
return res[:len(res)-1]
}
return ""
}

View File

@@ -1,5 +0,0 @@
package alidrive
var (
User *UserInfo
)

View File

@@ -13,41 +13,41 @@ import (
)
// get file
func GetFile(fileId string) (*File, error) {
func GetFile(fileId string, drive *conf.Drive) (*File, error) {
url := conf.Conf.AliDrive.ApiUrl + "/file/get"
req := GetReq{
DriveId: User.DefaultDriveId,
DriveId: drive.DefaultDriveId,
FileId: fileId,
ImageThumbnailProcess: conf.ImageThumbnailProcess,
VideoThumbnailProcess: conf.VideoThumbnailProcess,
}
var resp File
if err := BodyToJson(url, req, &resp, true); err != nil {
if err := BodyToJson(url, req, &resp, drive); err != nil {
return nil, err
}
return &resp, nil
}
// get download_url
func GetDownLoadUrl(fileId string) (*DownloadResp, error) {
func GetDownLoadUrl(fileId string, drive *conf.Drive) (*DownloadResp, error) {
url := conf.Conf.AliDrive.ApiUrl + "/file/get_download_url"
req := DownloadReq{
DriveId: User.DefaultDriveId,
DriveId: drive.DefaultDriveId,
FileId: fileId,
ExpireSec: 14400,
}
var resp DownloadResp
if err := BodyToJson(url, req, &resp, true); err != nil {
if err := BodyToJson(url, req, &resp, drive); err != nil {
return nil, err
}
return &resp, nil
}
// search by keyword
func Search(key string, limit int, marker string) (*Files, error) {
func Search(key string, limit int, marker string, drive *conf.Drive) (*Files, error) {
url := conf.Conf.AliDrive.ApiUrl + "/file/search"
req := SearchReq{
DriveId: User.DefaultDriveId,
DriveId: drive.DefaultDriveId,
ImageThumbnailProcess: conf.ImageThumbnailProcess,
ImageUrlProcess: conf.ImageUrlProcess,
Limit: limit,
@@ -57,22 +57,22 @@ func Search(key string, limit int, marker string) (*Files, error) {
VideoThumbnailProcess: conf.VideoThumbnailProcess,
}
var resp Files
if err := BodyToJson(url, req, &resp, true); err != nil {
if err := BodyToJson(url, req, &resp, drive); err != nil {
return nil, err
}
return &resp, nil
}
// get root folder
func GetRoot(limit int, marker string, orderBy string, orderDirection string) (*Files, error) {
return GetList(conf.Conf.AliDrive.RootFolder, limit, marker, orderBy, orderDirection)
func GetRoot(limit int, marker string, orderBy string, orderDirection string, drive *conf.Drive) (*Files, error) {
return GetList(drive.RootFolder, limit, marker, orderBy, orderDirection, drive)
}
// get folder list by file_id
func GetList(parent string, limit int, marker string, orderBy string, orderDirection string) (*Files, error) {
func GetList(parent string, limit int, marker string, orderBy string, orderDirection string, drive *conf.Drive) (*Files, error) {
url := conf.Conf.AliDrive.ApiUrl + "/file/list"
req := ListReq{
DriveId: User.DefaultDriveId,
DriveId: drive.DefaultDriveId,
Fields: "*",
ImageThumbnailProcess: conf.ImageThumbnailProcess,
ImageUrlProcess: conf.ImageUrlProcess,
@@ -84,40 +84,40 @@ func GetList(parent string, limit int, marker string, orderBy string, orderDirec
VideoThumbnailProcess: conf.VideoThumbnailProcess,
}
var resp Files
if err := BodyToJson(url, req, &resp, true); err != nil {
if err := BodyToJson(url, req, &resp, drive); err != nil {
return nil, err
}
return &resp, nil
}
// get user info
func GetUserInfo() (*UserInfo, error) {
func GetUserInfo(drive *conf.Drive) (*UserInfo, error) {
url := conf.Conf.AliDrive.ApiUrl + "/user/get"
var resp UserInfo
if err := BodyToJson(url, map[string]interface{}{}, &resp, true); err != nil {
if err := BodyToJson(url, map[string]interface{}{}, &resp, drive); err != nil {
return nil, err
}
return &resp, nil
}
// get office preview url and token
func GetOfficePreviewUrl(fileId string) (*OfficePreviewUrlResp, error) {
func GetOfficePreviewUrl(fileId string, drive *conf.Drive) (*OfficePreviewUrlResp, error) {
url := conf.Conf.AliDrive.ApiUrl + "/file/get_office_preview_url"
req := OfficePreviewUrlReq{
AccessToken: conf.Conf.AliDrive.AccessToken,
DriveId: User.DefaultDriveId,
AccessToken: drive.AccessToken,
DriveId: drive.DefaultDriveId,
FileId: fileId,
}
var resp OfficePreviewUrlResp
if err := BodyToJson(url, req, &resp, true); err != nil {
if err := BodyToJson(url, req, &resp, drive); err != nil {
return nil, err
}
return &resp, nil
}
// convert body to json
func BodyToJson(url string, req interface{}, resp RespHandle, auth bool) error {
if body, err := DoPost(url, req, auth); err != nil {
func BodyToJson(url string, req interface{}, resp RespHandle, drive *conf.Drive) error {
if body, err := DoPost(url, req, drive.AccessToken); err != nil {
log.Errorf("doPost出错:%s", err.Error())
return err
} else {
@@ -131,15 +131,15 @@ func BodyToJson(url string, req interface{}, resp RespHandle, auth bool) error {
}
if resp.GetCode() == conf.AccessTokenInvalid {
resp.SetCode("")
if RefreshToken() {
return BodyToJson(url, req, resp, auth)
if RefreshToken(drive) {
return BodyToJson(url, req, resp, drive)
}
}
return fmt.Errorf(resp.GetMessage())
}
// do post request
func DoPost(url string, request interface{}, auth bool) (body []byte, err error) {
func DoPost(url string, request interface{}, auth string) (body []byte, err error) {
var (
resp *http.Response
)
@@ -154,8 +154,8 @@ func DoPost(url string, request interface{}, auth bool) (body []byte, err error)
log.Errorf("创建request出错:%s", err.Error())
return
}
if auth {
req.Header.Set("authorization", conf.Authorization)
if auth != "" {
req.Header.Set("authorization", conf.Bearer + auth)
}
req.Header.Add("content-type", "application/json")
req.Header.Add("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")
@@ -181,24 +181,3 @@ func DoPost(url string, request interface{}, auth bool) (body []byte, err error)
log.Debugf("请求返回信息:%s", string(body))
return
}
func GetPaths(fileId string) (*[]Path, error) {
paths := make([]Path, 0)
for fileId != conf.Conf.AliDrive.RootFolder && fileId != "root" {
file, err := GetFile(fileId)
if err != nil {
log.Errorf("获取path出错:%s", err.Error())
return nil, err
}
paths = append(paths, Path{
Name: file.Name,
FileId: file.FileId,
})
fileId = file.ParentFileId
}
paths = append(paths, Path{
Name: "Root",
FileId: "root",
})
return &paths, nil
}