aliyunpan/internal/command/save.go

180 lines
5.0 KiB
Go
Raw Normal View History

2023-12-10 21:44:48 +08:00
// Copyright (c) 2020 tickstep.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package command
import (
"fmt"
2024-03-03 14:02:05 +08:00
"github.com/tickstep/aliyunpan-api/aliyunpan_web"
2023-12-10 21:44:48 +08:00
"path"
"strings"
"github.com/tickstep/aliyunpan/internal/config"
"github.com/urfave/cli"
)
func CmdSave() cli.Command {
return cli.Command{
Name: "save",
Usage: "保存分享文件/目录",
UsageText: `
aliyunpan save <分享链接> (<提取码>) <目标目录>`,
Description: `
注意: 保存大量文件时, 命令完成后可能还需要额外等待一段时间; 分享的根目录下如果包含大量文件或文件夹, 可能存在不稳定的情况
示例:
公开分享 保存到 根目录 /
aliyunpan save ABCD1234wxyz /
aliyunpan save https://www.alipan.com/s/ABCD1234wxyz /
私密分享 保存到 指定目录 /资源分享
aliyunpan save ABCD1234wxyz akd1 /资源分享
aliyunpan save https://www.alipan.com/s/ABCD1234wxyz akd1 /资源分享
`,
Category: "阿里云盘",
Before: ReloadConfigFunc,
Action: func(c *cli.Context) error {
if c.NArg() <= 1 || c.NArg() > 3 {
cli.ShowCommandHelp(c, c.Command.Name)
return nil
}
if config.Config.ActiveUser() == nil {
fmt.Println("未登录账号")
return nil
}
2024-03-02 19:25:55 +08:00
if config.Config.ActiveUser().PanClient().WebapiPanClient() == nil {
2024-03-02 21:22:02 +08:00
fmt.Println("WEB客户端未登录请登录后再使用该命令")
2024-03-02 19:25:55 +08:00
return nil
}
2023-12-10 21:44:48 +08:00
RunSave(parseDriveId(c), c.Args()...)
return nil
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "driveId",
Usage: "网盘ID",
Value: "",
},
},
}
}
2024-03-02 19:25:55 +08:00
// RunSave 保存分享的文件
2023-12-10 21:44:48 +08:00
func RunSave(driveId string, args ...string) {
activeUser := GetActiveUser()
targetFilePath := path.Clean(args[len(args)-1])
absolutePath := activeUser.PathJoin(driveId, targetFilePath)
2024-03-02 00:55:46 +08:00
targetFile, err := activeUser.PanClient().WebapiPanClient().FileInfoByPath(driveId, absolutePath)
2023-12-10 21:44:48 +08:00
if err != nil || !targetFile.IsFolder() {
fmt.Println("指定目标文件夹不存在")
return
}
fmt.Println("保存文件至:", targetFilePath)
shareID := args[0]
if i := strings.Index(shareID, "alipan.com/s/"); i > 0 {
shareID = shareID[i+13:]
}
sharePwd := ""
if len(args) == 3 {
sharePwd = args[1]
}
2024-03-02 00:55:46 +08:00
token, err := activeUser.PanClient().WebapiPanClient().GetShareToken(shareID, sharePwd)
2023-12-10 21:44:48 +08:00
if err != nil {
fmt.Println("读取分享链接失败:", err)
return
}
2024-03-02 00:55:46 +08:00
list, err := activeUser.PanClient().WebapiPanClient().GetListByShare(token.ShareToken, shareID, "")
2023-12-10 21:44:48 +08:00
if err != nil {
fmt.Println("读取分享文件列表失败:", err)
return
}
for list.NextMarker != "" {
2024-03-02 00:55:46 +08:00
list2, err := activeUser.PanClient().WebapiPanClient().GetListByShare(token.ShareToken, shareID, "")
2023-12-10 21:44:48 +08:00
if err != nil {
fmt.Println("读取分享文件列表失败:", err)
return
}
list.Items = append(list.Items, list2.Items...)
list.NextMarker = list2.NextMarker
}
2024-03-03 14:02:05 +08:00
var params []*aliyunpan_web.FileSaveParam
files := make(map[string]*aliyunpan_web.ListByShareItem)
2023-12-10 21:44:48 +08:00
for _, item := range list.Items {
if item.FileExtension != "" {
fmt.Println(" ", item.Name)
} else {
fmt.Println(" ", item.Name+"/")
}
files[item.FileID] = item
2024-03-03 14:02:05 +08:00
params = append(params, &aliyunpan_web.FileSaveParam{
2023-12-10 21:44:48 +08:00
ShareID: shareID,
FileId: item.FileID,
AutoRename: true,
ToDriveId: driveId,
ToParentFileId: targetFile.FileId,
})
}
fmt.Println()
2024-03-02 00:55:46 +08:00
result, err := activeUser.PanClient().WebapiPanClient().FileCopy(token.ShareToken, params)
2023-12-10 21:44:48 +08:00
if err != nil {
fmt.Println("保存分享文件失败:", err)
return
}
var ids []string
var failedSaveFileIds []string
tasks := make(map[string]string)
for _, item := range result {
if item.AsyncTaskId == "" {
if item.Status != 201 {
failedSaveFileIds = append(failedSaveFileIds, item.FileId)
}
} else {
tasks[item.AsyncTaskId] = item.FileId
ids = append(ids, item.AsyncTaskId)
}
}
if ids != nil {
2024-03-02 00:55:46 +08:00
result2, err := activeUser.PanClient().WebapiPanClient().AsyncTaskGet(token.ShareToken, ids)
2023-12-10 21:44:48 +08:00
if err != nil {
fmt.Println("读取保存结果失败:", err)
}
for _, item := range result2 {
if !item.Success {
failedSaveFileIds = append(failedSaveFileIds, tasks[item.AsyncTaskId])
}
}
}
if failedSaveFileIds != nil {
fmt.Println("以下文件保存失败:")
for _, id := range failedSaveFileIds {
if v, ok := files[id]; ok {
fmt.Println(v.Name)
} else {
fmt.Println(id)
}
}
fmt.Println("")
}
fmt.Println("操作成功, 分享文件已保存到目标目录: ", targetFile.Path)
}