Feat: stream uploading

pull/247/head
HFO4 5 years ago
parent 5aa61301af
commit 861ba77b04

@ -1,6 +1,9 @@
package local
import "mime/multipart"
import (
"io"
"mime/multipart"
)
// FileData 上传的文件数据
type FileData struct {
@ -29,3 +32,30 @@ func (file FileData) Close() error {
func (file FileData) GetFileName() string {
return file.Name
}
type FileStream struct {
File io.ReadCloser
Size uint64
Name string
MIMEType string
}
func (file FileStream) Read(p []byte) (n int, err error) {
return file.File.Read(p)
}
func (file FileStream) GetMIMEType() string {
return file.MIMEType
}
func (file FileStream) GetSize() uint64 {
return file.Size
}
func (file FileStream) Close() error {
return file.File.Close()
}
func (file FileStream) GetFileName() string {
return file.Name
}

@ -3,9 +3,12 @@ package controllers
import (
"context"
"github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/filesystem"
"github.com/HFO4/cloudreve/pkg/filesystem/local"
"github.com/HFO4/cloudreve/pkg/serializer"
"github.com/HFO4/cloudreve/service/file"
"github.com/gin-gonic/gin"
"strconv"
)
// FileUpload 本地策略文件上传
@ -30,3 +33,44 @@ func FileUpload(c *gin.Context) {
c.JSON(200, ErrorResponse(err))
}
}
func FileUploadStream(c *gin.Context) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
fileSize, err := strconv.ParseUint(c.Request.Header.Get("Content-Length"), 10, 64)
if err != nil {
c.JSON(200, ErrorResponse(err))
return
}
fileData := local.FileStream{
MIMEType: c.Request.Header.Get("Content-Type"),
File: c.Request.Body,
Size: fileSize,
Name: c.Request.Header.Get("X-FileName"),
}
user, _ := c.Get("user")
// 创建文件系统
fs, err := filesystem.NewFileSystem(user.(*model.User))
if err != nil {
c.JSON(200, serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err))
return
}
// 给文件系统分配钩子
fs.BeforeUpload = filesystem.GenericBeforeUpload
// 执行上传
err = fs.Upload(ctx, fileData)
if err != nil {
c.JSON(200, serializer.Err(serializer.CodeUploadFailed, err.Error(), err))
return
}
c.JSON(200, serializer.Response{
Code: 0,
Msg: "Pong",
})
}

@ -49,8 +49,10 @@ func InitRouter() *gin.Engine {
// 文件
file := auth.Group("File")
{
// 当前登录用户信息
// 小文件上传
file.POST("Upload", controllers.FileUpload)
// 小文件上传
file.POST("UploadStream", controllers.FileUploadStream)
}
}

Loading…
Cancel
Save