🎇 file delete
This commit is contained in:
45
server/controllers/file/delete.go
Normal file
45
server/controllers/file/delete.go
Normal file
@ -0,0 +1,45 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/drivers/operate"
|
||||
"github.com/Xhofe/alist/server/common"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type DeleteFilesReq struct {
|
||||
Path string `json:"path"`
|
||||
Names []string `json:"names"`
|
||||
}
|
||||
|
||||
func DeleteFiles(c *gin.Context) {
|
||||
var req DeleteFilesReq
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
common.ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
if len(req.Names) == 0 {
|
||||
common.ErrorResp(c, errors.New("empty file names"), 400)
|
||||
return
|
||||
}
|
||||
for i, name := range req.Names {
|
||||
account, path_, driver, err := common.ParsePath(utils.Join(req.Path, name))
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
clearCache := false
|
||||
if i == len(req.Names)-1 {
|
||||
clearCache = true
|
||||
}
|
||||
err = operate.Delete(driver, account, path_, clearCache)
|
||||
if err != nil {
|
||||
_ = base.DeleteCache(utils.Dir(path_), account)
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
}
|
||||
common.SuccessResp(c)
|
||||
}
|
64
server/controllers/file/upload.go
Normal file
64
server/controllers/file/upload.go
Normal file
@ -0,0 +1,64 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/drivers/operate"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/server/common"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func UploadFiles(c *gin.Context) {
|
||||
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
|
||||
}
|
||||
form, err := c.MultipartForm()
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 400)
|
||||
}
|
||||
files := form.File["files"]
|
||||
if err != nil {
|
||||
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)
|
||||
}
|
Reference in New Issue
Block a user