2022-09-08 07:00:57 +00:00
|
|
|
package _189pc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
2023-08-09 08:13:09 +00:00
|
|
|
"strconv"
|
2022-09-08 07:00:57 +00:00
|
|
|
"strings"
|
2023-08-03 06:05:33 +00:00
|
|
|
"time"
|
2022-09-08 07:00:57 +00:00
|
|
|
|
|
|
|
"github.com/alist-org/alist/v3/drivers/base"
|
|
|
|
"github.com/alist-org/alist/v3/internal/driver"
|
2023-08-03 06:05:33 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/errs"
|
2022-09-08 07:00:57 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/model"
|
|
|
|
"github.com/alist-org/alist/v3/pkg/utils"
|
|
|
|
"github.com/go-resty/resty/v2"
|
|
|
|
)
|
|
|
|
|
2022-12-21 07:03:09 +00:00
|
|
|
type Cloud189PC struct {
|
2022-09-08 07:00:57 +00:00
|
|
|
model.Storage
|
|
|
|
Addition
|
|
|
|
|
|
|
|
identity string
|
|
|
|
|
2023-05-27 06:28:58 +00:00
|
|
|
client *resty.Client
|
2022-09-08 07:00:57 +00:00
|
|
|
|
|
|
|
loginParam *LoginParam
|
|
|
|
tokenInfo *AppSessionResp
|
2023-08-09 08:13:09 +00:00
|
|
|
|
|
|
|
uploadThread int
|
2023-09-06 06:46:35 +00:00
|
|
|
|
|
|
|
storageConfig driver.Config
|
2022-09-08 07:00:57 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 07:03:09 +00:00
|
|
|
func (y *Cloud189PC) Config() driver.Config {
|
2023-09-06 06:46:35 +00:00
|
|
|
if y.storageConfig.Name == "" {
|
|
|
|
y.storageConfig = config
|
|
|
|
}
|
|
|
|
return y.storageConfig
|
2022-09-08 07:00:57 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 07:03:09 +00:00
|
|
|
func (y *Cloud189PC) GetAddition() driver.Additional {
|
2022-12-13 10:03:30 +00:00
|
|
|
return &y.Addition
|
2022-09-08 07:00:57 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 07:03:09 +00:00
|
|
|
func (y *Cloud189PC) Init(ctx context.Context) (err error) {
|
2023-09-06 06:46:35 +00:00
|
|
|
// 兼容旧上传接口
|
|
|
|
y.storageConfig.NoOverwriteUpload = y.isFamily() && (y.Addition.RapidUpload || y.Addition.UploadMethod == "old")
|
|
|
|
|
2022-09-08 07:00:57 +00:00
|
|
|
// 处理个人云和家庭云参数
|
|
|
|
if y.isFamily() && y.RootFolderID == "-11" {
|
|
|
|
y.RootFolderID = ""
|
|
|
|
}
|
|
|
|
if !y.isFamily() && y.RootFolderID == "" {
|
|
|
|
y.RootFolderID = "-11"
|
|
|
|
y.FamilyID = ""
|
|
|
|
}
|
|
|
|
|
2023-08-09 08:13:09 +00:00
|
|
|
// 限制上传线程数
|
|
|
|
y.uploadThread, _ = strconv.Atoi(y.UploadThread)
|
|
|
|
if y.uploadThread < 1 || y.uploadThread > 32 {
|
|
|
|
y.uploadThread, y.UploadThread = 3, "3"
|
|
|
|
}
|
|
|
|
|
2022-09-08 07:00:57 +00:00
|
|
|
// 初始化请求客户端
|
|
|
|
if y.client == nil {
|
|
|
|
y.client = base.NewRestyClient().SetHeaders(map[string]string{
|
|
|
|
"Accept": "application/json;charset=UTF-8",
|
|
|
|
"Referer": WEB_URL,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// 避免重复登陆
|
2023-07-15 08:28:55 +00:00
|
|
|
identity := utils.GetMD5EncodeStr(y.Username + y.Password)
|
2022-09-08 07:00:57 +00:00
|
|
|
if !y.isLogin() || y.identity != identity {
|
|
|
|
y.identity = identity
|
|
|
|
if err = y.login(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 处理家庭云ID
|
|
|
|
if y.isFamily() && y.FamilyID == "" {
|
|
|
|
if y.FamilyID, err = y.getFamilyID(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-21 07:03:09 +00:00
|
|
|
func (y *Cloud189PC) Drop(ctx context.Context) error {
|
2022-09-08 07:00:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-21 07:03:09 +00:00
|
|
|
func (y *Cloud189PC) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
|
2022-09-08 07:00:57 +00:00
|
|
|
return y.getFiles(ctx, dir.GetID())
|
|
|
|
}
|
|
|
|
|
2022-12-21 07:03:09 +00:00
|
|
|
func (y *Cloud189PC) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
|
2022-09-08 07:00:57 +00:00
|
|
|
var downloadUrl struct {
|
|
|
|
URL string `json:"fileDownloadUrl"`
|
|
|
|
}
|
|
|
|
|
|
|
|
fullUrl := API_URL
|
|
|
|
if y.isFamily() {
|
|
|
|
fullUrl += "/family/file"
|
|
|
|
}
|
|
|
|
fullUrl += "/getFileDownloadUrl.action"
|
|
|
|
|
|
|
|
_, err := y.get(fullUrl, func(r *resty.Request) {
|
|
|
|
r.SetContext(ctx)
|
|
|
|
r.SetQueryParam("fileId", file.GetID())
|
|
|
|
if y.isFamily() {
|
|
|
|
r.SetQueryParams(map[string]string{
|
|
|
|
"familyId": y.FamilyID,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
r.SetQueryParams(map[string]string{
|
|
|
|
"dt": "3",
|
|
|
|
"flag": "1",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}, &downloadUrl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 重定向获取真实链接
|
|
|
|
downloadUrl.URL = strings.Replace(strings.ReplaceAll(downloadUrl.URL, "&", "&"), "http://", "https://", 1)
|
2023-09-06 06:46:35 +00:00
|
|
|
res, err := base.NoRedirectClient.R().SetContext(ctx).Head(downloadUrl.URL)
|
2022-09-08 07:00:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if res.StatusCode() == 302 {
|
|
|
|
downloadUrl.URL = res.Header().Get("location")
|
|
|
|
}
|
|
|
|
|
|
|
|
like := &model.Link{
|
|
|
|
URL: downloadUrl.URL,
|
|
|
|
Header: http.Header{
|
|
|
|
"User-Agent": []string{base.UserAgent},
|
|
|
|
},
|
|
|
|
}
|
2022-09-13 10:44:07 +00:00
|
|
|
/*
|
|
|
|
// 获取链接有效时常
|
|
|
|
strs := regexp.MustCompile(`(?i)expire[^=]*=([0-9]*)`).FindStringSubmatch(downloadUrl.URL)
|
|
|
|
if len(strs) == 2 {
|
|
|
|
timestamp, err := strconv.ParseInt(strs[1], 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
expired := time.Duration(timestamp-time.Now().Unix()) * time.Second
|
|
|
|
like.Expiration = &expired
|
|
|
|
}
|
2022-09-08 07:00:57 +00:00
|
|
|
}
|
2022-09-13 10:44:07 +00:00
|
|
|
*/
|
2022-09-08 07:00:57 +00:00
|
|
|
return like, nil
|
|
|
|
}
|
|
|
|
|
2023-08-03 06:05:33 +00:00
|
|
|
func (y *Cloud189PC) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) (model.Obj, error) {
|
2022-09-08 07:00:57 +00:00
|
|
|
fullUrl := API_URL
|
|
|
|
if y.isFamily() {
|
|
|
|
fullUrl += "/family/file"
|
|
|
|
}
|
|
|
|
fullUrl += "/createFolder.action"
|
|
|
|
|
2023-08-03 06:05:33 +00:00
|
|
|
var newFolder Cloud189Folder
|
2022-09-08 07:00:57 +00:00
|
|
|
_, err := y.post(fullUrl, func(req *resty.Request) {
|
|
|
|
req.SetContext(ctx)
|
|
|
|
req.SetQueryParams(map[string]string{
|
|
|
|
"folderName": dirName,
|
|
|
|
"relativePath": "",
|
|
|
|
})
|
|
|
|
if y.isFamily() {
|
|
|
|
req.SetQueryParams(map[string]string{
|
|
|
|
"familyId": y.FamilyID,
|
|
|
|
"parentId": parentDir.GetID(),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
req.SetQueryParams(map[string]string{
|
|
|
|
"parentFolderId": parentDir.GetID(),
|
|
|
|
})
|
|
|
|
}
|
2023-08-03 06:05:33 +00:00
|
|
|
}, &newFolder)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &newFolder, nil
|
2022-09-08 07:00:57 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 06:05:33 +00:00
|
|
|
func (y *Cloud189PC) Move(ctx context.Context, srcObj, dstDir model.Obj) (model.Obj, error) {
|
|
|
|
var resp CreateBatchTaskResp
|
2022-09-08 07:00:57 +00:00
|
|
|
_, err := y.post(API_URL+"/batch/createBatchTask.action", func(req *resty.Request) {
|
|
|
|
req.SetContext(ctx)
|
|
|
|
req.SetFormData(map[string]string{
|
|
|
|
"type": "MOVE",
|
|
|
|
"taskInfos": MustString(utils.Json.MarshalToString(
|
|
|
|
[]BatchTaskInfo{
|
|
|
|
{
|
|
|
|
FileId: srcObj.GetID(),
|
|
|
|
FileName: srcObj.GetName(),
|
|
|
|
IsFolder: BoolToNumber(srcObj.IsDir()),
|
|
|
|
},
|
|
|
|
})),
|
|
|
|
"targetFolderId": dstDir.GetID(),
|
|
|
|
})
|
|
|
|
if y.isFamily() {
|
|
|
|
req.SetFormData(map[string]string{
|
|
|
|
"familyId": y.FamilyID,
|
|
|
|
})
|
|
|
|
}
|
2023-08-03 06:05:33 +00:00
|
|
|
}, &resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err = y.WaitBatchTask("MOVE", resp.TaskID, time.Millisecond*400); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return srcObj, nil
|
2022-09-08 07:00:57 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 06:05:33 +00:00
|
|
|
func (y *Cloud189PC) Rename(ctx context.Context, srcObj model.Obj, newName string) (model.Obj, error) {
|
2022-09-08 07:00:57 +00:00
|
|
|
queryParam := make(map[string]string)
|
|
|
|
fullUrl := API_URL
|
|
|
|
method := http.MethodPost
|
|
|
|
if y.isFamily() {
|
|
|
|
fullUrl += "/family/file"
|
|
|
|
method = http.MethodGet
|
|
|
|
queryParam["familyId"] = y.FamilyID
|
|
|
|
}
|
2023-08-03 06:05:33 +00:00
|
|
|
|
|
|
|
var newObj model.Obj
|
|
|
|
switch f := srcObj.(type) {
|
|
|
|
case *Cloud189File:
|
2022-09-08 07:00:57 +00:00
|
|
|
fullUrl += "/renameFile.action"
|
|
|
|
queryParam["fileId"] = srcObj.GetID()
|
|
|
|
queryParam["destFileName"] = newName
|
2023-08-03 06:05:33 +00:00
|
|
|
newObj = &Cloud189File{Icon: f.Icon} // 复用预览
|
|
|
|
case *Cloud189Folder:
|
|
|
|
fullUrl += "/renameFolder.action"
|
|
|
|
queryParam["folderId"] = srcObj.GetID()
|
|
|
|
queryParam["destFolderName"] = newName
|
|
|
|
newObj = &Cloud189Folder{}
|
|
|
|
default:
|
|
|
|
return nil, errs.NotSupport
|
2022-09-08 07:00:57 +00:00
|
|
|
}
|
2023-08-03 06:05:33 +00:00
|
|
|
|
2022-09-08 07:00:57 +00:00
|
|
|
_, err := y.request(fullUrl, method, func(req *resty.Request) {
|
2023-08-03 06:05:33 +00:00
|
|
|
req.SetContext(ctx).SetQueryParams(queryParam)
|
|
|
|
}, nil, newObj)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return newObj, nil
|
2022-09-08 07:00:57 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 07:03:09 +00:00
|
|
|
func (y *Cloud189PC) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
|
2023-08-03 06:05:33 +00:00
|
|
|
var resp CreateBatchTaskResp
|
2022-09-08 07:00:57 +00:00
|
|
|
_, err := y.post(API_URL+"/batch/createBatchTask.action", func(req *resty.Request) {
|
|
|
|
req.SetContext(ctx)
|
|
|
|
req.SetFormData(map[string]string{
|
|
|
|
"type": "COPY",
|
|
|
|
"taskInfos": MustString(utils.Json.MarshalToString(
|
|
|
|
[]BatchTaskInfo{
|
|
|
|
{
|
|
|
|
FileId: srcObj.GetID(),
|
|
|
|
FileName: srcObj.GetName(),
|
|
|
|
IsFolder: BoolToNumber(srcObj.IsDir()),
|
|
|
|
},
|
|
|
|
})),
|
|
|
|
"targetFolderId": dstDir.GetID(),
|
|
|
|
"targetFileName": dstDir.GetName(),
|
|
|
|
})
|
|
|
|
if y.isFamily() {
|
|
|
|
req.SetFormData(map[string]string{
|
|
|
|
"familyId": y.FamilyID,
|
|
|
|
})
|
|
|
|
}
|
2023-08-03 06:05:33 +00:00
|
|
|
}, &resp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return y.WaitBatchTask("COPY", resp.TaskID, time.Second)
|
2022-09-08 07:00:57 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 07:03:09 +00:00
|
|
|
func (y *Cloud189PC) Remove(ctx context.Context, obj model.Obj) error {
|
2023-08-03 06:05:33 +00:00
|
|
|
var resp CreateBatchTaskResp
|
2022-09-08 07:00:57 +00:00
|
|
|
_, err := y.post(API_URL+"/batch/createBatchTask.action", func(req *resty.Request) {
|
|
|
|
req.SetContext(ctx)
|
|
|
|
req.SetFormData(map[string]string{
|
|
|
|
"type": "DELETE",
|
|
|
|
"taskInfos": MustString(utils.Json.MarshalToString(
|
|
|
|
[]*BatchTaskInfo{
|
|
|
|
{
|
|
|
|
FileId: obj.GetID(),
|
|
|
|
FileName: obj.GetName(),
|
|
|
|
IsFolder: BoolToNumber(obj.IsDir()),
|
|
|
|
},
|
|
|
|
})),
|
|
|
|
})
|
|
|
|
|
|
|
|
if y.isFamily() {
|
|
|
|
req.SetFormData(map[string]string{
|
|
|
|
"familyId": y.FamilyID,
|
|
|
|
})
|
|
|
|
}
|
2023-08-03 06:05:33 +00:00
|
|
|
}, &resp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// 批量任务数量限制,过快会导致无法删除
|
|
|
|
return y.WaitBatchTask("DELETE", resp.TaskID, time.Millisecond*200)
|
2022-09-08 07:00:57 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 06:05:33 +00:00
|
|
|
func (y *Cloud189PC) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) (model.Obj, error) {
|
2023-09-06 06:46:35 +00:00
|
|
|
// 响应时间长,按需启用
|
|
|
|
if y.Addition.RapidUpload {
|
|
|
|
if newObj, err := y.RapidUpload(ctx, dstDir, stream); err == nil {
|
|
|
|
return newObj, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-27 06:28:58 +00:00
|
|
|
switch y.UploadMethod {
|
|
|
|
case "old":
|
|
|
|
return y.OldUpload(ctx, dstDir, stream, up)
|
|
|
|
case "rapid":
|
2022-09-08 07:00:57 +00:00
|
|
|
return y.FastUpload(ctx, dstDir, stream, up)
|
2023-08-03 06:05:33 +00:00
|
|
|
case "stream":
|
|
|
|
if stream.GetSize() == 0 {
|
|
|
|
return y.FastUpload(ctx, dstDir, stream, up)
|
|
|
|
}
|
|
|
|
fallthrough
|
2023-05-27 06:28:58 +00:00
|
|
|
default:
|
2023-08-03 06:05:33 +00:00
|
|
|
return y.StreamUpload(ctx, dstDir, stream, up)
|
2022-09-08 07:00:57 +00:00
|
|
|
}
|
|
|
|
}
|