Skip to content

Commit

Permalink
feat(fs): add overwrite option to preventing unintentional overwrit…
Browse files Browse the repository at this point in the history
…ing (#7809)
  • Loading branch information
Lanfei authored Jan 18, 2025
1 parent bb40e2e commit 59e0228
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
37 changes: 32 additions & 5 deletions server/handles/fsmanage.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ func FsMkdir(c *gin.Context) {
}

type MoveCopyReq struct {
SrcDir string `json:"src_dir"`
DstDir string `json:"dst_dir"`
Names []string `json:"names"`
SrcDir string `json:"src_dir"`
DstDir string `json:"dst_dir"`
Names []string `json:"names"`
Overwrite bool `json:"overwrite"`
}

func FsMove(c *gin.Context) {
Expand Down Expand Up @@ -86,6 +87,14 @@ func FsMove(c *gin.Context) {
common.ErrorResp(c, err, 403)
return
}
if !req.Overwrite {
for _, name := range req.Names {
if res, _ := fs.Get(c, stdpath.Join(dstDir, name), &fs.GetArgs{NoLog: true}); res != nil {
common.ErrorStrResp(c, "file exists", 403)
return
}
}
}
for i, name := range req.Names {
err := fs.Move(c, stdpath.Join(srcDir, name), dstDir, len(req.Names) > i+1)
if err != nil {
Expand Down Expand Up @@ -121,6 +130,14 @@ func FsCopy(c *gin.Context) {
common.ErrorResp(c, err, 403)
return
}
if !req.Overwrite {
for _, name := range req.Names {
if res, _ := fs.Get(c, stdpath.Join(dstDir, name), &fs.GetArgs{NoLog: true}); res != nil {
common.ErrorStrResp(c, "file exists", 403)
return
}
}
}
var addedTasks []task.TaskExtensionInfo
for i, name := range req.Names {
t, err := fs.Copy(c, stdpath.Join(srcDir, name), dstDir, len(req.Names) > i+1)
Expand All @@ -138,8 +155,9 @@ func FsCopy(c *gin.Context) {
}

type RenameReq struct {
Path string `json:"path"`
Name string `json:"name"`
Path string `json:"path"`
Name string `json:"name"`
Overwrite bool `json:"overwrite"`
}

func FsRename(c *gin.Context) {
Expand All @@ -158,6 +176,15 @@ func FsRename(c *gin.Context) {
common.ErrorResp(c, err, 403)
return
}
if !req.Overwrite {
dstPath := stdpath.Join(stdpath.Dir(reqPath), req.Name)
if dstPath != reqPath {
if res, _ := fs.Get(c, dstPath, &fs.GetArgs{NoLog: true}); res != nil {
common.ErrorStrResp(c, "file exists", 403)
return
}
}
}
if err := fs.Rename(c, reqPath, req.Name); err != nil {
common.ErrorResp(c, err, 500)
return
Expand Down
16 changes: 16 additions & 0 deletions server/handles/fsup.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,20 @@ func FsStream(c *gin.Context) {
return
}
asTask := c.GetHeader("As-Task") == "true"
overwrite := c.GetHeader("Overwrite") != "false"
user := c.MustGet("user").(*model.User)
path, err = user.JoinPath(path)
if err != nil {
common.ErrorResp(c, err, 403)
return
}
if !overwrite {
if res, _ := fs.Get(c, path, &fs.GetArgs{NoLog: true}); res != nil {
_, _ = io.Copy(io.Discard, c.Request.Body)
common.ErrorStrResp(c, "file exists", 403)
return
}
}
dir, name := stdpath.Split(path)
sizeStr := c.GetHeader("Content-Length")
size, err := strconv.ParseInt(sizeStr, 10, 64)
Expand Down Expand Up @@ -85,12 +93,20 @@ func FsForm(c *gin.Context) {
return
}
asTask := c.GetHeader("As-Task") == "true"
overwrite := c.GetHeader("Overwrite") != "false"
user := c.MustGet("user").(*model.User)
path, err = user.JoinPath(path)
if err != nil {
common.ErrorResp(c, err, 403)
return
}
if !overwrite {
if res, _ := fs.Get(c, path, &fs.GetArgs{NoLog: true}); res != nil {
_, _ = io.Copy(io.Discard, c.Request.Body)
common.ErrorStrResp(c, "file exists", 403)
return
}
}
storage, err := fs.GetStorage(path, &fs.GetStoragesArgs{})
if err != nil {
common.ErrorResp(c, err, 400)
Expand Down

0 comments on commit 59e0228

Please sign in to comment.