mirror of https://github.com/Xhofe/alist
feat(fs): add `overwrite` option to preventing unintentional overwriting (#7809)
parent
bb40e2e2cd
commit
59e02287b2
|
@ -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) {
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
|
@ -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) {
|
||||
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue