refactor mv command

This commit is contained in:
tickstep 2024-03-07 14:22:56 +08:00
parent 1f10f1e982
commit 31aaadfcc1

View File

@ -85,49 +85,44 @@ func RunMove(driveId string, paths ...string) {
cacheCleanPaths = append(cacheCleanPaths, targetFile.Path) cacheCleanPaths = append(cacheCleanPaths, targetFile.Path)
failedMoveFiles := []*aliyunpan.FileEntity{} failedMoveFiles := []*aliyunpan.FileEntity{}
moveFileParamList := []*aliyunpan.FileMoveParam{} successMoveFiles := []*aliyunpan.FileEntity{}
fileId2FileEntity := map[string]*aliyunpan.FileEntity{}
for _, mfi := range opFileList { for _, mfi := range opFileList {
fileId2FileEntity[mfi.FileId] = mfi fmr, er := activeUser.PanClient().OpenapiPanClient().FileMove(&aliyunpan.FileMoveParam{
moveFileParamList = append(moveFileParamList,
&aliyunpan.FileMoveParam{
DriveId: driveId, DriveId: driveId,
FileId: mfi.FileId, FileId: mfi.FileId,
ToDriveId: driveId, ToDriveId: driveId,
ToParentFileId: targetFile.FileId, ToParentFileId: targetFile.FileId,
}) })
if er != nil || !fmr.Success {
failedMoveFiles = append(failedMoveFiles, mfi)
} else {
successMoveFiles = append(successMoveFiles, mfi)
}
cacheCleanPaths = append(cacheCleanPaths, path.Dir(mfi.Path)) cacheCleanPaths = append(cacheCleanPaths, path.Dir(mfi.Path))
} }
fmr, er := activeUser.PanClient().OpenapiPanClient().FileMove(moveFileParamList)
for _, rs := range fmr {
if !rs.Success {
failedMoveFiles = append(failedMoveFiles, fileId2FileEntity[rs.FileId])
}
}
if len(failedMoveFiles) > 0 { if len(failedMoveFiles) > 0 {
fmt.Println("以下文件移动失败:") fmt.Println("以下文件移动失败:")
for _, f := range failedMoveFiles { for _, f := range failedMoveFiles {
fmt.Println(f.FileName) fmt.Println(f.Path)
} }
fmt.Println("") fmt.Println("")
} }
if er == nil { if len(successMoveFiles) > 0 {
pnt := func() { pnt := func() {
tb := cmdtable.NewTable(os.Stdout) tb := cmdtable.NewTable(os.Stdout)
tb.SetHeader([]string{"#", "文件/目录"}) tb.SetHeader([]string{"#", "文件/目录"})
for k, rs := range fmr { for k, rs := range successMoveFiles {
tb.Append([]string{strconv.Itoa(k + 1), fileId2FileEntity[rs.FileId].Path}) tb.Append([]string{strconv.Itoa(k + 1), rs.Path})
} }
tb.Render() tb.Render()
} }
fmt.Println("操作成功, 以下文件已移动到目标目录: ", targetFile.Path) fmt.Println("操作成功, 以下文件已移动到目标目录: ", targetFile.Path)
pnt() pnt()
activeUser.DeleteCache(cacheCleanPaths)
} else { } else {
fmt.Println("无法移动文件,请稍后重试") fmt.Println("无法移动文件,请稍后重试")
} }
activeUser.DeleteCache(cacheCleanPaths)
} }
func getFileInfo(driveId string, paths ...string) (opFileList []*aliyunpan.FileEntity, targetFile *aliyunpan.FileEntity, failedPaths []string, error error) { func getFileInfo(driveId string, paths ...string) (opFileList []*aliyunpan.FileEntity, targetFile *aliyunpan.FileEntity, failedPaths []string, error error) {