support wildcard for move command #89

This commit is contained in:
tickstep 2022-10-07 12:11:34 +08:00
parent 81d7bd22c9
commit 8f79a7e807
3 changed files with 88 additions and 25 deletions

View File

@ -17,9 +17,19 @@ import (
"fmt"
"github.com/tickstep/aliyunpan-api/aliyunpan"
"github.com/tickstep/aliyunpan/cmder"
"github.com/tickstep/aliyunpan/cmder/cmdtable"
"github.com/tickstep/aliyunpan/internal/config"
"github.com/tickstep/library-go/logger"
"github.com/urfave/cli"
"os"
"path"
"strconv"
)
type (
MvOption struct {
UseWildcard bool
}
)
func CmdMv() cli.Command {
@ -29,12 +39,15 @@ func CmdMv() cli.Command {
UsageText: `移动:
aliyunpan mv <文件/目录1> <文件/目录2> <文件/目录3> ... <目标目录>`,
Description: `
注意: 移动多个文件和目录时, 请确保每一个文件和目录都存在, 否则移动操作会失败.
注意: 移动多个文件和目录时, 请确保每一个文件和目录都存在, 否则移动操作会失败支持通配符匹配移动文件通配符当前只能匹配文件名不能匹配文件路径
示例:
/我的资源/1.mp4 移动到 根目录 /
aliyunpan mv /我的资源/1.mp4 /
/我的资源 目录下所有的.png文件 移动到 /我的图片 目录下面使用通配符匹配
aliyunpan mv -wc /我的资源/*.png /我的图片
`,
Category: "阿里云盘",
Before: cmder.ReloadConfigFunc,
@ -48,7 +61,10 @@ func CmdMv() cli.Command {
return nil
}
RunMove(parseDriveId(c), c.Args()...)
opt := MvOption{
UseWildcard: c.Bool("wc"),
}
RunMove(parseDriveId(c), opt, c.Args()...)
return nil
},
Flags: []cli.Flag{
@ -57,16 +73,20 @@ func CmdMv() cli.Command {
Usage: "网盘ID",
Value: "",
},
cli.BoolFlag{
Name: "wc",
Usage: "wildcard使用通配符匹配文件名",
},
},
}
}
// RunMove 执行移动文件/目录
func RunMove(driveId string, paths ...string) {
func RunMove(driveId string, option MvOption, paths ...string) {
activeUser := GetActiveUser()
cacheCleanPaths := []string{}
opFileList, targetFile, _, err := getFileInfo(driveId, paths...)
if err != nil {
opFileList, targetFile, _, err := getFileInfo(driveId, option, paths...)
if err != nil {
fmt.Println(err)
return
}
@ -83,20 +103,20 @@ func RunMove(driveId string, paths ...string) {
failedMoveFiles := []*aliyunpan.FileEntity{}
moveFileParamList := []*aliyunpan.FileMoveParam{}
fileId2FileEntity := map[string]*aliyunpan.FileEntity{}
for _,mfi := range opFileList {
for _, mfi := range opFileList {
fileId2FileEntity[mfi.FileId] = mfi
moveFileParamList = append(moveFileParamList,
&aliyunpan.FileMoveParam{
DriveId: driveId,
FileId: mfi.FileId,
ToDriveId: driveId,
DriveId: driveId,
FileId: mfi.FileId,
ToDriveId: driveId,
ToParentFileId: targetFile.FileId,
})
cacheCleanPaths = append(cacheCleanPaths, path.Dir(mfi.Path))
}
fmr,er := activeUser.PanClient().FileMove(moveFileParamList)
fmr, er := activeUser.PanClient().FileMove(moveFileParamList)
for _,rs := range fmr {
for _, rs := range fmr {
if !rs.Success {
failedMoveFiles = append(failedMoveFiles, fileId2FileEntity[rs.FileId])
}
@ -104,20 +124,29 @@ func RunMove(driveId string, paths ...string) {
if len(failedMoveFiles) > 0 {
fmt.Println("以下文件移动失败:")
for _,f := range failedMoveFiles {
for _, f := range failedMoveFiles {
fmt.Println(f.FileName)
}
fmt.Println("")
}
if er == nil {
fmt.Println("操作成功, 已移动文件到目标目录: ", targetFile.Path)
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()
activeUser.DeleteCache(cacheCleanPaths)
} else {
fmt.Println("无法移动文件,请稍后重试")
}
}
func getFileInfo(driveId string, paths ...string) (opFileList []*aliyunpan.FileEntity, targetFile *aliyunpan.FileEntity, failedPaths []string, error error) {
func getFileInfo(driveId string, option MvOption, paths ...string) (opFileList []*aliyunpan.FileEntity, targetFile *aliyunpan.FileEntity, failedPaths []string, error error) {
if len(paths) <= 1 {
return nil, nil, nil, fmt.Errorf("请指定目标文件夹路径")
}
@ -130,6 +159,40 @@ func getFileInfo(driveId string, paths ...string) (opFileList []*aliyunpan.FileE
return nil, nil, nil, fmt.Errorf("指定目标文件夹不存在")
}
opFileList, failedPaths, error = GetAppFileInfoByPaths(driveId, paths[:len(paths)-1]...)
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)
}
}
return
}

View File

@ -23,7 +23,6 @@ import (
"github.com/urfave/cli"
"os"
"path"
"path/filepath"
"strconv"
)
@ -81,7 +80,7 @@ func CmdRm() cli.Command {
},
cli.BoolFlag{
Name: "wc",
Usage: "wildcard使用通配符匹配",
Usage: "wildcard使用通配符匹配文件名",
},
},
}
@ -176,11 +175,3 @@ func RunRemove(driveId string, option RmOption, paths ...string) {
return
}
}
func isIncludeFile(pattern string, fileName string) bool {
b, er := filepath.Match(pattern, fileName)
if er != nil {
return false
}
return b
}

View File

@ -21,6 +21,7 @@ import (
"math/rand"
"net/url"
"path"
"path/filepath"
"strings"
"time"
)
@ -161,3 +162,11 @@ func ReloadRefreshTokenInNeed(activeUser *config.PanUser) bool {
}
return false
}
func isIncludeFile(pattern string, fileName string) bool {
b, er := filepath.Match(pattern, fileName)
if er != nil {
return false
}
return b
}