后端重构
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,
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,9 @@ type ApiGroup struct {
|
||||
DBApi
|
||||
AutoCodeApi
|
||||
AutoCodeHistoryApi
|
||||
AutoCodePackageApi
|
||||
AutoCodeTemplateApi
|
||||
AutoCodePluginApi
|
||||
}
|
||||
|
||||
// 业务层依赖
|
||||
@@ -42,6 +45,7 @@ var (
|
||||
exportTemplateUsecase *system.ExportTemplateUsecase
|
||||
autoCodeUsecase *system.AutoCodeUsecase
|
||||
autoCodeHistoryUsecase *system.AutoCodeHistoryUsecase
|
||||
autoCodePackageUsecase *system.AutoCodePackageUsecase
|
||||
)
|
||||
|
||||
// InitUsecases 初始化业务层依赖
|
||||
@@ -62,6 +66,7 @@ func InitUsecases(
|
||||
exportTemplate *system.ExportTemplateUsecase,
|
||||
autoCode *system.AutoCodeUsecase,
|
||||
autoCodeHistory *system.AutoCodeHistoryUsecase,
|
||||
autoCodePackage *system.AutoCodePackageUsecase,
|
||||
) {
|
||||
userUsecase = user
|
||||
apiUsecase = api
|
||||
@@ -79,4 +84,10 @@ func InitUsecases(
|
||||
exportTemplateUsecase = exportTemplate
|
||||
autoCodeUsecase = autoCode
|
||||
autoCodeHistoryUsecase = autoCodeHistory
|
||||
autoCodePackageUsecase = autoCodePackage
|
||||
}
|
||||
|
||||
// Api类型定义
|
||||
type AutoCodePackageApi struct{}
|
||||
type AutoCodeTemplateApi struct{}
|
||||
type AutoCodePluginApi struct{}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"kra/pkg/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// GetPackages 获取所有包
|
||||
// @Tags AutoCodePackage
|
||||
// @Summary 获取所有包
|
||||
// @Security ApiKeyAuth
|
||||
// @Produce application/json
|
||||
// @Success 200 {object} response.Response{data=[]system.SysAutoCodePackage} "获取成功"
|
||||
// @Router /autoCode/getPackage [post]
|
||||
func GetPackages(c *gin.Context) {
|
||||
pkgs, err := autoCodePackageUsecase.All(c.Request.Context())
|
||||
if err != nil {
|
||||
response.FailWithMessage("获取失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
response.OkWithDetailed(pkgs, "获取成功", c)
|
||||
}
|
||||
|
||||
// DeletePackage 删除包
|
||||
// @Tags AutoCodePackage
|
||||
// @Summary 删除包
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body request.GetById true "删除包"
|
||||
// @Success 200 {object} response.Response{msg=string} "删除成功"
|
||||
// @Router /autoCode/delPackage [post]
|
||||
func DeletePackage(c *gin.Context) {
|
||||
var req struct {
|
||||
ID uint `json:"id"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
if err := autoCodePackageUsecase.Delete(c.Request.Context(), req.ID); err != nil {
|
||||
response.FailWithMessage("删除失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
response.OkWithMessage("删除成功", c)
|
||||
}
|
||||
|
||||
// GetTemplates 获取所有模版
|
||||
// @Tags AutoCodePackage
|
||||
// @Summary 获取所有模版
|
||||
// @Security ApiKeyAuth
|
||||
// @Produce application/json
|
||||
// @Success 200 {object} response.Response{data=[]string} "获取成功"
|
||||
// @Router /autoCode/getTemplates [get]
|
||||
func GetTemplates(c *gin.Context) {
|
||||
// 返回固定的模版列表
|
||||
templates := []string{"package", "plugin"}
|
||||
response.OkWithDetailed(templates, "获取成功", c)
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"kra/pkg/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// InstallPlugin 安装插件
|
||||
// @Tags AutoCodePlugin
|
||||
// @Summary 安装插件
|
||||
// @Security ApiKeyAuth
|
||||
// @accept multipart/form-data
|
||||
// @Produce application/json
|
||||
// @Param plug formData file true "插件文件"
|
||||
// @Success 200 {object} response.Response{data=[]interface{}} "安装成功"
|
||||
// @Router /autoCode/installPlugin [post]
|
||||
func InstallPlugin(c *gin.Context) {
|
||||
// TODO: 实现插件安装功能
|
||||
response.FailWithMessage("功能开发中", c)
|
||||
}
|
||||
|
||||
// PackagedPlugin 打包插件
|
||||
// @Tags AutoCodePlugin
|
||||
// @Summary 打包插件
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param plugName query string true "插件名称"
|
||||
// @Success 200 {object} response.Response{msg=string} "打包成功"
|
||||
// @Router /autoCode/pubPlug [post]
|
||||
func PackagedPlugin(c *gin.Context) {
|
||||
// TODO: 实现插件打包功能
|
||||
response.FailWithMessage("功能开发中", c)
|
||||
}
|
||||
|
||||
// InitPluginMenu 初始化插件菜单
|
||||
// @Tags AutoCodePlugin
|
||||
// @Summary 初始化插件菜单
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body map[string]interface{} true "菜单信息"
|
||||
// @Success 200 {object} response.Response{msg=string} "初始化成功"
|
||||
// @Router /autoCode/initMenu [post]
|
||||
func InitPluginMenu(c *gin.Context) {
|
||||
// TODO: 实现插件菜单初始化功能
|
||||
response.FailWithMessage("功能开发中", c)
|
||||
}
|
||||
|
||||
// InitPluginAPI 初始化插件API
|
||||
// @Tags AutoCodePlugin
|
||||
// @Summary 初始化插件API
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body map[string]interface{} true "API信息"
|
||||
// @Success 200 {object} response.Response{msg=string} "初始化成功"
|
||||
// @Router /autoCode/initAPI [post]
|
||||
func InitPluginAPI(c *gin.Context) {
|
||||
// TODO: 实现插件API初始化功能
|
||||
response.FailWithMessage("功能开发中", c)
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"kra/pkg/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// PreviewCode 预览代码
|
||||
// @Tags AutoCodeTemplate
|
||||
// @Summary 预览创建后的代码
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body map[string]interface{} true "预览创建代码"
|
||||
// @Success 200 {object} response.Response{data=map[string]interface{}} "预览成功"
|
||||
// @Router /autoCode/preview [post]
|
||||
func PreviewCode(c *gin.Context) {
|
||||
// TODO: 实现代码预览功能
|
||||
response.FailWithMessage("功能开发中", c)
|
||||
}
|
||||
|
||||
// CreateCode 创建代码
|
||||
// @Tags AutoCodeTemplate
|
||||
// @Summary 自动代码模板
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body map[string]interface{} true "创建自动代码"
|
||||
// @Success 200 {object} response.Response{msg=string} "创建成功"
|
||||
// @Router /autoCode/createTemp [post]
|
||||
func CreateCode(c *gin.Context) {
|
||||
// TODO: 实现代码创建功能
|
||||
response.FailWithMessage("功能开发中", c)
|
||||
}
|
||||
|
||||
// AddFunc 增加方法
|
||||
// @Tags AutoCodeTemplate
|
||||
// @Summary 增加方法
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body map[string]interface{} true "增加方法"
|
||||
// @Success 200 {object} response.Response{msg=string} "注入成功"
|
||||
// @Router /autoCode/addFunc [post]
|
||||
func AddFunc(c *gin.Context) {
|
||||
// TODO: 实现方法注入功能
|
||||
response.FailWithMessage("功能开发中", c)
|
||||
}
|
||||
Reference in New Issue
Block a user