web upload

pull/548/head
微凉 2021-12-31 20:33:39 +08:00
parent cc217df924
commit 103e049f22
5 changed files with 96 additions and 16 deletions

View File

@ -10,6 +10,7 @@ type Meta struct {
Path string `json:"path" gorm:"unique" binding:"required"` Path string `json:"path" gorm:"unique" binding:"required"`
Password string `json:"password"` Password string `json:"password"`
Hide string `json:"hide"` Hide string `json:"hide"`
Upload bool `json:"upload"`
} }
func GetMetaByPath(path string) (*Meta, error) { func GetMetaByPath(path string) (*Meta, error) {

View File

@ -1,6 +1,7 @@
package common package common
import ( import (
"errors"
"fmt" "fmt"
"github.com/Xhofe/alist/drivers/base" "github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/model" "github.com/Xhofe/alist/model"
@ -29,6 +30,9 @@ func ParsePath(rawPath string) (*model.Account, string, base.Driver, error) {
path = rawPath path = rawPath
break break
default: default:
if path == "/" {
return nil, "", nil, errors.New("can't operate root of multiple accounts")
}
paths := strings.Split(rawPath, "/") paths := strings.Split(rawPath, "/")
path = "/" + strings.Join(paths[2:], "/") path = "/" + strings.Join(paths[2:], "/")
name = paths[1] name = paths[1]

View File

@ -0,0 +1,57 @@
package controllers
import (
"errors"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
)
func UploadFile(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
}
}
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)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
fileStream := model.FileStream{
File: open,
Size: uint64(file.Size),
ParentPath: path_,
Name: file.Filename,
MIMEType: c.GetHeader("Content-Type"),
}
err = driver.Upload(&fileStream, account)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c)
}

View File

@ -12,8 +12,8 @@ import (
"strings" "strings"
) )
func Hide(files []model.File, path string) []model.File { func Hide(meta *model.Meta, files []model.File, path string) []model.File {
meta, _ := model.GetMetaByPath(path) //meta, _ := model.GetMetaByPath(path)
if meta != nil && meta.Hide != "" { if meta != nil && meta.Hide != "" {
tmpFiles := make([]model.File, 0) tmpFiles := make([]model.File, 0)
hideFiles := strings.Split(meta.Hide, ",") hideFiles := strings.Split(meta.Hide, ",")
@ -27,29 +27,41 @@ func Hide(files []model.File, path string) []model.File {
return files return files
} }
type Meta struct {
Driver string `json:"driver"`
Upload bool `json:"upload"`
}
type PathResp struct { type PathResp struct {
Type string `json:"type"` Type string `json:"type"`
Driver string `json:"driver"` Meta Meta `json:"meta"`
Files []model.File `json:"files"` Files []model.File `json:"files"`
} }
func Path(c *gin.Context) { func Path(c *gin.Context) {
reqV, _ := c.Get("req") reqV, _ := c.Get("req")
req := reqV.(common.PathReq) req := reqV.(common.PathReq)
meta, _ := model.GetMetaByPath(req.Path)
upload := false
if meta != nil && meta.Upload {
upload = true
}
if model.AccountsCount() > 1 && req.Path == "/" { if model.AccountsCount() > 1 && req.Path == "/" {
files, err := model.GetAccountFiles() files, err := model.GetAccountFiles()
if err != nil { if err != nil {
common.ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return return
} }
files = Hide(files, req.Path) files = Hide(meta, files, req.Path)
c.JSON(200, common.Resp{ c.JSON(200, common.Resp{
Code: 200, Code: 200,
Message: "success", Message: "success",
Data: PathResp{ Data: PathResp{
Type: "folder", Type: "folder",
Driver: "root", Meta: Meta{
Files: files, Driver: "root",
},
Files: files,
}, },
}) })
return return
@ -84,13 +96,15 @@ func Path(c *gin.Context) {
Code: 200, Code: 200,
Message: "success", Message: "success",
Data: PathResp{ Data: PathResp{
Type: "file", Type: "file",
Driver: driver.Config().Name, Meta: Meta{
Files: []model.File{*file}, Driver: driver.Config().Name,
},
Files: []model.File{*file},
}, },
}) })
} else { } else {
files = Hide(files, req.Path) files = Hide(meta, files, req.Path)
if driver.Config().LocalSort { if driver.Config().LocalSort {
model.SortFiles(files, account) model.SortFiles(files, account)
} }
@ -98,9 +112,12 @@ func Path(c *gin.Context) {
Code: 200, Code: 200,
Message: "success", Message: "success",
Data: PathResp{ Data: PathResp{
Type: "folder", Type: "folder",
Driver: "root", Meta: Meta{
Files: files, Driver: driver.Config().Name,
Upload: upload,
},
Files: files,
}, },
}) })
} }

View File

@ -23,6 +23,7 @@ func InitApiRouter(r *gin.Engine) {
path.POST("/preview", controllers.Preview) path.POST("/preview", controllers.Preview)
//path.POST("/link",middlewares.Auth, controllers.Link) //path.POST("/link",middlewares.Auth, controllers.Link)
public.POST("/upload", controllers.UploadFile)
public.GET("/settings", controllers.GetSettingsPublic) public.GET("/settings", controllers.GetSettingsPublic)
} }