2021-12-06 07:55:05 +00:00
|
|
|
package alidrive
|
2021-11-27 11:40:36 +00:00
|
|
|
|
|
|
|
import (
|
2021-12-06 09:49:20 +00:00
|
|
|
"errors"
|
2021-11-27 11:40:36 +00:00
|
|
|
"fmt"
|
2021-12-06 07:55:05 +00:00
|
|
|
"github.com/Xhofe/alist/drivers/base"
|
2021-11-27 11:40:36 +00:00
|
|
|
"github.com/Xhofe/alist/model"
|
|
|
|
"github.com/Xhofe/alist/utils"
|
|
|
|
"github.com/go-resty/resty/v2"
|
2022-02-23 06:56:17 +00:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2021-11-27 11:40:36 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
var aliClient = resty.New()
|
|
|
|
|
|
|
|
func (driver AliDrive) FormatFile(file *AliFile) *model.File {
|
|
|
|
f := &model.File{
|
|
|
|
Id: file.FileId,
|
|
|
|
Name: file.Name,
|
|
|
|
Size: file.Size,
|
|
|
|
UpdatedAt: file.UpdatedAt,
|
|
|
|
Thumbnail: file.Thumbnail,
|
2021-11-30 01:37:51 +00:00
|
|
|
Driver: driver.Config().Name,
|
2021-11-27 11:40:36 +00:00
|
|
|
Url: file.Url,
|
|
|
|
}
|
2022-04-08 13:51:21 +00:00
|
|
|
f.Type = file.GetType()
|
2021-11-27 11:40:36 +00:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
func (driver AliDrive) GetFiles(fileId string, account *model.Account) ([]AliFile, error) {
|
|
|
|
marker := "first"
|
|
|
|
res := make([]AliFile, 0)
|
|
|
|
for marker != "" {
|
|
|
|
if marker == "first" {
|
|
|
|
marker = ""
|
|
|
|
}
|
|
|
|
var resp AliFiles
|
|
|
|
var e AliRespError
|
|
|
|
_, err := aliClient.R().
|
|
|
|
SetResult(&resp).
|
|
|
|
SetError(&e).
|
|
|
|
SetHeader("authorization", "Bearer\t"+account.AccessToken).
|
2021-12-06 07:55:05 +00:00
|
|
|
SetBody(base.Json{
|
2021-11-27 11:40:36 +00:00
|
|
|
"drive_id": account.DriveId,
|
|
|
|
"fields": "*",
|
|
|
|
"image_thumbnail_process": "image/resize,w_400/format,jpeg",
|
|
|
|
"image_url_process": "image/resize,w_1920/format,jpeg",
|
|
|
|
"limit": account.Limit,
|
|
|
|
"marker": marker,
|
|
|
|
"order_by": account.OrderBy,
|
|
|
|
"order_direction": account.OrderDirection,
|
|
|
|
"parent_file_id": fileId,
|
|
|
|
"video_thumbnail_process": "video/snapshot,t_0,f_jpg,ar_auto,w_300",
|
|
|
|
"url_expire_sec": 14400,
|
|
|
|
}).Post("https://api.aliyundrive.com/v2/file/list")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if e.Code != "" {
|
|
|
|
if e.Code == "AccessTokenInvalid" {
|
|
|
|
err = driver.RefreshToken(account)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
_ = model.SaveAccount(account)
|
|
|
|
return driver.GetFiles(fileId, account)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("%s", e.Message)
|
|
|
|
}
|
|
|
|
marker = resp.NextMarker
|
|
|
|
res = append(res, resp.Items...)
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (driver AliDrive) GetFile(path string, account *model.Account) (*AliFile, error) {
|
|
|
|
dir, name := filepath.Split(path)
|
2021-11-28 07:10:06 +00:00
|
|
|
dir = utils.ParsePath(dir)
|
2021-11-27 11:40:36 +00:00
|
|
|
_, err := driver.Files(dir, account)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-08 14:58:44 +00:00
|
|
|
parentFiles_, _ := base.GetCache(dir, account)
|
2021-11-27 11:40:36 +00:00
|
|
|
parentFiles, _ := parentFiles_.([]AliFile)
|
|
|
|
for _, file := range parentFiles {
|
|
|
|
if file.Name == name {
|
|
|
|
if file.Type == "file" {
|
|
|
|
return &file, err
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("not file")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-06 07:55:05 +00:00
|
|
|
return nil, base.ErrPathNotFound
|
2021-11-27 11:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (driver AliDrive) RefreshToken(account *model.Account) error {
|
|
|
|
url := "https://auth.aliyundrive.com/v2/account/token"
|
2021-12-06 07:55:05 +00:00
|
|
|
var resp base.TokenResp
|
2021-11-27 11:40:36 +00:00
|
|
|
var e AliRespError
|
|
|
|
_, err := aliClient.R().
|
|
|
|
//ForceContentType("application/json").
|
2021-12-06 07:55:05 +00:00
|
|
|
SetBody(base.Json{"refresh_token": account.RefreshToken, "grant_type": "refresh_token"}).
|
2021-11-27 11:40:36 +00:00
|
|
|
SetResult(&resp).
|
|
|
|
SetError(&e).
|
|
|
|
Post(url)
|
|
|
|
if err != nil {
|
|
|
|
account.Status = err.Error()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Debugf("%+v,%+v", resp, e)
|
|
|
|
if e.Code != "" {
|
|
|
|
account.Status = e.Message
|
|
|
|
return fmt.Errorf("failed to refresh token: %s", e.Message)
|
|
|
|
} else {
|
|
|
|
account.Status = "work"
|
|
|
|
}
|
|
|
|
account.RefreshToken, account.AccessToken = resp.RefreshToken, resp.AccessToken
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-03 12:06:36 +00:00
|
|
|
func (driver AliDrive) rename(fileId, name string, account *model.Account) error {
|
2021-12-06 09:49:20 +00:00
|
|
|
var resp base.Json
|
|
|
|
var e AliRespError
|
|
|
|
_, err := aliClient.R().SetResult(&resp).SetError(&e).
|
|
|
|
SetHeader("authorization", "Bearer\t"+account.AccessToken).
|
|
|
|
SetBody(base.Json{
|
|
|
|
"check_name_mode": "refuse",
|
|
|
|
"drive_id": account.DriveId,
|
|
|
|
"file_id": fileId,
|
|
|
|
"name": name,
|
|
|
|
}).Post("https://api.aliyundrive.com/v3/file/update")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if e.Code != "" {
|
|
|
|
if e.Code == "AccessTokenInvalid" {
|
|
|
|
err = driver.RefreshToken(account)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
_ = model.SaveAccount(account)
|
2022-01-03 12:06:36 +00:00
|
|
|
return driver.rename(fileId, name, account)
|
2021-12-06 09:49:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("%s", e.Message)
|
|
|
|
}
|
|
|
|
if resp["name"] == name {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("%+v", resp)
|
|
|
|
}
|
|
|
|
|
2022-02-23 06:56:17 +00:00
|
|
|
func (driver AliDrive) batch(srcId, dstId string, url string, account *model.Account) error {
|
2021-12-06 09:49:20 +00:00
|
|
|
var e AliRespError
|
|
|
|
res, err := aliClient.R().SetError(&e).
|
|
|
|
SetHeader("authorization", "Bearer\t"+account.AccessToken).
|
|
|
|
SetBody(base.Json{
|
|
|
|
"requests": []base.Json{
|
|
|
|
{
|
|
|
|
"headers": base.Json{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
2022-01-03 12:06:36 +00:00
|
|
|
"method": "POST",
|
|
|
|
"id": srcId,
|
|
|
|
"body": base.Json{
|
|
|
|
"drive_id": account.DriveId,
|
|
|
|
"file_id": srcId,
|
|
|
|
"to_drive_id": account.DriveId,
|
|
|
|
"to_parent_file_id": dstId,
|
2021-12-06 09:49:20 +00:00
|
|
|
},
|
2022-02-23 06:56:17 +00:00
|
|
|
"url": url,
|
2021-12-06 09:49:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"resource": "file",
|
|
|
|
}).Post("https://api.aliyundrive.com/v3/batch")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if e.Code != "" {
|
|
|
|
if e.Code == "AccessTokenInvalid" {
|
|
|
|
err = driver.RefreshToken(account)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
_ = model.SaveAccount(account)
|
2022-02-23 06:56:17 +00:00
|
|
|
return driver.batch(srcId, dstId, url, account)
|
2021-12-06 09:49:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("%s", e.Message)
|
|
|
|
}
|
2022-03-28 13:51:24 +00:00
|
|
|
status := jsoniter.Get(res.Body(), "responses", 0, "status").ToInt()
|
2022-02-23 06:56:17 +00:00
|
|
|
if status < 400 && status >= 100 {
|
2021-12-06 09:49:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New(res.String())
|
|
|
|
}
|
|
|
|
|
2021-11-27 11:40:36 +00:00
|
|
|
func init() {
|
2021-12-06 07:55:05 +00:00
|
|
|
base.RegisterDriver(&AliDrive{})
|
2021-11-27 11:40:36 +00:00
|
|
|
aliClient.
|
2022-02-14 06:59:00 +00:00
|
|
|
SetTimeout(base.DefaultTimeout).
|
2021-11-27 11:40:36 +00:00
|
|
|
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").
|
2021-12-07 08:32:59 +00:00
|
|
|
SetHeader("origin", "https://www.aliyundrive.com")
|
2021-11-27 11:40:36 +00:00
|
|
|
}
|