From dc469e87d2f727d12978d3a081d1037acf018811 Mon Sep 17 00:00:00 2001 From: xiaoyaofenfen <1254525673@qq.com> Date: Mon, 5 Dec 2022 20:43:35 +0800 Subject: [PATCH] share set command support wildcard pattern --- internal/command/share.go | 31 ++++++++++++++++++++++++++----- internal/command/utils.go | 37 ------------------------------------- 2 files changed, 26 insertions(+), 42 deletions(-) diff --git a/internal/command/share.go b/internal/command/share.go index 970e9d9..218bb92 100644 --- a/internal/command/share.go +++ b/internal/command/share.go @@ -24,6 +24,7 @@ import ( "github.com/tickstep/library-go/logger" "github.com/urfave/cli" "os" + "path" "path/filepath" "strconv" "time" @@ -52,6 +53,9 @@ func CmdShare() cli.Command { 创建文件 1.mp4 的分享链接 aliyunpan share set 1.mp4 + 创建 /我的视频/ 目录下所有mp4文件的分享链接,支持通配符 + aliyunpan share set /我的视频/*.mp4 + 创建文件 1.mp4 的分享链接,并指定分享密码为2333 aliyunpan share set -sharePwd 2333 1.mp4 @@ -235,15 +239,32 @@ func CmdShare() cli.Command { // RunShareSet 执行分享 func RunShareSet(driveId string, paths []string, expiredTime string, sharePwd string) { - panClient := GetActivePanClient() - fileList, _, err := GetFileInfoByPaths(paths[:len(paths)]...) - if err != nil { - fmt.Println(err) + if len(paths) <= 0 { + fmt.Println("请指定文件路径") return } + activeUser := GetActiveUser() + panClient := activeUser.PanClient() + + allFileList := []*aliyunpan.FileEntity{} + for idx := 0; idx < len(paths); idx++ { + absolutePath := path.Clean(activeUser.PathJoin(driveId, paths[idx])) + fileList, err1 := matchPathByShellPattern(driveId, absolutePath) + if err1 != nil { + fmt.Println("文件不存在: " + absolutePath) + continue + } + if fileList == nil || len(fileList) == 0 { + // 文件不存在 + fmt.Println("文件不存在: " + absolutePath) + continue + } + // 匹配的文件 + allFileList = append(allFileList, fileList...) + } fidList := []string{} - for _, f := range fileList { + for _, f := range allFileList { fidList = append(fidList, f.FileId) } diff --git a/internal/command/utils.go b/internal/command/utils.go index 185e382..405db94 100644 --- a/internal/command/utils.go +++ b/internal/command/utils.go @@ -14,7 +14,6 @@ package command import ( - "errors" "fmt" "github.com/tickstep/aliyunpan-api/aliyunpan" "github.com/tickstep/aliyunpan/internal/config" @@ -31,25 +30,6 @@ var ( panCommandVerbose = logger.New("PANCOMMAND", config.EnvVerbose) ) -// GetFileInfoByPaths 获取指定文件路径的文件详情信息 -func GetFileInfoByPaths(paths ...string) (fileInfoList []*aliyunpan.FileEntity, failedPaths []string, error error) { - if len(paths) <= 0 { - return nil, nil, fmt.Errorf("请指定文件路径") - } - activeUser := GetActiveUser() - - for idx := 0; idx < len(paths); idx++ { - absolutePath := path.Clean(activeUser.PathJoin(activeUser.ActiveDriveId, paths[idx])) - fe, err := activeUser.PanClient().FileInfoByPath(activeUser.ActiveDriveId, absolutePath) - if err != nil { - failedPaths = append(failedPaths, absolutePath) - continue - } - fileInfoList = append(fileInfoList, fe) - } - return -} - // RunTestShellPattern 执行测试通配符 func RunTestShellPattern(driveId string, pattern string) { acUser := GetActiveUser() @@ -64,23 +44,6 @@ func RunTestShellPattern(driveId string, pattern string) { return } -// matchPathByShellPatternOnce 通配符匹配唯一结果,如果有多条则报错 -func matchPathByShellPatternOnce(driveId string, pattern string) (*aliyunpan.FileEntity, error) { - acUser := GetActiveUser() - files, err := acUser.PanClient().MatchPathByShellPattern(driveId, GetActiveUser().PathJoin(driveId, pattern)) - if err != nil { - return nil, err - } - switch files.ItemCount() { - case 0: - return nil, errors.New("未匹配到路径, 请检测通配符") - case 1: - return files.Item(0), nil - default: - return nil, errors.New("多条通配符匹配结果") - } -} - // matchPathByShellPattern 通配符匹配路径,允许返回多个匹配结果 func matchPathByShellPattern(driveId string, patterns ...string) (files []*aliyunpan.FileEntity, e error) { acUser := GetActiveUser()