mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Feat: API for receiviing chunk data
This commit is contained in:
@@ -3,15 +3,10 @@ package controllers
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/request"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"sync"
|
||||
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/conf"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
|
||||
"github.com/cloudreve/Cloudreve/v3/service/explorer"
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -285,72 +280,65 @@ func PutContent(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// FileUploadStream 本地策略流式上传
|
||||
func FileUploadStream(c *gin.Context) {
|
||||
// FileUpload 本地策略文件上传
|
||||
func FileUpload(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 {
|
||||
var service explorer.UploadService
|
||||
if err := c.ShouldBindUri(&service); err == nil {
|
||||
res := service.Upload(ctx, c)
|
||||
c.JSON(200, res)
|
||||
} else {
|
||||
c.JSON(200, ErrorResponse(err))
|
||||
return
|
||||
}
|
||||
|
||||
// 解码文件名和路径
|
||||
fileName, err := url.QueryUnescape(c.Request.Header.Get("X-Cr-FileName"))
|
||||
filePath, err := url.QueryUnescape(c.Request.Header.Get("X-Cr-Path"))
|
||||
if err != nil {
|
||||
c.JSON(200, ErrorResponse(err))
|
||||
return
|
||||
}
|
||||
|
||||
fileData := fsctx.FileStream{
|
||||
MIMEType: c.Request.Header.Get("Content-Type"),
|
||||
File: c.Request.Body,
|
||||
Size: fileSize,
|
||||
Name: fileName,
|
||||
VirtualPath: filePath,
|
||||
Mode: fsctx.Create,
|
||||
}
|
||||
|
||||
// 创建文件系统
|
||||
fs, err := filesystem.NewFileSystemFromContext(c)
|
||||
if err != nil {
|
||||
c.JSON(200, serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err))
|
||||
return
|
||||
}
|
||||
|
||||
// 非可用策略时拒绝上传
|
||||
if !fs.Policy.IsTransitUpload(fileSize) {
|
||||
request.BlackHole(c.Request.Body)
|
||||
c.JSON(200, serializer.Err(serializer.CodePolicyNotAllowed, "当前存储策略无法使用", nil))
|
||||
return
|
||||
}
|
||||
|
||||
// 给文件系统分配钩子
|
||||
fs.Use("BeforeUpload", filesystem.HookValidateFile)
|
||||
fs.Use("BeforeUpload", filesystem.HookValidateCapacity)
|
||||
fs.Use("AfterUploadCanceled", filesystem.HookDeleteTempFile)
|
||||
fs.Use("AfterUploadCanceled", filesystem.HookGiveBackCapacity)
|
||||
fs.Use("AfterUpload", filesystem.GenericAfterUpload)
|
||||
fs.Use("AfterValidateFailed", filesystem.HookDeleteTempFile)
|
||||
fs.Use("AfterValidateFailed", filesystem.HookGiveBackCapacity)
|
||||
fs.Use("AfterUploadFailed", filesystem.HookGiveBackCapacity)
|
||||
|
||||
// 执行上传
|
||||
ctx = context.WithValue(ctx, fsctx.ValidateCapacityOnceCtx, &sync.Once{})
|
||||
uploadCtx := context.WithValue(ctx, fsctx.GinCtx, c)
|
||||
err = fs.Upload(uploadCtx, &fileData)
|
||||
if err != nil {
|
||||
c.JSON(200, serializer.Err(serializer.CodeUploadFailed, err.Error(), err))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, serializer.Response{
|
||||
Code: 0,
|
||||
})
|
||||
//fileData := fsctx.FileStream{
|
||||
// MIMEType: c.Request.Header.Get("Content-Type"),
|
||||
// File: c.Request.Body,
|
||||
// Size: fileSize,
|
||||
// Name: fileName,
|
||||
// VirtualPath: filePath,
|
||||
// Mode: fsctx.Create,
|
||||
//}
|
||||
//
|
||||
//// 创建文件系统
|
||||
//fs, err := filesystem.NewFileSystemFromContext(c)
|
||||
//if err != nil {
|
||||
// c.JSON(200, serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err))
|
||||
// return
|
||||
//}
|
||||
//
|
||||
//// 非可用策略时拒绝上传
|
||||
//if !fs.Policy.IsTransitUpload(fileSize) {
|
||||
// request.BlackHole(c.Request.Body)
|
||||
// c.JSON(200, serializer.Err(serializer.CodePolicyNotAllowed, "当前存储策略无法使用", nil))
|
||||
// return
|
||||
//}
|
||||
//
|
||||
//// 给文件系统分配钩子
|
||||
//fs.Use("BeforeUpload", filesystem.HookValidateFile)
|
||||
//fs.Use("BeforeUpload", filesystem.HookValidateCapacity)
|
||||
//fs.Use("AfterUploadCanceled", filesystem.HookDeleteTempFile)
|
||||
//fs.Use("AfterUploadCanceled", filesystem.HookGiveBackCapacity)
|
||||
//fs.Use("AfterUpload", filesystem.GenericAfterUpload)
|
||||
//fs.Use("AfterValidateFailed", filesystem.HookDeleteTempFile)
|
||||
//fs.Use("AfterValidateFailed", filesystem.HookGiveBackCapacity)
|
||||
//fs.Use("AfterUploadFailed", filesystem.HookGiveBackCapacity)
|
||||
//
|
||||
//// 执行上传
|
||||
//ctx = context.WithValue(ctx, fsctx.ValidateCapacityOnceCtx, &sync.Once{})
|
||||
//uploadCtx := context.WithValue(ctx, fsctx.GinCtx, c)
|
||||
//err = fs.Upload(uploadCtx, &fileData)
|
||||
//if err != nil {
|
||||
// c.JSON(200, serializer.Err(serializer.CodeUploadFailed, err.Error(), err))
|
||||
// return
|
||||
//}
|
||||
//
|
||||
//c.JSON(200, serializer.Response{
|
||||
// Code: 0,
|
||||
//})
|
||||
}
|
||||
|
||||
// GetUploadCredential 创建上传会话
|
||||
|
||||
Reference in New Issue
Block a user