aliyunpan/internal/command/mv.go

199 lines
5.7 KiB
Go
Raw Normal View History

2021-10-10 10:48:53 +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"
"github.com/tickstep/aliyunpan-api/aliyunpan"
"github.com/tickstep/aliyunpan/cmder"
2022-10-07 12:11:34 +08:00
"github.com/tickstep/aliyunpan/cmder/cmdtable"
2021-10-10 10:48:53 +08:00
"github.com/tickstep/aliyunpan/internal/config"
2022-10-07 12:11:34 +08:00
"github.com/tickstep/library-go/logger"
2021-10-10 10:48:53 +08:00
"github.com/urfave/cli"
2022-10-07 12:11:34 +08:00
"os"
2021-10-10 10:48:53 +08:00
"path"
2022-10-07 12:11:34 +08:00
"strconv"
)
type (
MvOption struct {
UseWildcard bool
}
2021-10-10 10:48:53 +08:00
)
func CmdMv() cli.Command {
return cli.Command{
Name: "mv",
Usage: "移动文件/目录",
UsageText: `移动:
aliyunpan mv <文件/目录1> <文件/目录2> <文件/目录3> ... <目标目录>`,
Description: `
2022-10-07 12:11:34 +08:00
注意: 移动多个文件和目录时, 请确保每一个文件和目录都存在, 否则移动操作会失败支持通配符匹配移动文件通配符当前只能匹配文件名不能匹配文件路径
2021-10-10 10:48:53 +08:00
示例:
/我的资源/1.mp4 移动到 根目录 /
aliyunpan mv /我的资源/1.mp4 /
2022-10-07 12:11:34 +08:00
/我的资源 目录下所有的.png文件 移动到 /我的图片 目录下面使用通配符匹配
aliyunpan mv -wc /我的资源/*.png /我的图片
2021-10-10 10:48:53 +08:00
`,
Category: "阿里云盘",
Before: cmder.ReloadConfigFunc,
Action: func(c *cli.Context) error {
if c.NArg() <= 1 {
cli.ShowCommandHelp(c, c.Command.Name)
return nil
}
if config.Config.ActiveUser() == nil {
fmt.Println("未登录账号")
return nil
}
2022-10-07 12:11:34 +08:00
opt := MvOption{
UseWildcard: c.Bool("wc"),
}
RunMove(parseDriveId(c), opt, c.Args()...)
2021-10-10 10:48:53 +08:00
return nil
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "driveId",
Usage: "网盘ID",
Value: "",
},
2022-10-07 12:11:34 +08:00
cli.BoolFlag{
Name: "wc",
Usage: "wildcard使用通配符匹配文件名",
},
2021-10-10 10:48:53 +08:00
},
}
}
// RunMove 执行移动文件/目录
2022-10-07 12:11:34 +08:00
func RunMove(driveId string, option MvOption, paths ...string) {
2021-10-10 10:48:53 +08:00
activeUser := GetActiveUser()
2021-10-31 15:53:27 +08:00
cacheCleanPaths := []string{}
2022-10-07 12:11:34 +08:00
opFileList, targetFile, _, err := getFileInfo(driveId, option, paths...)
if err != nil {
2021-10-10 10:48:53 +08:00
fmt.Println(err)
return
}
if targetFile == nil {
fmt.Println("目标文件不存在")
return
}
if opFileList == nil || len(opFileList) == 0 {
fmt.Println("没有有效的文件可移动")
return
}
2021-10-31 15:53:27 +08:00
cacheCleanPaths = append(cacheCleanPaths, targetFile.Path)
2021-10-10 10:48:53 +08:00
failedMoveFiles := []*aliyunpan.FileEntity{}
moveFileParamList := []*aliyunpan.FileMoveParam{}
fileId2FileEntity := map[string]*aliyunpan.FileEntity{}
2022-10-07 12:11:34 +08:00
for _, mfi := range opFileList {
2021-10-10 10:48:53 +08:00
fileId2FileEntity[mfi.FileId] = mfi
moveFileParamList = append(moveFileParamList,
&aliyunpan.FileMoveParam{
2022-10-07 12:11:34 +08:00
DriveId: driveId,
FileId: mfi.FileId,
ToDriveId: driveId,
2021-10-10 10:48:53 +08:00
ToParentFileId: targetFile.FileId,
})
2021-10-31 15:53:27 +08:00
cacheCleanPaths = append(cacheCleanPaths, path.Dir(mfi.Path))
2021-10-10 10:48:53 +08:00
}
2022-10-07 12:11:34 +08:00
fmr, er := activeUser.PanClient().FileMove(moveFileParamList)
2021-10-10 10:48:53 +08:00
2022-10-07 12:11:34 +08:00
for _, rs := range fmr {
2021-10-10 10:48:53 +08:00
if !rs.Success {
failedMoveFiles = append(failedMoveFiles, fileId2FileEntity[rs.FileId])
}
}
if len(failedMoveFiles) > 0 {
fmt.Println("以下文件移动失败:")
2022-10-07 12:11:34 +08:00
for _, f := range failedMoveFiles {
2021-10-10 10:48:53 +08:00
fmt.Println(f.FileName)
}
fmt.Println("")
}
if er == nil {
2022-10-07 12:11:34 +08:00
pnt := func() {
tb := cmdtable.NewTable(os.Stdout)
tb.SetHeader([]string{"#", "文件/目录"})
for k, rs := range fmr {
tb.Append([]string{strconv.Itoa(k + 1), fileId2FileEntity[rs.FileId].Path})
}
tb.Render()
}
fmt.Println("操作成功, 以下文件已移动到目标目录: ", targetFile.Path)
pnt()
2021-10-31 15:53:27 +08:00
activeUser.DeleteCache(cacheCleanPaths)
2021-10-10 10:48:53 +08:00
} else {
fmt.Println("无法移动文件,请稍后重试")
}
}
2022-10-07 12:11:34 +08:00
func getFileInfo(driveId string, option MvOption, paths ...string) (opFileList []*aliyunpan.FileEntity, targetFile *aliyunpan.FileEntity, failedPaths []string, error error) {
2021-10-10 10:48:53 +08:00
if len(paths) <= 1 {
return nil, nil, nil, fmt.Errorf("请指定目标文件夹路径")
}
activeUser := GetActiveUser()
// the last one is the target file path
targetFilePath := path.Clean(paths[len(paths)-1])
absolutePath := activeUser.PathJoin(driveId, targetFilePath)
targetFile, err := activeUser.PanClient().FileInfoByPath(driveId, absolutePath)
if err != nil || !targetFile.IsFolder() {
return nil, nil, nil, fmt.Errorf("指定目标文件夹不存在")
}
2022-10-07 12:11:34 +08:00
for idx := 0; idx < (len(paths) - 1); idx++ {
absolutePath = path.Clean(activeUser.PathJoin(driveId, paths[idx]))
if option.UseWildcard {
// 通配符
parentDir := path.Dir(absolutePath)
wildcardName := path.Base(absolutePath)
pf, err1 := activeUser.PanClient().FileInfoByPath(driveId, parentDir)
if err1 != nil {
failedPaths = append(failedPaths, absolutePath)
continue
}
fileList, er := activeUser.PanClient().FileListGetAll(&aliyunpan.FileListParam{
DriveId: driveId,
ParentFileId: pf.FileId,
}, 500)
if er != nil {
failedPaths = append(failedPaths, absolutePath)
continue
}
for _, f := range fileList {
if isIncludeFile(wildcardName, f.FileName) {
f.Path = parentDir + "/" + f.FileName
logger.Verboseln("wildcard match move: " + f.Path)
opFileList = append(opFileList, f)
}
}
} else {
fe, err1 := activeUser.PanClient().FileInfoByPath(driveId, absolutePath)
if err1 != nil {
failedPaths = append(failedPaths, absolutePath)
continue
}
opFileList = append(opFileList, fe)
}
}
2021-10-10 10:48:53 +08:00
return
}