From 5c45a9b08e1f4e55083c48f506d20284167b57d1 Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Wed, 26 Feb 2025 17:55:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=96=87=E4=BB=B6/=E9=85=8D=E7=BD=AE/?= =?UTF-8?q?=E8=8F=9C=E5=8D=95=E6=9D=83=E9=99=90=E6=8E=A5=E5=8F=A3=E5=8F=98?= =?UTF-8?q?=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/common/common.go | 10 +-- src/modules/common/controller/file.go | 87 +++++++++++---------- src/modules/system/controller/sys_config.go | 2 +- src/modules/system/system.go | 4 +- 4 files changed, 52 insertions(+), 51 deletions(-) diff --git a/src/modules/common/common.go b/src/modules/common/common.go index a8d937a3..4b43a267 100644 --- a/src/modules/common/common.go +++ b/src/modules/common/common.go @@ -89,11 +89,11 @@ func Setup(router *gin.Engine) { // 文件操作处理 fileGroup := router.Group("/file") { - fileGroup.GET("/download/:filePath", middleware.PreAuthorize(nil), controller.NewFile.Download) fileGroup.POST("/upload", middleware.PreAuthorize(nil), controller.NewFile.Upload) - fileGroup.POST("/chunkCheck", middleware.PreAuthorize(nil), controller.NewFile.ChunkCheck) - fileGroup.POST("/chunkUpload", middleware.PreAuthorize(nil), controller.NewFile.ChunkUpload) - fileGroup.POST("/chunkMerge", middleware.PreAuthorize(nil), controller.NewFile.ChunkMerge) - fileGroup.POST("/transferStaticFile", middleware.PreAuthorize(nil), controller.NewFile.TransferStaticFile) + fileGroup.POST("/chunk-check", middleware.PreAuthorize(nil), controller.NewFile.ChunkCheck) + fileGroup.POST("/chunk-upload", middleware.PreAuthorize(nil), controller.NewFile.ChunkUpload) + fileGroup.POST("/chunk-merge", middleware.PreAuthorize(nil), controller.NewFile.ChunkMerge) + fileGroup.GET("/download/:filePath", middleware.PreAuthorize(nil), controller.NewFile.Download) + fileGroup.POST("/transfer-static-file", middleware.PreAuthorize(nil), controller.NewFile.TransferStaticFile) } } diff --git a/src/modules/common/controller/file.go b/src/modules/common/controller/file.go index eb919334..e7e46746 100644 --- a/src/modules/common/controller/file.go +++ b/src/modules/common/controller/file.go @@ -82,39 +82,41 @@ func (s *FileController) Download(c *gin.Context) { // @Description Upload a file, interface param use // @Router /file/upload [post] func (s *FileController) Upload(c *gin.Context) { - language := reqctx.AcceptLanguage(c) // 上传的文件 formFile, err := c.FormFile("file") if err != nil { - c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400"))) + c.JSON(400, resp.CodeMsg(40010, "bind err: file is empty")) return } - // 子路径 + // 子路径需要在指定范围内 subPath := c.PostForm("subPath") - if _, ok := constants.UPLOAD_SUB_PATH[subPath]; !ok { - c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400"))) + _, ok := constants.UPLOAD_SUB_PATH[subPath] + if subPath != "" && !ok { + c.JSON(400, resp.CodeMsg(40010, "bind err: subPath not in range")) return } + if subPath == "" { + subPath = constants.UPLOAD_COMMON + } // 上传文件转存 - upFilePath, err := file.TransferUploadFile(formFile, subPath, nil) + uploadFilePath, err := file.TransferUploadFile(formFile, subPath, []string{}) if err != nil { c.JSON(200, resp.ErrMsg(err.Error())) return } - newFileName := upFilePath[strings.LastIndex(upFilePath, "/")+1:] c.JSON(200, resp.OkData(map[string]string{ - "url": "//" + c.Request.Host + upFilePath, - "fileName": upFilePath, - "newFileName": newFileName, + "url": "//" + c.Request.Host + uploadFilePath, + "filePath": uploadFilePath, + "newFileName": filepath.Base(uploadFilePath), "originalFileName": formFile.Filename, })) } // 切片文件检查 // -// POST /chunkCheck +// POST /chunk-check // // @Tags common/file // @Accept json @@ -124,18 +126,15 @@ func (s *FileController) Upload(c *gin.Context) { // @Security TokenAuth // @Summary Slice file checking // @Description Slice file checking -// @Router /file/chunkCheck [post] +// @Router /file/chunk-check [post] func (s *FileController) ChunkCheck(c *gin.Context) { - language := reqctx.AcceptLanguage(c) var body struct { - // 唯一标识 - Identifier string `json:"identifier" binding:"required"` - // 文件名 - FileName string `json:"fileName" binding:"required"` + Identifier string `json:"identifier" binding:"required"` // 唯一标识 + FileName string `json:"fileName" binding:"required"` // 文件名 } - err := c.ShouldBindJSON(&body) - if err != nil { - c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400"))) + if err := c.ShouldBindJSON(&body); err != nil { + errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err)) + c.JSON(422, resp.CodeMsg(40422, errMsgs)) return } @@ -150,7 +149,7 @@ func (s *FileController) ChunkCheck(c *gin.Context) { // 切片文件合并 // -// POST /chunkMerge +// POST /chunk-merge // // @Tags common/file // @Accept json @@ -160,26 +159,26 @@ func (s *FileController) ChunkCheck(c *gin.Context) { // @Security TokenAuth // @Summary Slice file merge // @Description Slice file merge -// @Router /file/chunkMerge [post] +// @Router /file/chunk-merge [post] func (s *FileController) ChunkMerge(c *gin.Context) { - language := reqctx.AcceptLanguage(c) var body struct { - // 唯一标识 - Identifier string `json:"identifier" binding:"required"` - // 文件名 - FileName string `json:"fileName" binding:"required"` - // 子路径类型 - SubPath string `json:"subPath" binding:"required"` + Identifier string `json:"identifier" binding:"required"` // 唯一标识 + FileName string `json:"fileName" binding:"required"` // 文件名 + SubPath string `json:"subPath"` // 子路径类型 } - err := c.ShouldBindJSON(&body) - if err != nil { - c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400"))) + if err := c.ShouldBindJSON(&body); err != nil { + errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err)) + c.JSON(422, resp.CodeMsg(40422, errMsgs)) return } - if _, ok := constants.UPLOAD_SUB_PATH[body.SubPath]; !ok { - c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400"))) + // 子路径需要在指定范围内 + if _, ok := constants.UPLOAD_SUB_PATH[body.SubPath]; body.SubPath != "" && !ok { + c.JSON(400, resp.CodeMsg(40010, "bind err: subPath not in range")) return } + if body.SubPath == "" { + body.SubPath = constants.UPLOAD_COMMON + } // 切片文件合并 mergeFilePath, err := file.ChunkMergeFile(body.Identifier, body.FileName, body.SubPath) @@ -188,18 +187,17 @@ func (s *FileController) ChunkMerge(c *gin.Context) { return } - newFileName := mergeFilePath[strings.LastIndex(mergeFilePath, "/")+1:] c.JSON(200, resp.OkData(map[string]string{ "url": "//" + c.Request.Host + mergeFilePath, - "fileName": mergeFilePath, - "newFileName": newFileName, + "filePath": mergeFilePath, + "newFileName": filepath.Base(mergeFilePath), "originalFileName": body.FileName, })) } // 切片文件上传 // -// POST /chunkUpload +// POST /chunk-upload // // @Tags common/file // @Accept multipart/form-data @@ -211,17 +209,20 @@ func (s *FileController) ChunkMerge(c *gin.Context) { // @Security TokenAuth // @Summary Sliced file upload // @Description Sliced file upload -// @Router /file/chunkUpload [post] +// @Router /file/chunk-upload [post] func (s *FileController) ChunkUpload(c *gin.Context) { - language := reqctx.AcceptLanguage(c) // 切片编号 index := c.PostForm("index") // 切片唯一标识 identifier := c.PostForm("identifier") + if index == "" || identifier == "" { + c.JSON(400, resp.CodeMsg(40010, "bind err: index and identifier must be set")) + return + } // 上传的文件 formFile, err := c.FormFile("file") - if index == "" || identifier == "" || err != nil { - c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400"))) + if err != nil { + c.JSON(400, resp.CodeMsg(40010, "bind err: file is empty")) return } @@ -236,7 +237,7 @@ func (s *FileController) ChunkUpload(c *gin.Context) { // 转存指定对应文件到静态目录 // -// POST /transferStaticFile +// POST /transfer-static-file func (s *FileController) TransferStaticFile(c *gin.Context) { var body struct { UploadPath string `json:"uploadPath" binding:"required"` diff --git a/src/modules/system/controller/sys_config.go b/src/modules/system/controller/sys_config.go index 75d3fb95..1010ae84 100644 --- a/src/modules/system/controller/sys_config.go +++ b/src/modules/system/controller/sys_config.go @@ -310,7 +310,7 @@ func (s SysConfigController) Export(c *gin.Context) { // 参数配置修改配置参数 // -// PUT /changeValue +// PUT /change-value func (s *SysConfigController) ConfigValue(c *gin.Context) { language := reqctx.AcceptLanguage(c) var body struct { diff --git a/src/modules/system/system.go b/src/modules/system/system.go index f8c2fb57..3803f1a9 100644 --- a/src/modules/system/system.go +++ b/src/modules/system/system.go @@ -62,7 +62,7 @@ func Setup(router *gin.Engine) { controller.NewSysConfig.Export, ) sysConfigGroup.PUT("/change-value", - middleware.PreAuthorize(map[string][]string{"hasPerms": {"system:config:edit"}}), + middleware.PreAuthorize(map[string][]string{"hasPerms": {"system:config:edit"}, "hasRoles": {"admin"}}), middleware.OperateLog(middleware.OptionNew("log.operate.title.sysConfig", middleware.BUSINESS_TYPE_UPDATE)), controller.NewSysConfig.ConfigValue, ) @@ -234,7 +234,7 @@ func Setup(router *gin.Engine) { sysMenuGroup := router.Group("/system/menu") { sysMenuGroup.GET("/list", - middleware.PreAuthorize(map[string][]string{"hasPerms": {"system:menu:list"}}), + middleware.PreAuthorize(map[string][]string{"hasPerms": {"system:menu:list"}, "hasRoles": {"admin"}}), controller.NewSysMenu.List, ) sysMenuGroup.GET("/:menuId",