mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 02:48:34 +08:00
文件上传
This commit is contained in:
@@ -14,11 +14,12 @@ func InitUploadRouter(container *restful.Container) {
|
|||||||
ws.Path("/upload").Produces(restful.MIME_JSON)
|
ws.Path("/upload").Produces(restful.MIME_JSON)
|
||||||
tags := []string{"system", "文件"}
|
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)
|
restfulx.NewReqCtx(request, response).WithLog("上传图片").Handle(s.UploadImage)
|
||||||
}).
|
}).
|
||||||
Doc("上传图片").
|
Doc("上传图片").
|
||||||
Param(ws.FormParameter("imagefile", "文件")).
|
Param(ws.FormParameter("imagefile", "文件")).
|
||||||
|
Param(ws.PathParameter("path", "文件类型")).
|
||||||
Metadata(restfulspec.KeyOpenAPITags, tags).
|
Metadata(restfulspec.KeyOpenAPITags, tags).
|
||||||
Returns(200, "OK", map[string]string{}))
|
Returns(200, "OK", map[string]string{}))
|
||||||
|
|
||||||
@@ -30,7 +31,7 @@ func InitUploadRouter(container *restful.Container) {
|
|||||||
Metadata(restfulspec.KeyOpenAPITags, tags).
|
Metadata(restfulspec.KeyOpenAPITags, tags).
|
||||||
Returns(200, "OK", map[string]string{}))
|
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)
|
restfulx.NewReqCtx(request, response).WithNeedToken(false).WithNeedCasbin(false).WithLog("获取文件").Handle(s.GetImage)
|
||||||
}).
|
}).
|
||||||
Doc("获取文件").
|
Doc("获取文件").
|
||||||
|
|||||||
BIN
pkg/rule_engine.zip
Normal file
BIN
pkg/rule_engine.zip
Normal file
Binary file not shown.
@@ -11,6 +11,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"pandax/pkg/global"
|
"pandax/pkg/global"
|
||||||
"path"
|
"path"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"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) {
|
func (local *Local) Base64ToFile(name, base64Str string) (path string, fileName string, err error) {
|
||||||
|
fileType := "jpg"
|
||||||
if strings.Contains(base64Str, "data:image") {
|
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)
|
imgData, err := base64.StdEncoding.DecodeString(base64Str)
|
||||||
filename := name + ".png"
|
filename := name + "." + fileType
|
||||||
path = local.Path + "/" + filename
|
path = local.Path + "/" + filename
|
||||||
_, err = os.Stat(path)
|
_, err = os.Stat(path)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user