Skip to content

Commit

Permalink
feat(recursive-move): Advanced conflict policy for preventing uninten…
Browse files Browse the repository at this point in the history
…tional overwriting
  • Loading branch information
Lanfei committed Jan 30, 2025
1 parent b9f397d commit f606e8b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
26 changes: 17 additions & 9 deletions server/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,29 @@ func ErrorStrResp(c *gin.Context, str string, code int, l ...bool) {
}

func SuccessResp(c *gin.Context, data ...interface{}) {
if len(data) == 0 {
c.JSON(200, Resp[interface{}]{
Code: 200,
Message: "success",
Data: nil,
})
return
SuccessWithMsgResp(c, "success", data...)
}

func SuccessWithMsgResp(c *gin.Context, msg string, data ...interface{}) {
var respData interface{}
if len(data) > 0 {
respData = data[0]
}

c.JSON(200, Resp[interface{}]{
Code: 200,
Message: "success",
Data: data[0],
Message: msg,
Data: respData,
})
}

func Pluralize(count int, singular, plural string) string {
if count == 1 {
return singular
}
return plural
}

func GetHttpReq(ctx context.Context) *http.Request {
if c, ok := ctx.(*gin.Context); ok {
return c.Request
Expand Down
7 changes: 7 additions & 0 deletions server/handles/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package handles

const (
CANCEL = "cancel"
OVERWRITE = "overwrite"
SKIP = "skip"
)
22 changes: 13 additions & 9 deletions server/handles/fsbatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (
)

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

func FsRecursiveMove(c *gin.Context) {
Expand Down Expand Up @@ -60,7 +60,7 @@ func FsRecursiveMove(c *gin.Context) {
}

var existingFileNames []string
if !req.Overwrite {
if req.ConflictPolicy != OVERWRITE {
dstFiles, err := fs.List(c, dstDir, &fs.ListArgs{})
if err != nil {
common.ErrorResp(c, err, 500)
Expand Down Expand Up @@ -99,35 +99,39 @@ func FsRecursiveMove(c *gin.Context) {
filePathMap[subFile] = subFilePath
}
} else {

if movingFilePath == dstDir {
// same directory, don't move
continue
}

if !req.Overwrite {
if slices.Contains(existingFileNames, movingFile.GetName()) {
if slices.Contains(existingFileNames, movingFile.GetName()) {
if req.ConflictPolicy == CANCEL {
common.ErrorStrResp(c, fmt.Sprintf("file [%s] exists", movingFile.GetName()), 403)
return
} else if req.ConflictPolicy == SKIP {
continue
}
} else if req.ConflictPolicy != OVERWRITE {
existingFileNames = append(existingFileNames, movingFile.GetName())
}

movingFileNames = append(movingFileNames, movingFileName)

}

}

var count = 0
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
}
count++
}

common.SuccessResp(c)
common.SuccessWithMsgResp(c, fmt.Sprintf("Successfully moved %d %s", count, common.Pluralize(count, "file", "files")))
}

type BatchRenameReq struct {
Expand Down

0 comments on commit f606e8b

Please # to comment.