文件上传

This commit is contained in:
tfl
2024-09-23 08:43:18 +08:00
parent c5d073591b
commit a628ae35c0
3 changed files with 14 additions and 4 deletions

View File

@@ -14,11 +14,12 @@ func InitUploadRouter(container *restful.Container) {
ws.Path("/upload").Produces(restful.MIME_JSON)
tags := []string{"system", "文件"}
ws.Route(ws.POST("/up").To(func(request *restful.Request, response *restful.Response) {
ws.Route(ws.POST("/up/{path}").To(func(request *restful.Request, response *restful.Response) {
restfulx.NewReqCtx(request, response).WithLog("上传图片").Handle(s.UploadImage)
}).
Doc("上传图片").
Param(ws.FormParameter("imagefile", "文件")).
Param(ws.PathParameter("path", "文件类型")).
Metadata(restfulspec.KeyOpenAPITags, tags).
Returns(200, "OK", map[string]string{}))
@@ -30,7 +31,7 @@ func InitUploadRouter(container *restful.Container) {
Metadata(restfulspec.KeyOpenAPITags, tags).
Returns(200, "OK", map[string]string{}))
ws.Route(ws.GET("/get/{subpath}").To(func(request *restful.Request, response *restful.Response) {
ws.Route(ws.GET("/down/{path}/{subpath:*}").To(func(request *restful.Request, response *restful.Response) {
restfulx.NewReqCtx(request, response).WithNeedToken(false).WithNeedCasbin(false).WithLog("获取文件").Handle(s.GetImage)
}).
Doc("获取文件").

BIN
pkg/rule_engine.zip Normal file

Binary file not shown.

View File

@@ -11,6 +11,7 @@ import (
"os"
"pandax/pkg/global"
"path"
"regexp"
"strings"
"time"
)
@@ -94,12 +95,20 @@ func (local *Local) UploadFile(file *multipart.FileHeader) (string, string, erro
}
func (local *Local) Base64ToFile(name, base64Str string) (path string, fileName string, err error) {
fileType := "jpg"
if strings.Contains(base64Str, "data:image") {
base64Str = strings.TrimPrefix(base64Str, "data:image/png;base64,")
re := regexp.MustCompile(`^data:image/(\w+);base64,?`)
matchedString := re.FindStringSubmatch(base64Str)
global.Log.Info("re", matchedString)
if len(matchedString) <= 1 {
return "", "", errors.New("文件Base64格式错误")
}
base64Str = strings.TrimPrefix(base64Str, matchedString[0])
fileType = matchedString[1]
}
imgData, err := base64.StdEncoding.DecodeString(base64Str)
filename := name + ".png"
filename := name + "." + fileType
path = local.Path + "/" + filename
_, err = os.Stat(path)
if err == nil {