aliyunpan/internal/command/rm.go

139 lines
4.0 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"
"github.com/tickstep/aliyunpan/cmder/cmdtable"
"github.com/tickstep/aliyunpan/internal/config"
"github.com/urfave/cli"
"os"
"path"
"strconv"
)
func CmdRm() cli.Command {
return cli.Command{
Name: "rm",
Usage: "删除文件/目录",
UsageText: cmder.App().Name + " rm <文件/目录的路径1> <文件/目录2> <文件/目录3> ...",
Description: `
注意: 删除多个文件和目录时, 请确保每一个文件和目录都存在, 否则删除操作会失败.
2022-10-07 11:41:27 +08:00
被删除的文件或目录可在网盘文件回收站找回支持通配符匹配删除文件通配符当前只能匹配文件名不能匹配文件路径
2021-10-10 10:48:53 +08:00
示例:
删除 /我的资源/1.mp4
aliyunpan rm /我的资源/1.mp4
删除 /我的资源/1.mp4 /我的资源/2.mp4
aliyunpan rm /我的资源/1.mp4 /我的资源/2.mp4
删除 /我的资源 整个目录 !!
aliyunpan rm /我的资源
2022-10-07 11:41:27 +08:00
删除 /我的资源 目录下面的所有.zip文件使用通配符匹配
aliyunpan rm /我的资源/*.zip
2021-10-10 10:48:53 +08:00
`,
Category: "阿里云盘",
Before: cmder.ReloadConfigFunc,
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
cli.ShowCommandHelp(c, c.Command.Name)
return nil
}
if config.Config.ActiveUser() == nil {
fmt.Println("未登录账号")
return nil
}
RunRemove(parseDriveId(c), c.Args()...)
2021-10-10 10:48:53 +08:00
return nil
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "driveId",
Usage: "网盘ID",
Value: "",
},
},
}
}
// RunRemove 执行 批量删除文件/目录
func RunRemove(driveId string, paths ...string) {
2021-10-10 10:48:53 +08:00
activeUser := GetActiveUser()
2021-10-31 15:53:27 +08:00
cacheCleanDirs := []string{}
2021-10-10 10:48:53 +08:00
failedRmPaths := make([]string, 0, len(paths))
delFileInfos := []*aliyunpan.FileBatchActionParam{}
fileId2FileEntity := map[string]*aliyunpan.FileEntity{}
for _, p := range paths {
absolutePath := path.Clean(activeUser.PathJoin(driveId, p))
2022-12-05 20:10:02 +08:00
fileList, err1 := matchPathByShellPattern(driveId, absolutePath)
if err1 != nil {
failedRmPaths = append(failedRmPaths, absolutePath)
continue
}
if fileList == nil || len(fileList) == 0 {
// 失败,文件不存在
failedRmPaths = append(failedRmPaths, absolutePath)
continue
}
for _, f := range fileList {
// 删除匹配的文件
2022-10-07 11:41:27 +08:00
delFileInfos = append(delFileInfos, &aliyunpan.FileBatchActionParam{
DriveId: driveId,
2022-12-05 20:10:02 +08:00
FileId: f.FileId,
2022-10-07 11:41:27 +08:00
})
2022-12-05 20:10:02 +08:00
fileId2FileEntity[f.FileId] = f
cacheCleanDirs = append(cacheCleanDirs, path.Dir(f.Path))
2021-10-10 10:48:53 +08:00
}
}
// delete
successDelFileEntity := []*aliyunpan.FileEntity{}
fdr, err := activeUser.PanClient().FileDelete(delFileInfos)
if fdr != nil {
for _, item := range fdr {
2021-10-10 10:48:53 +08:00
if !item.Success {
failedRmPaths = append(failedRmPaths, fileId2FileEntity[item.FileId].Path)
} else {
successDelFileEntity = append(successDelFileEntity, fileId2FileEntity[item.FileId])
}
}
}
pnt := func() {
tb := cmdtable.NewTable(os.Stdout)
tb.SetHeader([]string{"#", "文件/目录"})
for k := range successDelFileEntity {
tb.Append([]string{strconv.Itoa(k + 1), successDelFileEntity[k].Path})
2021-10-10 10:48:53 +08:00
}
tb.Render()
}
if len(successDelFileEntity) > 0 {
fmt.Println("操作成功, 以下文件/目录已删除, 可在云盘文件回收站找回: ")
pnt()
2021-10-31 15:53:27 +08:00
activeUser.DeleteCache(cacheCleanDirs)
2021-10-10 10:48:53 +08:00
}
if len(successDelFileEntity) == 0 && err != nil {
fmt.Println("无法删除文件,请稍后重试")
return
}
}