|
@@ -4,18 +4,15 @@ import (
|
|
|
"context"
|
|
|
"encoding/base64"
|
|
|
"file_upload/app/e"
|
|
|
- "file_upload/app/model"
|
|
|
"file_upload/global"
|
|
|
"file_upload/simple_zap"
|
|
|
"file_upload/utils"
|
|
|
- "fmt"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"github.com/go-playground/validator/v10"
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
"go.uber.org/zap"
|
|
|
"os"
|
|
|
- "path/filepath"
|
|
|
)
|
|
|
|
|
|
type Body struct {
|
|
@@ -30,7 +27,7 @@ type Body struct {
|
|
|
// 无返回值
|
|
|
func SaveFile(c *gin.Context) {
|
|
|
// 解析请求体中的文件信息
|
|
|
- file := model.Root{}
|
|
|
+ file := Body{}
|
|
|
err := c.BindJSON(&file)
|
|
|
if err != nil {
|
|
|
// 日志记录参数解析失败
|
|
@@ -39,41 +36,6 @@ func SaveFile(c *gin.Context) {
|
|
|
e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
|
|
|
return
|
|
|
}
|
|
|
- // 准备将文件信息序列化为BSON格式(MongoDB使用的数据格式)
|
|
|
- doc := bson.M{
|
|
|
- "name": file.Name,
|
|
|
- "type": file.Type,
|
|
|
- "data": file.Data,
|
|
|
- }
|
|
|
- if len(file.Data.Elements) == 2 {
|
|
|
- imagCode := file.Data.Elements[1].Url
|
|
|
- _, data := utils.ParseBase64ImageString(imagCode)
|
|
|
- decodeString, err := base64.StdEncoding.DecodeString(data)
|
|
|
- if err != nil {
|
|
|
- simple_zap.WithCtx(context.TODO()).Sugar().Warn(err, "base64解码失败")
|
|
|
- e.ResponseWithMsg(c, e.ERROR, "图片转换失败")
|
|
|
- return
|
|
|
- }
|
|
|
- optputpath := "./upload" + "/" + file.Name + ".png"
|
|
|
- create, err := os.Create(optputpath)
|
|
|
- if err != nil {
|
|
|
- simple_zap.WithCtx(context.TODO()).Sugar().Warn(err, "创建文件失败")
|
|
|
- e.ResponseWithMsg(c, e.ERROR, "创建文件失败")
|
|
|
- }
|
|
|
- defer create.Close()
|
|
|
- _, err = create.Write(decodeString)
|
|
|
- if err != nil {
|
|
|
- simple_zap.WithCtx(context.TODO()).Sugar().Warn(err, "写入文件失败")
|
|
|
- e.ResponseWithMsg(c, e.ERROR, "写入文件失败")
|
|
|
- }
|
|
|
- simple_zap.WithCtx(context.TODO()).Sugar().Info("文件写入成功")
|
|
|
- abs, err := filepath.Abs(optputpath)
|
|
|
- if err != nil {
|
|
|
- simple_zap.WithCtx(context.TODO()).Sugar().Warn()
|
|
|
- }
|
|
|
- file.Data.Elements[1].Url = abs
|
|
|
- fmt.Println(file.Data.Elements[1].Url)
|
|
|
- }
|
|
|
// 检查文件是否已存在
|
|
|
filter := bson.M{"name": file.Name}
|
|
|
count, err := global.MongoCon.CountDocuments(context.TODO(), filter)
|
|
@@ -89,6 +51,46 @@ func SaveFile(c *gin.Context) {
|
|
|
e.ResponseWithMsg(c, e.AlreadyExists, e.AlreadyExists.GetMsg())
|
|
|
return
|
|
|
}
|
|
|
+ var url string = "https://erp.baozhida.cn/assets/icon-91f5d92f.png"
|
|
|
+ // 准备将文件信息序列化为BSON格式(MongoDB使用的数据格式)
|
|
|
+ m, ok := file.Data.(map[string]any)
|
|
|
+ if ok {
|
|
|
+ elements := m["elements"].([]any)
|
|
|
+ for _, v := range elements {
|
|
|
+ imageurl := v.(map[string]any)["url"]
|
|
|
+ if imageurl != nil {
|
|
|
+ _, data := utils.ParseBase64ImageString(imageurl.(string))
|
|
|
+ decodeString, err := base64.StdEncoding.DecodeString(data)
|
|
|
+ if err != nil {
|
|
|
+ simple_zap.WithCtx(context.TODO()).Sugar().Warn(err, "base64解码失败")
|
|
|
+ e.ResponseWithMsg(c, e.ERROR, "图片转换失败")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ optputpath := "./upload" + "/" + file.Name + ".png"
|
|
|
+ create, err := os.Create(optputpath)
|
|
|
+ if err != nil {
|
|
|
+ simple_zap.WithCtx(context.TODO()).Sugar().Warn(err, "创建文件失败")
|
|
|
+ e.ResponseWithMsg(c, e.ERROR, "创建文件失败")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer create.Close()
|
|
|
+ _, err = create.Write(decodeString)
|
|
|
+ if err != nil {
|
|
|
+ simple_zap.WithCtx(context.TODO()).Sugar().Warn(err, "写入文件失败")
|
|
|
+ e.ResponseWithMsg(c, e.ERROR, "写入文件失败")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ simple_zap.WithCtx(context.TODO()).Sugar().Info("文件写入成功")
|
|
|
+ url = "http://localhost:8080/dwonload/" + file.Name + ".png"
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ doc := bson.M{
|
|
|
+ "name": file.Name,
|
|
|
+ "type": file.Type,
|
|
|
+ "data": file.Data,
|
|
|
+ "url": url,
|
|
|
+ }
|
|
|
// 保存文件到MongoDB
|
|
|
_, err = global.MongoCon.InsertOne(context.TODO(), doc)
|
|
|
if err != nil {
|
|
@@ -113,7 +115,10 @@ func TemplateItem(c *gin.Context) {
|
|
|
}
|
|
|
var result []bson.M
|
|
|
opts := options.Find()
|
|
|
- opts.SetProjection(bson.M{"_id": 0})
|
|
|
+ opts.SetProjection(bson.M{
|
|
|
+ "_id": 0,
|
|
|
+ "data": 0,
|
|
|
+ })
|
|
|
cur, err := global.MongoCon.Find(context.Background(), filter, opts)
|
|
|
if err != nil {
|
|
|
// 记录日志并返回错误信息
|
|
@@ -144,7 +149,6 @@ func GetTemplate(c *gin.Context) {
|
|
|
e.ResponseWithMsg(c, e.ERROR, "获取文件失败")
|
|
|
return
|
|
|
}
|
|
|
-
|
|
|
e.ResponseSuccess(c, result)
|
|
|
}
|
|
|
|
|
@@ -181,7 +185,10 @@ func SearchTemplate(c *gin.Context) {
|
|
|
fileter := bson.M{"name": bson.M{"$regex": query}}
|
|
|
var result []bson.M
|
|
|
find := options.Find()
|
|
|
- find.SetProjection(bson.M{"_id": 0})
|
|
|
+ find.SetProjection(bson.M{
|
|
|
+ "_id": 0,
|
|
|
+ "data": 0,
|
|
|
+ })
|
|
|
cursor, err := global.MongoCon.Find(context.Background(), fileter, find)
|
|
|
if err != nil {
|
|
|
simple_zap.Logger.Error("查询失败", zap.Error(err))
|
|
@@ -189,6 +196,5 @@ func SearchTemplate(c *gin.Context) {
|
|
|
return
|
|
|
}
|
|
|
err = cursor.All(context.Background(), &result)
|
|
|
-
|
|
|
e.ResponseSuccess(c, result)
|
|
|
}
|