mirror of
https://github.com/tickstep/aliyunpan.git
synced 2025-01-23 14:32:14 +08:00
160 lines
4.7 KiB
Go
160 lines
4.7 KiB
Go
// 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/cmdtable"
|
|
"github.com/tickstep/aliyunpan/internal/config"
|
|
"github.com/urfave/cli"
|
|
"os"
|
|
"path"
|
|
"strconv"
|
|
)
|
|
|
|
func CmdMv() cli.Command {
|
|
return cli.Command{
|
|
Name: "mv",
|
|
Usage: "移动文件/目录",
|
|
UsageText: `
|
|
aliyunpan mv <文件/目录1> <文件/目录2> <文件/目录3> ... <目标目录>`,
|
|
Description: `
|
|
注意: 移动多个文件和目录时, 请确保每一个文件和目录都存在, 否则移动操作会失败。支持通配符匹配移动文件,通配符当前只能匹配文件名,不能匹配文件路径。
|
|
|
|
示例:
|
|
|
|
将 /我的资源/1.mp4 移动到 根目录 /
|
|
aliyunpan mv /我的资源/1.mp4 /
|
|
|
|
将 /我的资源 目录下所有的.png文件 移动到 /我的图片 目录下面,使用通配符匹配
|
|
aliyunpan mv /我的资源/*.png /我的图片
|
|
`,
|
|
Category: "阿里云盘",
|
|
Before: 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
|
|
}
|
|
RunMove(parseDriveId(c), c.Args()...)
|
|
return nil
|
|
},
|
|
Flags: []cli.Flag{
|
|
cli.StringFlag{
|
|
Name: "driveId",
|
|
Usage: "网盘ID",
|
|
Value: "",
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// RunMove 执行移动文件/目录
|
|
func RunMove(driveId string, paths ...string) {
|
|
activeUser := GetActiveUser()
|
|
cacheCleanPaths := []string{}
|
|
opFileList, targetFile, _, err := getFileInfo(driveId, paths...)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
if targetFile == nil {
|
|
fmt.Println("目标文件不存在")
|
|
return
|
|
}
|
|
if opFileList == nil || len(opFileList) == 0 {
|
|
fmt.Println("没有有效的文件可移动")
|
|
return
|
|
}
|
|
cacheCleanPaths = append(cacheCleanPaths, targetFile.Path)
|
|
|
|
failedMoveFiles := []*aliyunpan.FileEntity{}
|
|
successMoveFiles := []*aliyunpan.FileEntity{}
|
|
for _, mfi := range opFileList {
|
|
fmr, er := activeUser.PanClient().OpenapiPanClient().FileMove(&aliyunpan.FileMoveParam{
|
|
DriveId: driveId,
|
|
FileId: mfi.FileId,
|
|
ToDriveId: driveId,
|
|
ToParentFileId: targetFile.FileId,
|
|
})
|
|
if er != nil || !fmr.Success {
|
|
failedMoveFiles = append(failedMoveFiles, mfi)
|
|
} else {
|
|
successMoveFiles = append(successMoveFiles, mfi)
|
|
}
|
|
cacheCleanPaths = append(cacheCleanPaths, path.Dir(mfi.Path))
|
|
}
|
|
|
|
if len(failedMoveFiles) > 0 {
|
|
fmt.Println("以下文件移动失败:")
|
|
for _, f := range failedMoveFiles {
|
|
fmt.Println(f.Path)
|
|
}
|
|
fmt.Println("")
|
|
}
|
|
if len(successMoveFiles) > 0 {
|
|
pnt := func() {
|
|
tb := cmdtable.NewTable(os.Stdout)
|
|
tb.SetHeader([]string{"#", "文件/目录"})
|
|
for k, rs := range successMoveFiles {
|
|
tb.Append([]string{strconv.Itoa(k + 1), rs.Path})
|
|
}
|
|
tb.Render()
|
|
}
|
|
fmt.Println("操作成功, 以下文件已移动到目标目录: ", targetFile.Path)
|
|
pnt()
|
|
} else {
|
|
fmt.Println("无法移动文件,请稍后重试")
|
|
}
|
|
activeUser.DeleteCache(cacheCleanPaths)
|
|
}
|
|
|
|
func getFileInfo(driveId string, paths ...string) (opFileList []*aliyunpan.FileEntity, targetFile *aliyunpan.FileEntity, failedPaths []string, error error) {
|
|
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().OpenapiPanClient().FileInfoByPath(driveId, absolutePath)
|
|
if err != nil || !targetFile.IsFolder() {
|
|
return nil, nil, nil, fmt.Errorf("指定目标文件夹不存在")
|
|
}
|
|
|
|
for idx := 0; idx < (len(paths) - 1); idx++ {
|
|
absolutePath = path.Clean(activeUser.PathJoin(driveId, paths[idx]))
|
|
fileList, err1 := matchPathByShellPattern(driveId, absolutePath)
|
|
if err1 != nil {
|
|
failedPaths = append(failedPaths, absolutePath)
|
|
continue
|
|
}
|
|
if fileList == nil || len(fileList) == 0 {
|
|
// 文件不存在
|
|
failedPaths = append(failedPaths, absolutePath)
|
|
continue
|
|
}
|
|
for _, f := range fileList {
|
|
// 移动匹配的文件
|
|
opFileList = append(opFileList, f)
|
|
}
|
|
}
|
|
return
|
|
}
|