Add: filesystem

pull/247/head
HFO4 5 years ago
parent d86a3533ca
commit 003274162b

@ -0,0 +1,28 @@
package filesystem
import (
"cloudreve/models"
"io"
)
// FileData 上传来的文件数据处理器
type FileData interface {
io.Reader
io.Closer
GetSize() uint64
GetMIMEType() string
}
// FileSystem 管理文件的文件系统
type FileSystem struct {
// 文件系统所有者
User *model.User
// 文件系统处理适配器
}
// Upload 上传文件
func (fs *FileSystem) Upload(File FileData) (err error) {
return nil
}

@ -0,0 +1,26 @@
package local
import "mime/multipart"
// FileData 上传的文件数据
type FileData struct {
File multipart.File
Size uint64
MIMEType string
}
func (file FileData) Read(p []byte) (n int, err error) {
return file.Read(p)
}
func (file FileData) GetMIMEType() string {
return file.MIMEType
}
func (file FileData) GetSize() uint64 {
return file.Size
}
func (file FileData) Close() error {
return file.Close()
}

@ -3,6 +3,7 @@ package controllers
import (
"cloudreve/models"
"cloudreve/pkg/serializer"
"cloudreve/service/file"
"github.com/gin-gonic/gin"
)
@ -15,18 +16,12 @@ func FileUpload(c *gin.Context) {
return
}
name := c.PostForm("name")
path := c.PostForm("path")
var service file.UploadService
// Source
_, err := c.FormFile("file")
if err != nil {
c.JSON(200, serializer.Err(serializer.CodeIOFailed, "无法获取文件数据", err))
return
if err := c.ShouldBind(&service); err == nil {
res := service.Upload(c)
c.JSON(200, res)
} else {
c.JSON(200, ErrorResponse(err))
}
c.JSON(200, serializer.Response{
Code: 0,
Msg: name + path,
})
}

@ -14,6 +14,7 @@ func ParamErrorMsg(filed string, tag string) string {
fieldMap := map[string]string{
"UserName": "邮箱",
"Password": "密码",
"Path": "路径",
}
// 未通过的规则与中文对应
tagMap := map[string]string{

@ -1 +1,44 @@
package file
import (
"cloudreve/models"
"cloudreve/pkg/filesystem"
"cloudreve/pkg/filesystem/local"
"cloudreve/pkg/serializer"
"github.com/gin-gonic/gin"
"mime/multipart"
)
// UploadService 本地策略文件上传服务
type UploadService struct {
Name string `form:"name" binding:"required,lte=255"`
Path string `form:"path" binding:"lte=65536"`
File *multipart.FileHeader `form:"file" binding:"required"`
}
// Upload 处理本地策略小文件上传
func (service *UploadService) Upload(c *gin.Context) serializer.Response {
// TODO 检查文件大小是否小于分片最大大小
file, err := service.File.Open()
if err != nil {
return serializer.Err(serializer.CodeIOFailed, "无法打开上传数据", err)
}
fileData := local.FileData{
MIMEType: service.File.Header.Get("Content-Type"),
File: file,
Size: uint64(service.File.Size),
}
user, _ := c.Get("user")
fs := filesystem.FileSystem{
User: user.(*model.User),
}
err = fs.Upload(fileData)
return serializer.Response{
Code: 0,
Msg: "Pong",
}
}

Loading…
Cancel
Save