refactor file rm command

This commit is contained in:
tickstep 2024-03-07 09:27:29 +08:00
parent 5e90f96577
commit a9f9f18f09
3 changed files with 22 additions and 31 deletions

View File

@ -78,8 +78,7 @@ func RunRemove(driveId string, paths ...string) {
cacheCleanDirs := []string{}
failedRmPaths := make([]string, 0, len(paths))
delFileInfos := []*aliyunpan.FileBatchActionParam{}
fileId2FileEntity := map[string]*aliyunpan.FileEntity{}
successDelFileEntity := []*aliyunpan.FileEntity{}
for _, p := range paths {
absolutePath := path.Clean(activeUser.PathJoin(driveId, p))
@ -95,28 +94,27 @@ func RunRemove(driveId string, paths ...string) {
}
for _, f := range fileList {
// 删除匹配的文件
delFileInfos = append(delFileInfos, &aliyunpan.FileBatchActionParam{
fdr, err := activeUser.PanClient().OpenapiPanClient().FileDelete(&aliyunpan.FileBatchActionParam{
DriveId: driveId,
FileId: f.FileId,
})
fileId2FileEntity[f.FileId] = f
if err != nil || !fdr.Success {
failedRmPaths = append(failedRmPaths, absolutePath)
} else {
successDelFileEntity = append(successDelFileEntity, f)
}
cacheCleanDirs = append(cacheCleanDirs, path.Dir(f.Path))
}
}
// delete
successDelFileEntity := []*aliyunpan.FileEntity{}
fdr, err := activeUser.PanClient().OpenapiPanClient().FileDelete(delFileInfos)
if fdr != nil {
for _, item := range fdr {
if !item.Success {
failedRmPaths = append(failedRmPaths, fileId2FileEntity[item.FileId].Path)
} else {
successDelFileEntity = append(successDelFileEntity, fileId2FileEntity[item.FileId])
}
// output
if len(failedRmPaths) > 0 {
fmt.Println("以下文件删除失败:")
for _, fp := range failedRmPaths {
fmt.Println(fp)
}
fmt.Println("")
}
pnt := func() {
tb := cmdtable.NewTable(os.Stdout)
tb.SetHeader([]string{"#", "文件/目录"})
@ -130,9 +128,4 @@ func RunRemove(driveId string, paths ...string) {
pnt()
activeUser.DeleteCache(cacheCleanDirs)
}
if len(successDelFileEntity) == 0 && err != nil {
fmt.Println("无法删除文件,请稍后重试")
return
}
}

View File

@ -470,10 +470,10 @@ StepUploadPrepareUpload:
return
}
// existed, delete it
var fileDeleteResult []*aliyunpan.FileBatchActionResult
var fileDeleteResult *aliyunpan.FileBatchActionResult
var err *apierror.ApiError
fileDeleteResult, err = utu.PanClient.OpenapiPanClient().FileDelete([]*aliyunpan.FileBatchActionParam{{DriveId: efi.DriveId, FileId: efi.FileId}})
if err != nil || len(fileDeleteResult) == 0 {
fileDeleteResult, err = utu.PanClient.OpenapiPanClient().FileDelete(&aliyunpan.FileBatchActionParam{DriveId: efi.DriveId, FileId: efi.FileId})
if err != nil || !fileDeleteResult.Success {
result.Err = err
result.ResultMessage = "无法删除文件,请稍后重试"
return

View File

@ -462,11 +462,9 @@ func (f *FileActionTask) uploadFile(ctx context.Context) error {
return nil
} else {
// 删除云盘文件
dp := []*aliyunpan.FileBatchActionParam{
{
DriveId: f.syncItem.DriveId,
FileId: panFileId,
},
dp := &aliyunpan.FileBatchActionParam{
DriveId: f.syncItem.DriveId,
FileId: panFileId,
}
if _, e := f.panClient.OpenapiPanClient().FileDelete(dp); e != nil {
logger.Verbosef(" 删除云盘旧文件失败: %s\n", targetPanFilePath)
@ -703,11 +701,11 @@ func (f *FileActionTask) deletePanFile(ctx context.Context) error {
}
// 删除
var fileDeleteResult []*aliyunpan.FileBatchActionResult
var fileDeleteResult *aliyunpan.FileBatchActionResult
var err *apierror.ApiError = nil
fileDeleteResult, err = f.panClient.OpenapiPanClient().FileDelete([]*aliyunpan.FileBatchActionParam{{DriveId: driveId, FileId: panFileId}})
fileDeleteResult, err = f.panClient.OpenapiPanClient().FileDelete(&aliyunpan.FileBatchActionParam{DriveId: driveId, FileId: panFileId})
time.Sleep(1 * time.Second)
if err != nil || len(fileDeleteResult) == 0 {
if err != nil || !fileDeleteResult.Success {
f.syncItem.Status = SyncFileStatusFailed
} else {
f.syncItem.Status = SyncFileStatusSuccess