alist/server/handles/fsmanage.go

264 lines
5.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"
"strconv"
"time"
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"
)
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() {
2022-06-29 09:08:31 +00:00
meta, err := db.GetNearestMeta(req.Path)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
2022-06-30 07:53:57 +00:00
if !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)
}
2022-06-30 07:53:57 +00:00
func canWrite(meta *model.Meta, path string) bool {
2022-06-29 09:08:31 +00:00
if meta == nil || !meta.Write {
return false
}
2022-06-30 07:41:58 +00:00
return meta.WSub || meta.Path == path
2022-06-29 09:08:31 +00:00
}
2022-06-29 07:01:22 +00:00
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
}
}
2022-06-30 14:51:49 +00:00
fs.ClearCache(req.Dir)
2022-06-29 07:01:22 +00:00
common.SuccessResp(c)
}
func FsPut(c *gin.Context) {
path := c.GetHeader("File-Path")
2022-07-01 09:11:22 +00:00
asTask := c.GetHeader("As-Task") == "true"
2022-06-29 07:01:22 +00:00
user := c.MustGet("user").(*model.User)
path = stdpath.Join(user.BasePath, path)
2022-06-30 07:53:57 +00:00
if !user.CanWrite() {
2022-06-29 09:08:31 +00:00
meta, err := db.GetNearestMeta(path)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
2022-06-30 07:53:57 +00:00
if !canWrite(meta, 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
dir, name := stdpath.Split(path)
sizeStr := c.GetHeader("Content-Length")
size, err := strconv.ParseInt(sizeStr, 10, 64)
if err != nil {
common.ErrorResp(c, err, 400)
return
}
2022-07-01 09:11:22 +00:00
stream := &model.FileStream{
2022-06-29 07:01:22 +00:00
Obj: model.Object{
Name: name,
Size: size,
Modified: time.Now(),
},
2022-07-01 07:04:02 +00:00
ReadCloser: c.Request.Body,
Mimetype: c.GetHeader("Content-Type"),
2022-07-01 09:11:22 +00:00
WebPutAsTask: asTask,
}
if asTask {
err = fs.PutAsTask(dir, stream)
} else {
err = fs.PutDirectly(c, dir, stream)
}
if err != nil {
2022-06-29 07:01:22 +00:00
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c)
}
2022-06-29 08:08:55 +00:00
2022-06-29 09:08:31 +00:00
// Link return real link, just for proxy program, it may contain cookie
2022-06-29 08:08:55 +00:00
func Link(c *gin.Context) {
2022-06-29 09:08:31 +00:00
var req FsGetOrLinkReq
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),
2022-08-12 06:51:23 +00:00
utils.EncodePath(req.Path, true),
2022-08-11 12:32:17 +00:00
sign.Sign(stdpath.Base(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
}