alist/server/handles/fsmanage.go

212 lines
4.9 KiB
Go
Raw Normal View History

2022-07-11 09:12:50 +00:00
package handles
2022-06-29 07:01:22 +00:00
import (
"fmt"
2022-08-03 06:26:59 +00:00
stdpath "path"
2022-06-29 09:08:31 +00:00
"github.com/alist-org/alist/v3/internal/db"
2022-06-29 10:03:12 +00:00
"github.com/alist-org/alist/v3/internal/errs"
2022-06-29 07:01:22 +00:00
"github.com/alist-org/alist/v3/internal/fs"
"github.com/alist-org/alist/v3/internal/model"
2022-06-29 08:08:55 +00:00
"github.com/alist-org/alist/v3/internal/sign"
2022-08-11 12:32:17 +00:00
"github.com/alist-org/alist/v3/pkg/utils"
2022-06-29 07:01:22 +00:00
"github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
2022-06-29 07:01:22 +00:00
)
2022-06-29 08:08:55 +00:00
type MkdirOrLinkReq struct {
Path string `json:"path" form:"path"`
2022-06-29 07:01:22 +00:00
}
func FsMkdir(c *gin.Context) {
2022-06-29 08:08:55 +00:00
var req MkdirOrLinkReq
2022-06-29 07:01:22 +00:00
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
user := c.MustGet("user").(*model.User)
req.Path = stdpath.Join(user.BasePath, req.Path)
2022-06-30 07:53:57 +00:00
if !user.CanWrite() {
meta, err := db.GetNearestMeta(stdpath.Dir(req.Path))
2022-06-29 09:08:31 +00:00
if err != nil {
if !errors.Is(errors.Cause(err), errs.MetaNotFound) {
common.ErrorResp(c, err, 500, true)
return
}
2022-06-29 09:08:31 +00:00
}
if !common.CanWrite(meta, req.Path) {
2022-06-29 10:03:12 +00:00
common.ErrorResp(c, errs.PermissionDenied, 403)
2022-06-29 09:08:31 +00:00
return
}
}
2022-06-29 07:01:22 +00:00
if err := fs.MakeDir(c, req.Path); err != nil {
common.ErrorResp(c, err, 500)
return
}
2022-06-30 14:51:49 +00:00
fs.ClearCache(stdpath.Dir(req.Path))
2022-06-29 07:01:22 +00:00
common.SuccessResp(c)
}
type MoveCopyReq struct {
SrcDir string `json:"src_dir"`
DstDir string `json:"dst_dir"`
Names []string `json:"names"`
}
func FsMove(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
}
user := c.MustGet("user").(*model.User)
2022-06-29 10:03:12 +00:00
if !user.CanMove() {
common.ErrorResp(c, errs.PermissionDenied, 403)
return
}
2022-06-29 07:01:22 +00:00
req.SrcDir = stdpath.Join(user.BasePath, req.SrcDir)
req.DstDir = stdpath.Join(user.BasePath, req.DstDir)
for _, name := range req.Names {
err := fs.Move(c, stdpath.Join(req.SrcDir, name), req.DstDir)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
}
2022-06-30 14:51:49 +00:00
fs.ClearCache(req.SrcDir)
fs.ClearCache(req.DstDir)
2022-06-29 07:01:22 +00:00
common.SuccessResp(c)
}
func FsCopy(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
}
user := c.MustGet("user").(*model.User)
2022-06-29 10:03:12 +00:00
if !user.CanCopy() {
common.ErrorResp(c, errs.PermissionDenied, 403)
return
}
2022-06-29 07:01:22 +00:00
req.SrcDir = stdpath.Join(user.BasePath, req.SrcDir)
req.DstDir = stdpath.Join(user.BasePath, req.DstDir)
var addedTask []string
for _, name := range req.Names {
ok, err := fs.Copy(c, stdpath.Join(req.SrcDir, name), req.DstDir)
if ok {
addedTask = append(addedTask, name)
}
if err != nil {
common.ErrorResp(c, err, 500)
return
}
}
2022-06-30 14:51:49 +00:00
if len(req.Names) != len(addedTask) {
fs.ClearCache(req.DstDir)
}
2022-06-29 07:01:22 +00:00
if len(addedTask) > 0 {
common.SuccessResp(c, fmt.Sprintf("Added %d tasks", len(addedTask)))
} else {
common.SuccessResp(c)
}
}
type RenameReq struct {
Path string `json:"path"`
Name string `json:"name"`
}
func FsRename(c *gin.Context) {
var req RenameReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
user := c.MustGet("user").(*model.User)
2022-06-29 10:03:12 +00:00
if !user.CanRename() {
common.ErrorResp(c, errs.PermissionDenied, 403)
return
}
2022-06-29 07:01:22 +00:00
req.Path = stdpath.Join(user.BasePath, req.Path)
if err := fs.Rename(c, req.Path, req.Name); err != nil {
common.ErrorResp(c, err, 500)
return
}
2022-06-30 14:51:49 +00:00
fs.ClearCache(stdpath.Dir(req.Path))
2022-06-29 07:01:22 +00:00
common.SuccessResp(c)
}
type RemoveReq struct {
2022-06-30 14:51:49 +00:00
Dir string `json:"dir"`
2022-06-29 07:01:22 +00:00
Names []string `json:"names"`
}
func FsRemove(c *gin.Context) {
var req RemoveReq
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
}
user := c.MustGet("user").(*model.User)
2022-06-29 10:03:12 +00:00
if !user.CanRemove() {
common.ErrorResp(c, errs.PermissionDenied, 403)
return
}
2022-06-30 14:51:49 +00:00
req.Dir = stdpath.Join(user.BasePath, req.Dir)
2022-06-29 07:01:22 +00:00
for _, name := range req.Names {
2022-06-30 14:51:49 +00:00
err := fs.Remove(c, stdpath.Join(req.Dir, name))
2022-06-29 07:01:22 +00:00
if err != nil {
common.ErrorResp(c, err, 500)
return
}
}
//fs.ClearCache(req.Dir)
2022-06-29 07:01:22 +00:00
common.SuccessResp(c)
}
2022-08-14 15:46:30 +00:00
// Link return real link, just for proxy program, it may contain cookie, so just allowed for admin
2022-06-29 08:08:55 +00:00
func Link(c *gin.Context) {
2022-08-14 15:46:30 +00:00
var req MkdirOrLinkReq
2022-06-29 08:08:55 +00:00
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
user := c.MustGet("user").(*model.User)
rawPath := stdpath.Join(user.BasePath, req.Path)
2022-07-10 06:45:39 +00:00
storage, err := fs.GetStorage(rawPath)
2022-06-29 08:08:55 +00:00
if err != nil {
common.ErrorResp(c, err, 500)
return
}
2022-07-10 06:45:39 +00:00
if storage.Config().OnlyLocal {
2022-06-29 08:08:55 +00:00
common.SuccessResp(c, model.Link{
2022-08-11 12:32:17 +00:00
URL: fmt.Sprintf("%s/p%s?d&sign=%s",
common.GetApiUrl(c.Request),
utils.EncodePath(rawPath, true),
sign.Sign(rawPath)),
2022-06-29 08:08:55 +00:00
})
return
}
link, _, err := fs.Link(c, rawPath, model.LinkArgs{IP: c.ClientIP()})
if err != nil {
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c, link)
return
}