2022-07-11 09:12:50 +00:00
|
|
|
package handles
|
2022-06-29 07:01:22 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-11-01 15:32:26 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/task"
|
2023-03-14 05:38:41 +00:00
|
|
|
"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"
|
2022-12-18 11:51:20 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/op"
|
2022-06-29 08:08:55 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/sign"
|
2023-03-10 11:01:49 +00:00
|
|
|
"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"
|
2022-08-19 03:02:00 +00:00
|
|
|
"github.com/pkg/errors"
|
2023-03-14 05:38:41 +00:00
|
|
|
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)
|
2022-11-30 13:38:00 +00:00
|
|
|
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() {
|
2022-12-18 11:51:20 +00:00
|
|
|
meta, err := op.GetNearestMeta(stdpath.Dir(reqPath))
|
2022-06-29 09:08:31 +00:00
|
|
|
if err != nil {
|
2022-08-19 03:02:00 +00:00
|
|
|
if !errors.Is(errors.Cause(err), errs.MetaNotFound) {
|
|
|
|
common.ErrorResp(c, err, 500, true)
|
|
|
|
return
|
|
|
|
}
|
2022-06-29 09:08:31 +00:00
|
|
|
}
|
2022-11-30 13:38:00 +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
|
|
|
|
}
|
|
|
|
}
|
2022-11-30 13:38:00 +00:00
|
|
|
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 {
|
2025-01-18 15:39:07 +00:00
|
|
|
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
|
|
|
|
}
|
2022-11-30 13:38:00 +00:00
|
|
|
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
|
|
|
|
}
|
2025-01-18 15:39:07 +00:00
|
|
|
if !req.Overwrite {
|
|
|
|
for _, name := range req.Names {
|
|
|
|
if res, _ := fs.Get(c, stdpath.Join(dstDir, name), &fs.GetArgs{NoLog: true}); res != nil {
|
2025-01-27 12:09:17 +00:00
|
|
|
common.ErrorStrResp(c, fmt.Sprintf("file [%s] exists", name), 403)
|
2025-01-18 15:39:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-20 07:02:40 +00:00
|
|
|
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
|
|
|
|
}
|
2022-11-30 13:38:00 +00:00
|
|
|
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
|
|
|
|
}
|
2025-01-18 15:39:07 +00:00
|
|
|
if !req.Overwrite {
|
|
|
|
for _, name := range req.Names {
|
|
|
|
if res, _ := fs.Get(c, stdpath.Join(dstDir, name), &fs.GetArgs{NoLog: true}); res != nil {
|
2025-01-27 12:09:17 +00:00
|
|
|
common.ErrorStrResp(c, fmt.Sprintf("file [%s] exists", name), 403)
|
2025-01-18 15:39:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-12-25 13:09:54 +00:00
|
|
|
var addedTasks []task.TaskExtensionInfo
|
2022-12-20 07:02:40 +00:00
|
|
|
for i, name := range req.Names {
|
2023-12-03 06:44:20 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2023-12-03 06:44:20 +00:00
|
|
|
common.SuccessResp(c, gin.H{
|
|
|
|
"tasks": getTaskInfos(addedTasks),
|
|
|
|
})
|
2022-06-29 07:01:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RenameReq struct {
|
2025-01-18 15:39:07 +00:00
|
|
|
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
|
|
|
|
}
|
2022-11-30 13:38:00 +00:00
|
|
|
reqPath, err := user.JoinPath(req.Path)
|
|
|
|
if err != nil {
|
|
|
|
common.ErrorResp(c, err, 403)
|
|
|
|
return
|
|
|
|
}
|
2025-01-18 15:39:07 +00:00
|
|
|
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 {
|
2025-01-27 12:09:17 +00:00
|
|
|
common.ErrorStrResp(c, fmt.Sprintf("file [%s] exists", req.Name), 403)
|
2025-01-18 15:39:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-30 13:38:00 +00:00
|
|
|
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
|
|
|
|
}
|
2022-11-30 13:38:00 +00:00
|
|
|
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 {
|
2022-11-30 13:38:00 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2022-09-14 12:28:52 +00:00
|
|
|
//fs.ClearCache(req.Dir)
|
2022-06-29 07:01:22 +00:00
|
|
|
common.SuccessResp(c)
|
|
|
|
}
|
|
|
|
|
2023-04-13 07:39:21 +00:00
|
|
|
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
|
|
|
|
}
|
2022-11-30 13:38:00 +00:00
|
|
|
//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
|
2023-04-06 16:02:07 +00:00
|
|
|
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),
|
2022-11-11 08:24:25 +00:00
|
|
|
utils.EncodePath(rawPath, true),
|
|
|
|
sign.Sign(rawPath)),
|
2022-06-29 08:08:55 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2023-05-26 13:54:57 +00:00
|
|
|
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
|
|
|
|
}
|
2023-08-27 13:14:23 +00:00
|
|
|
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()
|
2023-03-14 05:38:41 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("close link data error: %v", err)
|
|
|
|
}
|
2023-08-27 13:14:23 +00:00
|
|
|
}(link.MFile)
|
2023-03-14 05:38:41 +00:00
|
|
|
}
|
2022-06-29 08:08:55 +00:00
|
|
|
common.SuccessResp(c, link)
|
|
|
|
return
|
|
|
|
}
|