Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(recursive-move): add overwrite option to preventing unintentional overwriting #7868

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 82 additions & 54 deletions server/handles/fsbatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handles
import (
"fmt"
"regexp"
"slices"

"github.com/alist-org/alist/v3/internal/errs"
"github.com/alist-org/alist/v3/internal/fs"
Expand All @@ -14,56 +15,10 @@ import (
"github.com/pkg/errors"
)

type BatchRenameReq struct {
SrcDir string `json:"src_dir"`
RenameObjects []struct {
SrcName string `json:"src_name"`
NewName string `json:"new_name"`
} `json:"rename_objects"`
}

func FsBatchRename(c *gin.Context) {
var req BatchRenameReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
user := c.MustGet("user").(*model.User)
if !user.CanRename() {
common.ErrorResp(c, errs.PermissionDenied, 403)
return
}

reqPath, err := user.JoinPath(req.SrcDir)
if err != nil {
common.ErrorResp(c, err, 403)
return
}

meta, err := op.GetNearestMeta(reqPath)
if err != nil {
if !errors.Is(errors.Cause(err), errs.MetaNotFound) {
common.ErrorResp(c, err, 500, true)
return
}
}
c.Set("meta", meta)
for _, renameObject := range req.RenameObjects {
if renameObject.SrcName == "" || renameObject.NewName == "" {
continue
}
filePath := fmt.Sprintf("%s/%s", reqPath, renameObject.SrcName)
if err := fs.Rename(c, filePath, renameObject.NewName); err != nil {
common.ErrorResp(c, err, 500)
return
}
}
common.SuccessResp(c)
}

type RecursiveMoveReq struct {
SrcDir string `json:"src_dir"`
DstDir string `json:"dst_dir"`
SrcDir string `json:"src_dir"`
DstDir string `json:"dst_dir"`
Overwrite bool `json:"overwrite"`
}

func FsRecursiveMove(c *gin.Context) {
Expand Down Expand Up @@ -104,9 +59,23 @@ func FsRecursiveMove(c *gin.Context) {
return
}

var existingFileNames []string
if !req.Overwrite {
dstFiles, err := fs.List(c, dstDir, &fs.ListArgs{})
if err != nil {
common.ErrorResp(c, err, 500)
return
}
existingFileNames = make([]string, 0, len(dstFiles))
for _, dstFile := range dstFiles {
existingFileNames = append(existingFileNames, dstFile.GetName())
}
}

// record the file path
filePathMap := make(map[model.Obj]string)
movingFiles := generic.NewQueue[model.Obj]()
movingFileNames := make([]string, 0, len(rootFiles))
for _, file := range rootFiles {
movingFiles.Push(file)
filePathMap[file] = srcDir
Expand Down Expand Up @@ -136,16 +105,75 @@ func FsRecursiveMove(c *gin.Context) {
continue
}

// move
err := fs.Move(c, movingFileName, dstDir, movingFiles.IsEmpty())
if err != nil {
common.ErrorResp(c, err, 500)
return
if !req.Overwrite {
if slices.Contains(existingFileNames, movingFile.GetName()) {
common.ErrorStrResp(c, fmt.Sprintf("file [%s] exists", movingFile.GetName()), 403)
return
}
existingFileNames = append(existingFileNames, movingFile.GetName())
}

movingFileNames = append(movingFileNames, movingFileName)
}

}

for i, fileName := range movingFileNames {
// move
err := fs.Move(c, fileName, dstDir, len(movingFileNames) > i+1)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
}

common.SuccessResp(c)
}

type BatchRenameReq struct {
SrcDir string `json:"src_dir"`
RenameObjects []struct {
SrcName string `json:"src_name"`
NewName string `json:"new_name"`
} `json:"rename_objects"`
}

func FsBatchRename(c *gin.Context) {
var req BatchRenameReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
user := c.MustGet("user").(*model.User)
if !user.CanRename() {
common.ErrorResp(c, errs.PermissionDenied, 403)
return
}

reqPath, err := user.JoinPath(req.SrcDir)
if err != nil {
common.ErrorResp(c, err, 403)
return
}

meta, err := op.GetNearestMeta(reqPath)
if err != nil {
if !errors.Is(errors.Cause(err), errs.MetaNotFound) {
common.ErrorResp(c, err, 500, true)
return
}
}
c.Set("meta", meta)
for _, renameObject := range req.RenameObjects {
if renameObject.SrcName == "" || renameObject.NewName == "" {
continue
}
filePath := fmt.Sprintf("%s/%s", reqPath, renameObject.SrcName)
if err := fs.Rename(c, filePath, renameObject.NewName); err != nil {
common.ErrorResp(c, err, 500)
return
}
}
common.SuccessResp(c)
}

Expand Down
Loading