alist/server/handles/fsmanage.go

371 lines
8.5 KiB
Go
Raw Permalink Normal View History

2022-07-11 09:12:50 +00:00
package handles
2022-06-29 07:01:22 +00:00
import (
"fmt"
"github.com/alist-org/alist/v3/internal/task"
"io"
2022-08-03 06:26:59 +00:00
stdpath "path"
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"
"github.com/alist-org/alist/v3/internal/op"
2022-06-29 08:08:55 +00:00
"github.com/alist-org/alist/v3/internal/sign"
"github.com/alist-org/alist/v3/pkg/generic"
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"
log "github.com/sirupsen/logrus"
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)
reqPath, err := user.JoinPath(req.Path)
if err != nil {
common.ErrorResp(c, err, 403)
return
}
2022-06-30 07:53:57 +00:00
if !user.CanWrite() {
meta, err := op.GetNearestMeta(stdpath.Dir(reqPath))
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, reqPath) {
2022-06-29 10:03:12 +00:00
common.ErrorResp(c, errs.PermissionDenied, 403)
2022-06-29 09:08:31 +00:00
return
}
}
if err := fs.MakeDir(c, reqPath); err != nil {
2022-06-29 07:01:22 +00:00
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c)
}
type MoveCopyReq struct {
SrcDir string `json:"src_dir"`
DstDir string `json:"dst_dir"`
Names []string `json:"names"`
Overwrite bool `json:"overwrite"`
2022-06-29 07:01:22 +00:00
}
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
}
srcDir, err := user.JoinPath(req.SrcDir)
if err != nil {
common.ErrorResp(c, err, 403)
return
}
dstDir, err := user.JoinPath(req.DstDir)
if err != nil {
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, fmt.Sprintf("file [%s] exists", name), 403)
return
}
}
}
for i, name := range req.Names {
err := fs.Move(c, stdpath.Join(srcDir, name), dstDir, len(req.Names) > i+1)
2022-06-29 07:01:22 +00:00
if err != nil {
common.ErrorResp(c, err, 500)
return
}
}
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
}
srcDir, err := user.JoinPath(req.SrcDir)
if err != nil {
common.ErrorResp(c, err, 403)
return
}
dstDir, err := user.JoinPath(req.DstDir)
if err != nil {
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, fmt.Sprintf("file [%s] exists", name), 403)
return
}
}
}
2024-12-25 13:09:54 +00:00
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)
if t != nil {
addedTasks = append(addedTasks, t)
2022-06-29 07:01:22 +00:00
}
if err != nil {
common.ErrorResp(c, err, 500)
return
}
}
common.SuccessResp(c, gin.H{
"tasks": getTaskInfos(addedTasks),
})
2022-06-29 07:01:22 +00:00
}
type RenameReq struct {
Path string `json:"path"`
Name string `json:"name"`
Overwrite bool `json:"overwrite"`
2022-06-29 07:01:22 +00:00
}
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
}
reqPath, err := user.JoinPath(req.Path)
if err != nil {
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, fmt.Sprintf("file [%s] exists", req.Name), 403)
return
}
}
}
if err := fs.Rename(c, reqPath, req.Name); err != nil {
2022-06-29 07:01:22 +00:00
common.ErrorResp(c, err, 500)
return
}
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
}
reqDir, err := user.JoinPath(req.Dir)
if err != nil {
common.ErrorResp(c, err, 403)
return
}
2022-06-29 07:01:22 +00:00
for _, name := range req.Names {
err := fs.Remove(c, stdpath.Join(reqDir, 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)
}
type RemoveEmptyDirectoryReq struct {
SrcDir string `json:"src_dir"`
}
func FsRemoveEmptyDirectory(c *gin.Context) {
var req RemoveEmptyDirectoryReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
user := c.MustGet("user").(*model.User)
if !user.CanRemove() {
common.ErrorResp(c, errs.PermissionDenied, 403)
return
}
srcDir, err := user.JoinPath(req.SrcDir)
if err != nil {
common.ErrorResp(c, err, 403)
return
}
meta, err := op.GetNearestMeta(srcDir)
if err != nil {
if !errors.Is(errors.Cause(err), errs.MetaNotFound) {
common.ErrorResp(c, err, 500, true)
return
}
}
c.Set("meta", meta)
rootFiles, err := fs.List(c, srcDir, &fs.ListArgs{})
if err != nil {
common.ErrorResp(c, err, 500)
return
}
// record the file path
filePathMap := make(map[model.Obj]string)
// record the parent file
fileParentMap := make(map[model.Obj]model.Obj)
// removing files
removingFiles := generic.NewQueue[model.Obj]()
// removed files
removedFiles := make(map[string]bool)
for _, file := range rootFiles {
if !file.IsDir() {
continue
}
removingFiles.Push(file)
filePathMap[file] = srcDir
}
for !removingFiles.IsEmpty() {
removingFile := removingFiles.Pop()
removingFilePath := fmt.Sprintf("%s/%s", filePathMap[removingFile], removingFile.GetName())
if removedFiles[removingFilePath] {
continue
}
subFiles, err := fs.List(c, removingFilePath, &fs.ListArgs{Refresh: true})
if err != nil {
common.ErrorResp(c, err, 500)
return
}
if len(subFiles) == 0 {
// remove empty directory
err = fs.Remove(c, removingFilePath)
removedFiles[removingFilePath] = true
if err != nil {
common.ErrorResp(c, err, 500)
return
}
// recheck parent folder
parentFile, exist := fileParentMap[removingFile]
if exist {
removingFiles.Push(parentFile)
}
} else {
// recursive remove
for _, subFile := range subFiles {
if !subFile.IsDir() {
continue
}
removingFiles.Push(subFile)
filePathMap[subFile] = removingFilePath
fileParentMap[subFile] = removingFile
}
}
}
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)
// why need not join base_path? because it's always the full path
rawPath := req.Path
storage, err := fs.GetStorage(rawPath, &fs.GetStoragesArgs{})
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(), Header: c.Request.Header, HttpReq: c.Request})
2022-06-29 08:08:55 +00:00
if err != nil {
common.ErrorResp(c, err, 500)
return
}
if link.MFile != nil {
feat: Crypt driver, improve http/webdav handling (#4884) this PR has several enhancements, fixes, and features: - [x] Crypt: a transparent encryption driver. Anyone can easily, and safely store encrypted data on the remote storage provider. Consider your data is safely stored in the safe, and the storage provider can only see the safe, but not your data. - [x] Optional: compatible with [Rclone Crypt](https://rclone.org/crypt/). More ways to manipulate the encrypted data. - [x] directory and filename encryption - [x] server-side encryption mode (server encrypts & decrypts all data, all data flows thru the server) - [x] obfuscate sensitive information internally - [x] introduced a server memory-cached multi-thread downloader. - [x] Driver: **Quark** enabled this feature, faster load in any single thread scenario. e.g. media player directly playing from the link, now it's faster. - [x] general improvement on HTTP/WebDAV stream processing & header handling & response handling - [x] Driver: **Mega** driver support ranged http header - [x] Driver: **Quark** fix bug of not closing HTTP request to Quark server while user end has closed connection to alist ## Crypt, a transparent Encrypt/Decrypt Driver. (Rclone Crypt compatible) e.g. Crypt mount path -> /vault Crypt remote path -> /ali/encrypted Aliyun mount paht -> /ali when the user uploads a.jpg to /vault, the data will be encrypted and saved to /ali/encrypted/xxxxx. And when the user wants to access a.jpg, it's automatically decrypted, and the user can do anything with it. Since it's Rclone Crypt compatible, users can download /ali/encrypted/xxxxx and decrypt it with rclone crypt tool. Or the user can mount this folder using rclone, then mount the decrypted folder in Linux... NB. Some breaking changes is made to make it follow global standard, e.g. processing the HTTP header properly. close #4679 close #4827 Co-authored-by: Sean He <866155+seanhe26@users.noreply.github.com> Co-authored-by: Andy Hsu <i@nn.ci>
2023-08-02 06:40:36 +00:00
defer func(ReadSeekCloser io.ReadCloser) {
err := ReadSeekCloser.Close()
if err != nil {
log.Errorf("close link data error: %v", err)
}
}(link.MFile)
}
2022-06-29 08:08:55 +00:00
common.SuccessResp(c, link)
return
}