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

View File

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

View File

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