From a9f9f18f09cba227b642adbd0ec9b842c4331248 Mon Sep 17 00:00:00 2001 From: tickstep Date: Thu, 7 Mar 2024 09:27:29 +0800 Subject: [PATCH] refactor file rm command --- internal/command/rm.go | 33 ++++++++----------- .../functions/panupload/upload_task_unit.go | 6 ++-- internal/syncdrive/file_action_task.go | 14 ++++---- 3 files changed, 22 insertions(+), 31 deletions(-) diff --git a/internal/command/rm.go b/internal/command/rm.go index 8549997..ac4b5f4 100644 --- a/internal/command/rm.go +++ b/internal/command/rm.go @@ -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 - } } diff --git a/internal/functions/panupload/upload_task_unit.go b/internal/functions/panupload/upload_task_unit.go index 8db3a42..ab0d342 100644 --- a/internal/functions/panupload/upload_task_unit.go +++ b/internal/functions/panupload/upload_task_unit.go @@ -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 diff --git a/internal/syncdrive/file_action_task.go b/internal/syncdrive/file_action_task.go index 25b3c43..4f1b228 100644 --- a/internal/syncdrive/file_action_task.go +++ b/internal/syncdrive/file_action_task.go @@ -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