File size: 3,218 Bytes
7107f0b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
package handles
import (
"io"
"net/url"
stdpath "path"
"strconv"
"time"
"github.com/alist-org/alist/v3/pkg/tache"
"github.com/alist-org/alist/v3/internal/stream"
"github.com/alist-org/alist/v3/internal/fs"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin"
)
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
}
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"
user := c.MustGet("user").(*model.User)
path, err = user.JoinPath(path)
if err != nil {
common.ErrorResp(c, err, 403)
return
}
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
}
s := &stream.FileStream{
Obj: &model.Object{
Name: name,
Size: size,
Modified: getLastModified(c),
},
Reader: c.Request.Body,
Mimetype: c.GetHeader("Content-Type"),
WebPutAsTask: asTask,
}
var t tache.TaskWithInfo
if asTask {
t, err = fs.PutAsTask(dir, s)
} else {
err = fs.PutDirectly(c, dir, s, true)
}
defer c.Request.Body.Close()
if err != nil {
common.ErrorResp(c, err, 500)
return
}
if t == nil {
common.SuccessResp(c)
return
}
common.SuccessResp(c, gin.H{
"task": getTaskInfo(t),
})
}
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"
user := c.MustGet("user").(*model.User)
path, err = user.JoinPath(path)
if err != nil {
common.ErrorResp(c, err, 403)
return
}
storage, err := fs.GetStorage(path, &fs.GetStoragesArgs{})
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
}
f, err := file.Open()
if err != nil {
common.ErrorResp(c, err, 500)
return
}
defer f.Close()
dir, name := stdpath.Split(path)
s := stream.FileStream{
Obj: &model.Object{
Name: name,
Size: file.Size,
Modified: getLastModified(c),
},
Reader: f,
Mimetype: file.Header.Get("Content-Type"),
WebPutAsTask: asTask,
}
var t tache.TaskWithInfo
if asTask {
s.Reader = struct {
io.Reader
}{f}
t, err = fs.PutAsTask(dir, &s)
} else {
ss, err := stream.NewSeekableStream(s, nil)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
err = fs.PutDirectly(c, dir, ss, true)
}
if err != nil {
common.ErrorResp(c, err, 500)
return
}
if t == nil {
common.SuccessResp(c)
return
}
common.SuccessResp(c, gin.H{
"task": getTaskInfo(t),
})
}
|