share set command support wildcard pattern

This commit is contained in:
xiaoyaofenfen 2022-12-05 20:43:35 +08:00
parent 67c288318e
commit dc469e87d2
2 changed files with 26 additions and 42 deletions

View File

@ -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)
}

View File

@ -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()