2022-07-11 17:12:50 +08:00
|
|
|
package handles
|
2022-06-29 15:01:22 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-08-03 14:26:59 +08:00
|
|
|
stdpath "path"
|
|
|
|
|
2022-06-29 17:08:31 +08:00
|
|
|
"github.com/alist-org/alist/v3/internal/db"
|
2022-06-29 18:03:12 +08:00
|
|
|
"github.com/alist-org/alist/v3/internal/errs"
|
2022-06-29 15:01:22 +08:00
|
|
|
"github.com/alist-org/alist/v3/internal/fs"
|
|
|
|
"github.com/alist-org/alist/v3/internal/model"
|
2022-06-29 16:08:55 +08:00
|
|
|
"github.com/alist-org/alist/v3/internal/sign"
|
2022-08-11 20:32:17 +08:00
|
|
|
"github.com/alist-org/alist/v3/pkg/utils"
|
2022-06-29 15:01:22 +08:00
|
|
|
"github.com/alist-org/alist/v3/server/common"
|
|
|
|
"github.com/gin-gonic/gin"
|
2022-08-19 11:02:00 +08:00
|
|
|
"github.com/pkg/errors"
|
2022-06-29 15:01:22 +08:00
|
|
|
)
|
|
|
|
|
2022-06-29 16:08:55 +08:00
|
|
|
type MkdirOrLinkReq struct {
|
|
|
|
Path string `json:"path" form:"path"`
|
2022-06-29 15:01:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func FsMkdir(c *gin.Context) {
|
2022-06-29 16:08:55 +08:00
|
|
|
var req MkdirOrLinkReq
|
2022-06-29 15:01:22 +08: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 15:53:57 +08:00
|
|
|
if !user.CanWrite() {
|
2022-09-19 13:35:37 +08:00
|
|
|
meta, err := db.GetNearestMeta(stdpath.Dir(req.Path))
|
2022-06-29 17:08:31 +08:00
|
|
|
if err != nil {
|
2022-08-19 11:02:00 +08:00
|
|
|
if !errors.Is(errors.Cause(err), errs.MetaNotFound) {
|
|
|
|
common.ErrorResp(c, err, 500, true)
|
|
|
|
return
|
|
|
|
}
|
2022-06-29 17:08:31 +08:00
|
|
|
}
|
2022-06-30 15:53:57 +08:00
|
|
|
if !canWrite(meta, req.Path) {
|
2022-06-29 18:03:12 +08:00
|
|
|
common.ErrorResp(c, errs.PermissionDenied, 403)
|
2022-06-29 17:08:31 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-06-29 15:01:22 +08:00
|
|
|
if err := fs.MakeDir(c, req.Path); err != nil {
|
|
|
|
common.ErrorResp(c, err, 500)
|
|
|
|
return
|
|
|
|
}
|
2022-06-30 22:51:49 +08:00
|
|
|
fs.ClearCache(stdpath.Dir(req.Path))
|
2022-06-29 15:01:22 +08:00
|
|
|
common.SuccessResp(c)
|
|
|
|
}
|
|
|
|
|
2022-06-30 15:53:57 +08:00
|
|
|
func canWrite(meta *model.Meta, path string) bool {
|
2022-06-29 17:08:31 +08:00
|
|
|
if meta == nil || !meta.Write {
|
|
|
|
return false
|
|
|
|
}
|
2022-06-30 15:41:58 +08:00
|
|
|
return meta.WSub || meta.Path == path
|
2022-06-29 17:08:31 +08:00
|
|
|
}
|
|
|
|
|
2022-06-29 15:01:22 +08: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 18:03:12 +08:00
|
|
|
if !user.CanMove() {
|
|
|
|
common.ErrorResp(c, errs.PermissionDenied, 403)
|
|
|
|
return
|
|
|
|
}
|
2022-06-29 15:01:22 +08: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 22:51:49 +08:00
|
|
|
fs.ClearCache(req.SrcDir)
|
|
|
|
fs.ClearCache(req.DstDir)
|
2022-06-29 15:01:22 +08: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 18:03:12 +08:00
|
|
|
if !user.CanCopy() {
|
|
|
|
common.ErrorResp(c, errs.PermissionDenied, 403)
|
|
|
|
return
|
|
|
|
}
|
2022-06-29 15:01:22 +08: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 22:51:49 +08:00
|
|
|
if len(req.Names) != len(addedTask) {
|
|
|
|
fs.ClearCache(req.DstDir)
|
|
|
|
}
|
2022-06-29 15:01:22 +08: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 18:03:12 +08:00
|
|
|
if !user.CanRename() {
|
|
|
|
common.ErrorResp(c, errs.PermissionDenied, 403)
|
|
|
|
return
|
|
|
|
}
|
2022-06-29 15:01:22 +08: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 22:51:49 +08:00
|
|
|
fs.ClearCache(stdpath.Dir(req.Path))
|
2022-06-29 15:01:22 +08:00
|
|
|
common.SuccessResp(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
type RemoveReq struct {
|
2022-06-30 22:51:49 +08:00
|
|
|
Dir string `json:"dir"`
|
2022-06-29 15:01:22 +08: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 18:03:12 +08:00
|
|
|
if !user.CanRemove() {
|
|
|
|
common.ErrorResp(c, errs.PermissionDenied, 403)
|
|
|
|
return
|
|
|
|
}
|
2022-06-30 22:51:49 +08:00
|
|
|
req.Dir = stdpath.Join(user.BasePath, req.Dir)
|
2022-06-29 15:01:22 +08:00
|
|
|
for _, name := range req.Names {
|
2022-06-30 22:51:49 +08:00
|
|
|
err := fs.Remove(c, stdpath.Join(req.Dir, name))
|
2022-06-29 15:01:22 +08:00
|
|
|
if err != nil {
|
|
|
|
common.ErrorResp(c, err, 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-09-14 20:28:52 +08:00
|
|
|
//fs.ClearCache(req.Dir)
|
2022-06-29 15:01:22 +08:00
|
|
|
common.SuccessResp(c)
|
|
|
|
}
|
|
|
|
|
2022-08-14 23:46:30 +08:00
|
|
|
// Link return real link, just for proxy program, it may contain cookie, so just allowed for admin
|
2022-06-29 16:08:55 +08:00
|
|
|
func Link(c *gin.Context) {
|
2022-08-14 23:46:30 +08:00
|
|
|
var req MkdirOrLinkReq
|
2022-06-29 16:08:55 +08: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 14:45:39 +08:00
|
|
|
storage, err := fs.GetStorage(rawPath)
|
2022-06-29 16:08:55 +08:00
|
|
|
if err != nil {
|
|
|
|
common.ErrorResp(c, err, 500)
|
|
|
|
return
|
|
|
|
}
|
2022-07-10 14:45:39 +08:00
|
|
|
if storage.Config().OnlyLocal {
|
2022-06-29 16:08:55 +08:00
|
|
|
common.SuccessResp(c, model.Link{
|
2022-08-11 20:32:17 +08:00
|
|
|
URL: fmt.Sprintf("%s/p%s?d&sign=%s",
|
|
|
|
common.GetApiUrl(c.Request),
|
2022-08-12 14:51:23 +08:00
|
|
|
utils.EncodePath(req.Path, true),
|
2022-08-11 20:32:17 +08:00
|
|
|
sign.Sign(stdpath.Base(rawPath))),
|
2022-06-29 16:08:55 +08: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
|
|
|
|
}
|