feat: 系统使用文档转存
This commit is contained in:
@@ -2,7 +2,9 @@ package file
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
@@ -295,6 +297,43 @@ func ChunkMergeFile(identifier, originalFileName, subPath string) (string, error
|
||||
return filepath.ToSlash(urlPath), nil
|
||||
}
|
||||
|
||||
// CopyUploadFile 将上传文件资源转移新目录
|
||||
//
|
||||
// filePath 上传得到的文件路径 /upload....
|
||||
// dst 新文件路径 /a/xx.pdf
|
||||
func CopyUploadFile(filePath, dst string) error {
|
||||
srcPath := ParseUploadFilePath(filePath)
|
||||
src, err := os.Open(srcPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer src.Close()
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(dst), 0750); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 如果目标文件已经存在,先将目标文件重命名
|
||||
if _, err := os.Stat(dst); err == nil {
|
||||
ext := filepath.Ext(dst)
|
||||
name := dst[0 : len(dst)-len(ext)]
|
||||
newName := fmt.Sprintf("%s-%s%s", name, time.Now().Format("20060102_150405"), ext)
|
||||
err := os.Rename(dst, newName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
out, err := os.Create(dst)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
_, err = io.Copy(out, src)
|
||||
return err
|
||||
}
|
||||
|
||||
// ParseUploadFileDir 得到上传资源目录
|
||||
//
|
||||
// subPath 子路径,默认 UploadSubPath.DEFAULT
|
||||
@@ -304,7 +343,7 @@ func ParseUploadFileDir(subPath string) string {
|
||||
return filepath.Join(dir, filePath)
|
||||
}
|
||||
|
||||
// ParseUploadFilePath 本地资源路径
|
||||
// ParseUploadFilePath 上传资源本地绝对资源路径
|
||||
//
|
||||
// filePath 上传文件路径
|
||||
func ParseUploadFilePath(filePath string) string {
|
||||
|
||||
@@ -3,6 +3,7 @@ package common
|
||||
import (
|
||||
"ems.agt/src/framework/logger"
|
||||
"ems.agt/src/framework/middleware"
|
||||
"ems.agt/src/framework/middleware/collectlogs"
|
||||
"ems.agt/src/modules/common/controller"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -25,6 +26,12 @@ func Setup(router *gin.Engine) {
|
||||
|
||||
// 系统可暴露的配置信息
|
||||
indexGroup.GET("/sys-conf", controller.NewCommont.SysConfig)
|
||||
// 系统使用文档转存
|
||||
indexGroup.POST("/help-doc",
|
||||
middleware.PreAuthorize(nil),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("系统使用文档", collectlogs.BUSINESS_TYPE_UPDATE)),
|
||||
controller.NewCommont.HelpDoc,
|
||||
)
|
||||
|
||||
// 验证码操作处理
|
||||
indexGroup.GET("/captchaImage",
|
||||
|
||||
@@ -1,11 +1,18 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"ems.agt/src/framework/config"
|
||||
"ems.agt/src/framework/i18n"
|
||||
"ems.agt/src/framework/utils/ctx"
|
||||
"ems.agt/src/framework/utils/file"
|
||||
"ems.agt/src/framework/vo/result"
|
||||
commonService "ems.agt/src/modules/common/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
|
||||
// 实例化控制层 CommontController 结构体
|
||||
@@ -69,3 +76,43 @@ func (s *CommontController) SysConfig(c *gin.Context) {
|
||||
|
||||
c.JSON(200, result.OkData(data))
|
||||
}
|
||||
|
||||
// 转存帮助文档
|
||||
//
|
||||
// POST /help-doc
|
||||
func (s *CommontController) HelpDoc(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body struct {
|
||||
UploadPath string `json:"uploadPath" binding:"required"`
|
||||
Language string `json:"language" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 取语言前缀
|
||||
lang := strings.SplitN(body.Language, "_", 2)[0]
|
||||
|
||||
// 默认静态资源
|
||||
static := config.Get("staticFile.default").(map[string]any)
|
||||
dir, err := filepath.Abs(static["dir"].(string))
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
newFile := fmt.Sprintf("%s/helpDoc/%s_doc.pdf", dir, lang)
|
||||
fmt.Println(newFile)
|
||||
|
||||
// dst := ""
|
||||
|
||||
err = file.CopyUploadFile(body.UploadPath, newFile)
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
urlPath := strings.Replace(newFile, dir, static["prefix"].(string), 1)
|
||||
c.JSON(200, result.OkData(filepath.ToSlash(urlPath)))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user