copy api

pull/548/head
微凉 2022-01-26 14:07:51 +08:00
parent c70fc3fc4b
commit 8fc7c716c0
4 changed files with 65 additions and 3 deletions

View File

@ -0,0 +1,61 @@
package file
import (
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/drivers/operate"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
)
func Copy(c *gin.Context) {
var req MoveCopyReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
if len(req.Names) == 0 {
common.ErrorStrResp(c, "Empty file names", 400)
return
}
if model.AccountsCount() > 1 && (req.SrcDir == "/" || req.DstDir == "/") {
common.ErrorStrResp(c, "Can't operate root folder", 400)
return
}
srcAccount, srcPath, srcDriver, err := common.ParsePath(utils.Join(req.SrcDir, req.Names[0]))
if err != nil {
common.ErrorResp(c, err, 500)
return
}
dstAccount, dstPath, _, err := common.ParsePath(utils.Join(req.DstDir, req.Names[0]))
if err != nil {
common.ErrorResp(c, err, 500)
return
}
if srcAccount.Name != dstAccount.Name {
common.ErrorStrResp(c, "Can't copy files between two accounts", 400)
return
}
if srcPath == "/" || dstPath == "/" {
common.ErrorStrResp(c, "Can't copy root folder", 400)
return
}
srcDir, dstDir := utils.Dir(srcPath), utils.Dir(dstPath)
for i, name := range req.Names {
clearCache := false
if i == len(req.Names)-1 {
clearCache = true
}
err := operate.Copy(srcDriver, srcAccount, utils.Join(srcDir, name), utils.Join(dstDir, name), clearCache)
if err != nil {
if i == 0 {
_ = base.DeleteCache(srcDir, srcAccount)
_ = base.DeleteCache(dstDir, dstAccount)
}
common.ErrorResp(c, err, 500)
return
}
}
common.SuccessResp(c)
}

View File

@ -9,14 +9,14 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
type MoveReq struct { type MoveCopyReq struct {
SrcDir string `json:"src_dir"` SrcDir string `json:"src_dir"`
DstDir string `json:"dst_dir"` DstDir string `json:"dst_dir"`
Names []string `json:"names"` Names []string `json:"names"`
} }
func Move(c *gin.Context) { func Move(c *gin.Context) {
var req MoveReq var req MoveCopyReq
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400) common.ErrorResp(c, err, 400)
return return

View File

@ -24,7 +24,7 @@ func Rename(c *gin.Context) {
return return
} }
if path_ == "/" { if path_ == "/" {
common.ErrorStrResp(c, "Can't account name here", 400) common.ErrorStrResp(c, "Can't edit account name here", 400)
return return
} }
err = operate.Move(driver, account, path_, utils.Join(utils.Dir(path_), req.Name), true) err = operate.Move(driver, account, path_, utils.Join(utils.Dir(path_), req.Name), true)

View File

@ -54,6 +54,7 @@ func InitApiRouter(r *gin.Engine) {
admin.POST("/mkdir", file.Mkdir) admin.POST("/mkdir", file.Mkdir)
admin.POST("/rename", file.Rename) admin.POST("/rename", file.Rename)
admin.POST("/move", file.Move) admin.POST("/move", file.Move)
admin.POST("/copy", file.Copy)
admin.POST("/folder", file.Folder) admin.POST("/folder", file.Folder)
} }
WebDav(r) WebDav(r)