2022-09-22 08:53:58 +00:00
|
|
|
package handles
|
|
|
|
|
|
|
|
import (
|
2024-11-01 15:32:26 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/task"
|
2025-01-27 12:20:09 +00:00
|
|
|
"github.com/alist-org/alist/v3/pkg/utils"
|
2023-09-03 07:40:40 +00:00
|
|
|
"io"
|
2022-09-22 08:53:58 +00:00
|
|
|
"net/url"
|
|
|
|
stdpath "path"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/alist-org/alist/v3/internal/fs"
|
|
|
|
"github.com/alist-org/alist/v3/internal/model"
|
2024-11-01 15:32:26 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/stream"
|
2022-09-22 08:53:58 +00:00
|
|
|
"github.com/alist-org/alist/v3/server/common"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2023-08-28 08:33:09 +00:00
|
|
|
func getLastModified(c *gin.Context) time.Time {
|
|
|
|
now := time.Now()
|
|
|
|
lastModifiedStr := c.GetHeader("Last-Modified")
|
|
|
|
lastModifiedMillisecond, err := strconv.ParseInt(lastModifiedStr, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return now
|
|
|
|
}
|
|
|
|
lastModified := time.UnixMilli(lastModifiedMillisecond)
|
|
|
|
return lastModified
|
|
|
|
}
|
|
|
|
|
2022-09-22 08:53:58 +00:00
|
|
|
func FsStream(c *gin.Context) {
|
|
|
|
path := c.GetHeader("File-Path")
|
|
|
|
path, err := url.PathUnescape(path)
|
|
|
|
if err != nil {
|
|
|
|
common.ErrorResp(c, err, 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
asTask := c.GetHeader("As-Task") == "true"
|
2025-01-18 15:39:07 +00:00
|
|
|
overwrite := c.GetHeader("Overwrite") != "false"
|
2022-09-22 08:53:58 +00:00
|
|
|
user := c.MustGet("user").(*model.User)
|
2022-11-30 13:38:00 +00:00
|
|
|
path, err = user.JoinPath(path)
|
|
|
|
if err != nil {
|
|
|
|
common.ErrorResp(c, err, 403)
|
|
|
|
return
|
|
|
|
}
|
2025-01-18 15:39:07 +00:00
|
|
|
if !overwrite {
|
|
|
|
if res, _ := fs.Get(c, path, &fs.GetArgs{NoLog: true}); res != nil {
|
|
|
|
_, _ = io.Copy(io.Discard, c.Request.Body)
|
|
|
|
common.ErrorStrResp(c, "file exists", 403)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-09-22 08:53:58 +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
|
|
|
|
}
|
2025-01-27 12:20:09 +00:00
|
|
|
h := make(map[*utils.HashType]string)
|
|
|
|
if md5 := c.GetHeader("X-File-Md5"); md5 != "" {
|
|
|
|
h[utils.MD5] = md5
|
|
|
|
}
|
|
|
|
if sha1 := c.GetHeader("X-File-Sha1"); sha1 != "" {
|
|
|
|
h[utils.SHA1] = sha1
|
|
|
|
}
|
|
|
|
if sha256 := c.GetHeader("X-File-Sha256"); sha256 != "" {
|
|
|
|
h[utils.SHA256] = sha256
|
|
|
|
}
|
2023-08-27 13:14:23 +00:00
|
|
|
s := &stream.FileStream{
|
2022-09-22 08:53:58 +00:00
|
|
|
Obj: &model.Object{
|
|
|
|
Name: name,
|
|
|
|
Size: size,
|
2023-08-28 08:33:09 +00:00
|
|
|
Modified: getLastModified(c),
|
2025-01-27 12:20:09 +00:00
|
|
|
HashInfo: utils.NewHashInfoByMap(h),
|
2022-09-22 08:53:58 +00:00
|
|
|
},
|
2023-08-27 13:14:23 +00:00
|
|
|
Reader: c.Request.Body,
|
2022-09-22 08:53:58 +00:00
|
|
|
Mimetype: c.GetHeader("Content-Type"),
|
|
|
|
WebPutAsTask: asTask,
|
|
|
|
}
|
2024-12-25 13:09:54 +00:00
|
|
|
var t task.TaskExtensionInfo
|
2022-09-22 08:53:58 +00:00
|
|
|
if asTask {
|
2024-11-01 15:32:26 +00:00
|
|
|
t, err = fs.PutAsTask(c, dir, s)
|
2022-09-22 08:53:58 +00:00
|
|
|
} else {
|
2023-08-27 13:14:23 +00:00
|
|
|
err = fs.PutDirectly(c, dir, s, true)
|
2022-09-22 08:53:58 +00:00
|
|
|
}
|
2023-08-27 13:14:23 +00:00
|
|
|
defer c.Request.Body.Close()
|
2022-09-22 08:53:58 +00:00
|
|
|
if err != nil {
|
|
|
|
common.ErrorResp(c, err, 500)
|
|
|
|
return
|
|
|
|
}
|
2023-12-03 06:44:20 +00:00
|
|
|
if t == nil {
|
|
|
|
common.SuccessResp(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
common.SuccessResp(c, gin.H{
|
|
|
|
"task": getTaskInfo(t),
|
|
|
|
})
|
2022-09-22 08:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func FsForm(c *gin.Context) {
|
|
|
|
path := c.GetHeader("File-Path")
|
|
|
|
path, err := url.PathUnescape(path)
|
|
|
|
if err != nil {
|
|
|
|
common.ErrorResp(c, err, 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
asTask := c.GetHeader("As-Task") == "true"
|
2025-01-18 15:39:07 +00:00
|
|
|
overwrite := c.GetHeader("Overwrite") != "false"
|
2022-09-22 08:53:58 +00:00
|
|
|
user := c.MustGet("user").(*model.User)
|
2022-11-30 13:38:00 +00:00
|
|
|
path, err = user.JoinPath(path)
|
|
|
|
if err != nil {
|
|
|
|
common.ErrorResp(c, err, 403)
|
|
|
|
return
|
|
|
|
}
|
2025-01-18 15:39:07 +00:00
|
|
|
if !overwrite {
|
|
|
|
if res, _ := fs.Get(c, path, &fs.GetArgs{NoLog: true}); res != nil {
|
|
|
|
_, _ = io.Copy(io.Discard, c.Request.Body)
|
|
|
|
common.ErrorStrResp(c, "file exists", 403)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-04-06 16:02:07 +00:00
|
|
|
storage, err := fs.GetStorage(path, &fs.GetStoragesArgs{})
|
2022-09-22 08:53:58 +00:00
|
|
|
if err != nil {
|
|
|
|
common.ErrorResp(c, err, 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if storage.Config().NoUpload {
|
|
|
|
common.ErrorStrResp(c, "Current storage doesn't support upload", 405)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
file, err := c.FormFile("file")
|
|
|
|
if err != nil {
|
|
|
|
common.ErrorResp(c, err, 500)
|
|
|
|
return
|
|
|
|
}
|
2023-09-07 07:51:52 +00:00
|
|
|
f, err := file.Open()
|
2022-09-22 08:53:58 +00:00
|
|
|
if err != nil {
|
|
|
|
common.ErrorResp(c, err, 500)
|
|
|
|
return
|
|
|
|
}
|
2023-09-07 07:51:52 +00:00
|
|
|
defer f.Close()
|
2022-09-22 08:53:58 +00:00
|
|
|
dir, name := stdpath.Split(path)
|
2025-01-27 12:20:09 +00:00
|
|
|
h := make(map[*utils.HashType]string)
|
|
|
|
if md5 := c.GetHeader("X-File-Md5"); md5 != "" {
|
|
|
|
h[utils.MD5] = md5
|
|
|
|
}
|
|
|
|
if sha1 := c.GetHeader("X-File-Sha1"); sha1 != "" {
|
|
|
|
h[utils.SHA1] = sha1
|
|
|
|
}
|
|
|
|
if sha256 := c.GetHeader("X-File-Sha256"); sha256 != "" {
|
|
|
|
h[utils.SHA256] = sha256
|
|
|
|
}
|
2023-08-27 13:14:23 +00:00
|
|
|
s := stream.FileStream{
|
2022-09-22 08:53:58 +00:00
|
|
|
Obj: &model.Object{
|
|
|
|
Name: name,
|
|
|
|
Size: file.Size,
|
2023-08-28 08:33:09 +00:00
|
|
|
Modified: getLastModified(c),
|
2025-01-27 12:20:09 +00:00
|
|
|
HashInfo: utils.NewHashInfoByMap(h),
|
2022-09-22 08:53:58 +00:00
|
|
|
},
|
2023-08-27 13:14:23 +00:00
|
|
|
Reader: f,
|
2022-09-22 08:53:58 +00:00
|
|
|
Mimetype: file.Header.Get("Content-Type"),
|
2023-08-28 10:18:02 +00:00
|
|
|
WebPutAsTask: asTask,
|
2022-09-22 08:53:58 +00:00
|
|
|
}
|
2024-12-25 13:09:54 +00:00
|
|
|
var t task.TaskExtensionInfo
|
2022-09-22 08:53:58 +00:00
|
|
|
if asTask {
|
2023-09-07 07:51:52 +00:00
|
|
|
s.Reader = struct {
|
|
|
|
io.Reader
|
|
|
|
}{f}
|
2024-11-01 15:32:26 +00:00
|
|
|
t, err = fs.PutAsTask(c, dir, &s)
|
2022-09-22 08:53:58 +00:00
|
|
|
} else {
|
2023-09-07 07:51:52 +00:00
|
|
|
ss, err := stream.NewSeekableStream(s, nil)
|
|
|
|
if err != nil {
|
|
|
|
common.ErrorResp(c, err, 500)
|
|
|
|
return
|
|
|
|
}
|
2023-08-27 13:14:23 +00:00
|
|
|
err = fs.PutDirectly(c, dir, ss, true)
|
2022-09-22 08:53:58 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
common.ErrorResp(c, err, 500)
|
|
|
|
return
|
|
|
|
}
|
2023-12-03 06:44:20 +00:00
|
|
|
if t == nil {
|
|
|
|
common.SuccessResp(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
common.SuccessResp(c, gin.H{
|
|
|
|
"task": getTaskInfo(t),
|
|
|
|
})
|
2022-09-22 08:53:58 +00:00
|
|
|
}
|