后端重构
This commit is contained in:
@@ -14,6 +14,7 @@ var (
|
||||
fileUploadUsecase *example.FileUploadUsecase
|
||||
customerUsecase *example.CustomerUsecase
|
||||
attachmentCategoryUsecase *example.AttachmentCategoryUsecase
|
||||
breakpointContinueUsecase *example.BreakpointContinueUsecase
|
||||
)
|
||||
|
||||
// InitUsecases 初始化业务层依赖
|
||||
@@ -21,8 +22,10 @@ func InitUsecases(
|
||||
fileUpload *example.FileUploadUsecase,
|
||||
customer *example.CustomerUsecase,
|
||||
attachmentCategory *example.AttachmentCategoryUsecase,
|
||||
breakpointContinue *example.BreakpointContinueUsecase,
|
||||
) {
|
||||
fileUploadUsecase = fileUpload
|
||||
customerUsecase = customer
|
||||
attachmentCategoryUsecase = attachmentCategory
|
||||
breakpointContinueUsecase = breakpointContinue
|
||||
}
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
package example
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"kra/internal/biz/example"
|
||||
"kra/pkg/response"
|
||||
"kra/pkg/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// BreakpointContinue 断点续传到服务器
|
||||
func (f *FileUploadApi) BreakpointContinue(c *gin.Context) {
|
||||
fileMd5 := c.Request.FormValue("fileMd5")
|
||||
fileName := c.Request.FormValue("fileName")
|
||||
chunkMd5 := c.Request.FormValue("chunkMd5")
|
||||
chunkNumber, _ := strconv.Atoi(c.Request.FormValue("chunkNumber"))
|
||||
chunkTotal, _ := strconv.Atoi(c.Request.FormValue("chunkTotal"))
|
||||
|
||||
_, FileHeader, err := c.Request.FormFile("file")
|
||||
if err != nil {
|
||||
response.FailWithMessage("接收文件失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
file, err := FileHeader.Open()
|
||||
if err != nil {
|
||||
response.FailWithMessage("文件读取失败", c)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
content, _ := io.ReadAll(file)
|
||||
if !utils.CheckMd5(content, chunkMd5) {
|
||||
response.FailWithMessage("检查md5失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
exaFile, err := breakpointContinueUsecase.FindOrCreateFile(c.Request.Context(), fileMd5, fileName, chunkTotal)
|
||||
if err != nil {
|
||||
response.FailWithMessage("查找或创建记录失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
pathC, err := utils.BreakPointContinue(content, fileName, chunkNumber, chunkTotal, fileMd5)
|
||||
if err != nil {
|
||||
response.FailWithMessage("断点续传失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
if err = breakpointContinueUsecase.CreateFileChunk(c.Request.Context(), exaFile.ID, pathC, chunkNumber); err != nil {
|
||||
response.FailWithMessage("创建文件记录失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("切片创建成功", c)
|
||||
}
|
||||
|
||||
// FindFile 查找文件
|
||||
func (f *FileUploadApi) FindFile(c *gin.Context) {
|
||||
fileMd5 := c.Query("fileMd5")
|
||||
fileName := c.Query("fileName")
|
||||
chunkTotal, _ := strconv.Atoi(c.Query("chunkTotal"))
|
||||
|
||||
file, err := breakpointContinueUsecase.FindOrCreateFile(c.Request.Context(), fileMd5, fileName, chunkTotal)
|
||||
if err != nil {
|
||||
response.FailWithMessage("查找失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithDetailed(gin.H{"file": toFileResponse(file)}, "查找成功", c)
|
||||
}
|
||||
|
||||
// BreakpointContinueFinish 创建文件(合并切片)
|
||||
func (f *FileUploadApi) BreakpointContinueFinish(c *gin.Context) {
|
||||
fileMd5 := c.Query("fileMd5")
|
||||
fileName := c.Query("fileName")
|
||||
|
||||
filePath, err := utils.MakeFile(fileName, fileMd5)
|
||||
if err != nil {
|
||||
response.FailWithDetailed(gin.H{"filePath": filePath}, "文件创建失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithDetailed(gin.H{"filePath": filePath}, "文件创建成功", c)
|
||||
}
|
||||
|
||||
// RemoveChunk 删除切片
|
||||
func (f *FileUploadApi) RemoveChunk(c *gin.Context) {
|
||||
var req struct {
|
||||
FileMd5 string `json:"fileMd5"`
|
||||
FilePath string `json:"filePath"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 路径穿越拦截
|
||||
if strings.Contains(req.FilePath, "..") || strings.Contains(req.FilePath, "../") ||
|
||||
strings.Contains(req.FilePath, "./") || strings.Contains(req.FilePath, ".\\") {
|
||||
response.FailWithMessage("非法路径,禁止删除", c)
|
||||
return
|
||||
}
|
||||
|
||||
if err := utils.RemoveChunk(req.FileMd5); err != nil {
|
||||
response.FailWithMessage("缓存切片删除失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
if err := breakpointContinueUsecase.DeleteFileChunk(c.Request.Context(), req.FileMd5, req.FilePath); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("缓存切片删除成功", c)
|
||||
}
|
||||
|
||||
func toFileResponse(file *example.ExaFile) map[string]interface{} {
|
||||
chunks := make([]map[string]interface{}, len(file.ExaFileChunk))
|
||||
for i, chunk := range file.ExaFileChunk {
|
||||
chunks[i] = map[string]interface{}{
|
||||
"ID": chunk.ID,
|
||||
"exaFileID": chunk.ExaFileID,
|
||||
"fileChunkNumber": chunk.FileChunkNumber,
|
||||
"fileChunkPath": chunk.FileChunkPath,
|
||||
}
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"ID": file.ID,
|
||||
"fileName": file.FileName,
|
||||
"fileMd5": file.FileMd5,
|
||||
"filePath": file.FilePath,
|
||||
"chunkTotal": file.ChunkTotal,
|
||||
"isFinish": file.IsFinish,
|
||||
"exaFileChunk": chunks,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user