Cloudreve/routers/controllers/slave.go

82 lines
2.1 KiB
Go
Raw Normal View History

package controllers
import (
2019-12-27 08:15:05 -05:00
"context"
"github.com/HFO4/cloudreve/pkg/filesystem"
"github.com/HFO4/cloudreve/pkg/filesystem/fsctx"
"github.com/HFO4/cloudreve/pkg/filesystem/local"
"github.com/HFO4/cloudreve/pkg/serializer"
"github.com/gin-gonic/gin"
2019-12-27 08:15:05 -05:00
"net/url"
"strconv"
)
// SlaveUpload 从机文件上传
func SlaveUpload(c *gin.Context) {
2019-12-27 08:15:05 -05:00
// 创建上下文
ctx, cancel := context.WithCancel(context.Background())
ctx = context.WithValue(ctx, fsctx.GinCtx, c)
defer cancel()
// 创建匿名文件系统
fs, err := filesystem.NewAnonymousFileSystem()
if err != nil {
c.JSON(200, serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err))
return
}
2019-12-28 00:14:00 -05:00
fs.Handler = local.Handler{}
2019-12-27 08:15:05 -05:00
// 从请求中取得上传策略
uploadPolicyRaw := c.GetHeader("X-Policy")
if uploadPolicyRaw == "" {
c.JSON(200, serializer.ParamErr("未指定上传策略", nil))
2019-12-28 00:14:00 -05:00
return
2019-12-27 08:15:05 -05:00
}
// 解析上传策略
uploadPolicy, err := serializer.DecodeUploadPolicy(uploadPolicyRaw)
if err != nil {
c.JSON(200, serializer.ParamErr("上传策略格式有误", err))
2019-12-28 00:14:00 -05:00
return
2019-12-27 08:15:05 -05:00
}
2019-12-28 00:14:00 -05:00
ctx = context.WithValue(ctx, fsctx.UploadPolicyCtx, *uploadPolicy)
2019-12-27 08:15:05 -05:00
// 取得文件大小
fileSize, err := strconv.ParseUint(c.Request.Header.Get("Content-Length"), 10, 64)
if err != nil {
c.JSON(200, ErrorResponse(err))
return
}
// 解码文件名和路径
fileName, err := url.QueryUnescape(c.Request.Header.Get("X-FileName"))
if err != nil {
c.JSON(200, ErrorResponse(err))
return
}
fileData := local.FileStream{
MIMEType: c.Request.Header.Get("Content-Type"),
File: c.Request.Body,
Name: fileName,
Size: fileSize,
}
// 给文件系统分配钩子
fs.Use("BeforeUpload", filesystem.HookSlaveUploadValidate)
fs.Use("AfterUploadCanceled", filesystem.HookDeleteTempFile)
2019-12-28 00:14:00 -05:00
fs.Use("AfterUpload", filesystem.SlaveAfterUpload)
2019-12-27 08:15:05 -05:00
fs.Use("AfterValidateFailed", filesystem.HookDeleteTempFile)
// 执行上传
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,
})
}