alist/server/controllers/file/upload.go

67 lines
1.5 KiB
Go
Raw Normal View History

2022-01-08 14:09:27 +00:00
package file
2021-12-31 12:33:39 +00:00
import (
"errors"
"github.com/Xhofe/alist/conf"
2022-01-08 13:29:00 +00:00
"github.com/Xhofe/alist/drivers/base"
2022-01-03 12:06:36 +00:00
"github.com/Xhofe/alist/drivers/operate"
2021-12-31 12:33:39 +00:00
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
)
2022-01-08 14:09:27 +00:00
func UploadFiles(c *gin.Context) {
2021-12-31 12:33:39 +00:00
path := c.PostForm("path")
path = utils.ParsePath(path)
token := c.GetHeader("Authorization")
if token != conf.Token {
password := c.PostForm("password")
meta, _ := model.GetMetaByPath(path)
if meta == nil || !meta.Upload {
common.ErrorResp(c, errors.New("not allow upload"), 403)
return
}
if meta.Password != "" && meta.Password != password {
common.ErrorResp(c, errors.New("wrong password"), 403)
return
}
}
account, path_, driver, err := common.ParsePath(path)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
2022-01-08 13:29:00 +00:00
form, err := c.MultipartForm()
if err != nil {
common.ErrorResp(c, err, 400)
2021-12-31 12:33:39 +00:00
}
2022-01-08 13:29:00 +00:00
files := form.File["files"]
2021-12-31 12:33:39 +00:00
if err != nil {
return
}
2022-01-08 13:29:00 +00:00
for i, file := range files {
open, err := file.Open()
fileStream := model.FileStream{
File: open,
Size: uint64(file.Size),
ParentPath: path_,
Name: file.Filename,
MIMEType: file.Header.Get("Content-Type"),
}
clearCache := false
if i == len(files)-1 {
clearCache = true
}
err = operate.Upload(driver, account, &fileStream, clearCache)
if err != nil {
2022-01-11 15:45:12 +00:00
if i != 0 {
_ = base.DeleteCache(path_, account)
}
2022-01-08 13:29:00 +00:00
common.ErrorResp(c, err, 500)
return
}
}
2021-12-31 12:33:39 +00:00
common.SuccessResp(c)
}