mirror of https://github.com/Xhofe/alist
🎇 Multiple file upload
parent
925f386bed
commit
2b97882b42
|
@ -51,6 +51,9 @@ func Delete(driver base.Driver, account *model.Account, path string, clearCache
|
||||||
}
|
}
|
||||||
|
|
||||||
func Upload(driver base.Driver, account *model.Account, file *model.FileStream, clearCache bool) error {
|
func Upload(driver base.Driver, account *model.Account, file *model.FileStream, clearCache bool) error {
|
||||||
|
defer func() {
|
||||||
|
_ = file.Close()
|
||||||
|
}()
|
||||||
err := driver.Upload(file, account)
|
err := driver.Upload(file, account)
|
||||||
if err == nil && clearCache {
|
if err == nil && clearCache {
|
||||||
_ = base.DeleteCache(file.ParentPath, account)
|
_ = base.DeleteCache(file.ParentPath, account)
|
||||||
|
|
|
@ -3,6 +3,7 @@ package controllers
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/Xhofe/alist/conf"
|
"github.com/Xhofe/alist/conf"
|
||||||
|
"github.com/Xhofe/alist/drivers/base"
|
||||||
"github.com/Xhofe/alist/drivers/operate"
|
"github.com/Xhofe/alist/drivers/operate"
|
||||||
"github.com/Xhofe/alist/model"
|
"github.com/Xhofe/alist/model"
|
||||||
"github.com/Xhofe/alist/server/common"
|
"github.com/Xhofe/alist/server/common"
|
||||||
|
@ -26,33 +27,38 @@ func UploadFile(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
file, err := c.FormFile("file")
|
|
||||||
if err != nil {
|
|
||||||
common.ErrorResp(c, err, 400)
|
|
||||||
}
|
|
||||||
open, err := file.Open()
|
|
||||||
defer func() {
|
|
||||||
_ = open.Close()
|
|
||||||
}()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
account, path_, driver, err := common.ParsePath(path)
|
account, path_, driver, err := common.ParsePath(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.ErrorResp(c, err, 500)
|
common.ErrorResp(c, err, 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fileStream := model.FileStream{
|
form, err := c.MultipartForm()
|
||||||
File: open,
|
if err != nil {
|
||||||
Size: uint64(file.Size),
|
common.ErrorResp(c, err, 400)
|
||||||
ParentPath: path_,
|
}
|
||||||
Name: file.Filename,
|
files := form.File["files"]
|
||||||
MIMEType: file.Header.Get("Content-Type"),
|
|
||||||
}
|
|
||||||
err = operate.Upload(driver, account, &fileStream, true)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.ErrorResp(c, err, 500)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
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 {
|
||||||
|
_ = base.DeleteCache(path_, account)
|
||||||
|
common.ErrorResp(c, err, 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
common.SuccessResp(c)
|
common.SuccessResp(c)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue