|
@@ -3,12 +3,12 @@ package controller
|
|
|
import (
|
|
|
"context"
|
|
|
"file_upload/app/e"
|
|
|
- "file_upload/app/model"
|
|
|
"file_upload/global"
|
|
|
"file_upload/simple_zap"
|
|
|
- "github.com/bytedance/sonic"
|
|
|
"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"
|
|
|
)
|
|
|
|
|
@@ -18,76 +18,84 @@ type Body struct {
|
|
|
Type bool
|
|
|
}
|
|
|
|
|
|
-// func FuileUpload(c *gin.Context) {
|
|
|
-// const maxMemory = 1 << 20
|
|
|
-// if err := c.Request.ParseMultipartForm(maxMemory); err != nil {
|
|
|
-// e.ResponseWithMsg(c, e.ERROR, "获取文件失败")
|
|
|
-// simple_zap.WithCtx(context.Background()).Sugar().Warn(err)
|
|
|
-// return
|
|
|
-// }
|
|
|
-// file, err := c.FormFile("previewImage")
|
|
|
-// formFile, err := c.FormFile("excalidrawLib")
|
|
|
-// if err != nil {
|
|
|
-// e.ResponseWithMsg(c, e.ERROR, "获取文件失败")
|
|
|
-// simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取文件失败")
|
|
|
-// return
|
|
|
-// }
|
|
|
-// title := c.PostForm("name")
|
|
|
-// if err := c.SaveUploadedFile(file, "./upload/"+file.Filename+title+"."+"png"); err != nil {
|
|
|
-// e.ResponseWithMsg(c, e.ERROR, "保存文件失败")
|
|
|
-// simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "保存文件失败")
|
|
|
-// return
|
|
|
-// }
|
|
|
-// if err := c.SaveUploadedFile(formFile, "./upload/"+formFile.Filename+title+"."+"excalidrawlib"); err != nil {
|
|
|
-// e.ResponseWithMsg(c, e.ERROR, "保存文件失败")
|
|
|
-// simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "保存文件失败")
|
|
|
-// return
|
|
|
-// }
|
|
|
-// e.ResponseSuccess(c, e.SUCCESS)
|
|
|
+//func SaveFile(c *gin.Context) {
|
|
|
+// file := Body{}
|
|
|
+// err := c.BindJSON(&file)
|
|
|
+// if err != nil {
|
|
|
+// simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取参数失败")
|
|
|
+// e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
|
|
|
+// return
|
|
|
// }
|
|
|
-//
|
|
|
-// func FileDwon(c *gin.Context) {
|
|
|
-// filname := "cec.json"
|
|
|
-// filpath := "./upload/" + filname
|
|
|
-// if _, err := os.Stat(filpath); os.IsNotExist(err) {
|
|
|
-// simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "文件获取失败")
|
|
|
-// c.AbortWithStatus(http.StatusNotFound)
|
|
|
-// return
|
|
|
-// }
|
|
|
-// // 设置 Content-Disposition 响应头,提供默认的文件名
|
|
|
-// c.Header("Content-Disposition", "attachment; filename="+filname)
|
|
|
-// // 发送文件内容
|
|
|
-// c.File(filpath)
|
|
|
+// m := map[string]any{
|
|
|
+// "type": file.Type,
|
|
|
+// "data": file.Data,
|
|
|
+// }
|
|
|
+// marshal, err := sonic.Marshal(m)
|
|
|
+// if err != nil {
|
|
|
+// simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "序列化失败")
|
|
|
+// e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
|
|
|
+// return
|
|
|
+// }
|
|
|
+// result, err := global.Rdb.Exists(context.Background(), file.Name).Result()
|
|
|
+// if result == 1 {
|
|
|
+// e.ResponseWithMsg(c, e.AlreadyExists, e.AlreadyExists.GetMsg())
|
|
|
+// return
|
|
|
// }
|
|
|
+// set := global.Rdb.Set(context.Background(), file.Name, marshal, 0)
|
|
|
+// if set.Err() != nil {
|
|
|
+// simple_zap.WithCtx(context.Background()).Sugar().Warn(set.Err(), "保存文件失败")
|
|
|
+// e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
|
|
|
+// return
|
|
|
+// }
|
|
|
+// e.ResponseSuccess(c, e.SUCCESS)
|
|
|
+//}
|
|
|
+
|
|
|
+// SaveFile 保存文件到MongoDB
|
|
|
+// 参数:
|
|
|
+// - c *gin.Context: Gin框架的上下文对象,用于处理HTTP请求和响应
|
|
|
+// 无返回值
|
|
|
func SaveFile(c *gin.Context) {
|
|
|
+ // 解析请求体中的文件信息
|
|
|
file := Body{}
|
|
|
err := c.BindJSON(&file)
|
|
|
if err != nil {
|
|
|
+ // 日志记录参数解析失败
|
|
|
simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取参数失败")
|
|
|
+ // 返回参数解析失败的响应
|
|
|
e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
|
|
|
return
|
|
|
}
|
|
|
- m := map[string]any{
|
|
|
+ // 准备将文件信息序列化为BSON格式(MongoDB使用的数据格式)
|
|
|
+ doc := bson.M{
|
|
|
+ "name": file.Name,
|
|
|
"type": file.Type,
|
|
|
"data": file.Data,
|
|
|
}
|
|
|
- marshal, err := sonic.Marshal(m)
|
|
|
+ // 检查文件是否已存在
|
|
|
+ filter := bson.M{"name": file.Name}
|
|
|
+ count, err := global.MongoCon.CountDocuments(context.TODO(), filter)
|
|
|
if err != nil {
|
|
|
- simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "序列化失败")
|
|
|
+ // 日志记录查询失败
|
|
|
+ simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "查询文件是否存在失败")
|
|
|
+ // 返回错误响应
|
|
|
e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
|
|
|
return
|
|
|
}
|
|
|
- result, err := global.Rdb.Exists(context.Background(), file.Name).Result()
|
|
|
- if result == 1 {
|
|
|
+ if count > 0 {
|
|
|
+ // 返回文件已存在的响应
|
|
|
e.ResponseWithMsg(c, e.AlreadyExists, e.AlreadyExists.GetMsg())
|
|
|
return
|
|
|
}
|
|
|
- set := global.Rdb.Set(context.Background(), file.Name, marshal, 0)
|
|
|
- if set.Err() != nil {
|
|
|
- simple_zap.WithCtx(context.Background()).Sugar().Warn(set.Err(), "保存文件失败")
|
|
|
+ // 保存文件到MongoDB
|
|
|
+ _, err = global.MongoCon.InsertOne(context.TODO(), doc)
|
|
|
+ if err != nil {
|
|
|
+ // 日志记录保存文件失败
|
|
|
+ simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "保存文件失败")
|
|
|
+ // 返回保存文件失败的响应
|
|
|
e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
|
|
|
return
|
|
|
}
|
|
|
+ // 返回文件保存成功的响应
|
|
|
e.ResponseSuccess(c, e.SUCCESS)
|
|
|
}
|
|
|
|
|
@@ -96,69 +104,115 @@ func SaveFile(c *gin.Context) {
|
|
|
// - c *gin.Context: Gin框架的上下文对象,用于处理HTTP请求和响应
|
|
|
// 返回值:
|
|
|
// - 无
|
|
|
+//
|
|
|
+// func TemplateItem(c *gin.Context) {
|
|
|
+// type Types struct {
|
|
|
+// Type bool `json:"type"`
|
|
|
+// }
|
|
|
+// t := &Types{}
|
|
|
+// c.BindJSON(&t)
|
|
|
+// var allKeys []string // 存储所有检索到的模板项的键名
|
|
|
+// var cursor uint64 = 0 // 用于分页查询的游标
|
|
|
+// // 循环检索模板项,直到遍历完所有项或出现错误
|
|
|
+// for {
|
|
|
+// keys, newCursor, err := global.Rdb.Scan(context.Background(), cursor, "*", 100).Result() // 每次检索最多100个键名
|
|
|
+// if err != nil {
|
|
|
+// // 记录日志并返回错误信息
|
|
|
+// simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取模板失败")
|
|
|
+// e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
|
|
|
+// return
|
|
|
+// }
|
|
|
+// allKeys = append(allKeys, keys...) // 将检索到的键名添加到allKeys列表
|
|
|
+// cursor = newCursor // 更新游标
|
|
|
+// if cursor == 0 {
|
|
|
+// break // 如果游标为0,表示已遍历完所有项,退出循环
|
|
|
+// }
|
|
|
+// }
|
|
|
+// m := make(map[string]any) // 创建一个map用于存储模板项的键名和对应的值
|
|
|
+// // 遍历所有键名,获取其对应的值
|
|
|
+// for _, key := range allKeys {
|
|
|
+// result, err := global.Rdb.Get(context.Background(), key).Result()
|
|
|
+// if err != nil {
|
|
|
+// // 记录日志并返回错误信息
|
|
|
+// simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取模板失败")
|
|
|
+// e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
|
|
|
+// return
|
|
|
+// }
|
|
|
+// file := model.Root{}
|
|
|
+// //marshal, err := sonic.Marshal(result)
|
|
|
+// err = sonic.Unmarshal([]byte(result), &file)
|
|
|
+// if err != nil {
|
|
|
+// // 记录日志并返回错误信息
|
|
|
+// simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "序列化失败")
|
|
|
+// e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
|
|
|
+// return
|
|
|
+// }
|
|
|
+// if t.Type == file.Type {
|
|
|
+// m[key] = result // 将键名和对应的值添加到map中
|
|
|
+// }
|
|
|
+// }
|
|
|
+// // 返回所有模板项的键值对
|
|
|
+// e.ResponseSuccess(c, m)
|
|
|
+// }
|
|
|
func TemplateItem(c *gin.Context) {
|
|
|
type Types struct {
|
|
|
Type bool `json:"type"`
|
|
|
}
|
|
|
t := &Types{}
|
|
|
c.BindJSON(&t)
|
|
|
- var allKeys []string // 存储所有检索到的模板项的键名
|
|
|
- var cursor uint64 = 0 // 用于分页查询的游标
|
|
|
- // 循环检索模板项,直到遍历完所有项或出现错误
|
|
|
- for {
|
|
|
- keys, newCursor, err := global.Rdb.Scan(context.Background(), cursor, "*", 100).Result() // 每次检索最多100个键名
|
|
|
- if err != nil {
|
|
|
- // 记录日志并返回错误信息
|
|
|
- simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取模板失败")
|
|
|
- e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
|
|
|
- return
|
|
|
- }
|
|
|
- allKeys = append(allKeys, keys...) // 将检索到的键名添加到allKeys列表
|
|
|
- cursor = newCursor // 更新游标
|
|
|
- if cursor == 0 {
|
|
|
- break // 如果游标为0,表示已遍历完所有项,退出循环
|
|
|
- }
|
|
|
- }
|
|
|
- m := make(map[string]any) // 创建一个map用于存储模板项的键名和对应的值
|
|
|
- // 遍历所有键名,获取其对应的值
|
|
|
- for _, key := range allKeys {
|
|
|
- result, err := global.Rdb.Get(context.Background(), key).Result()
|
|
|
- if err != nil {
|
|
|
- // 记录日志并返回错误信息
|
|
|
- simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取模板失败")
|
|
|
- e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
|
|
|
- return
|
|
|
- }
|
|
|
- file := model.Root{}
|
|
|
- //marshal, err := sonic.Marshal(result)
|
|
|
- err = sonic.Unmarshal([]byte(result), &file)
|
|
|
- if err != nil {
|
|
|
- // 记录日志并返回错误信息
|
|
|
- simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "序列化失败")
|
|
|
- e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
|
|
|
- return
|
|
|
- }
|
|
|
- if t.Type == file.Type {
|
|
|
- m[key] = result // 将键名和对应的值添加到map中
|
|
|
- }
|
|
|
- }
|
|
|
- // 返回所有模板项的键值对
|
|
|
- e.ResponseSuccess(c, m)
|
|
|
+ filter := bson.M{
|
|
|
+ "type": t.Type,
|
|
|
+ }
|
|
|
+ var result []bson.M
|
|
|
+ opts := options.Find()
|
|
|
+ opts.SetProjection(bson.M{"_id": 0})
|
|
|
+ cur, err := global.MongoCon.Find(context.Background(), filter, opts)
|
|
|
+ if err != nil {
|
|
|
+ // 记录日志并返回错误信息
|
|
|
+ simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取模板失败")
|
|
|
+ e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ err = cur.All(context.Background(), &result)
|
|
|
+ if err != nil {
|
|
|
+ // 记录日志并返回错误信息
|
|
|
+ simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取模板失败")
|
|
|
+ e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ e.ResponseSuccess(c, result)
|
|
|
}
|
|
|
|
|
|
// GetTemplate 从服务器获取模板
|
|
|
+//func GetTemplate(c *gin.Context) {
|
|
|
+// name := c.Query("name")
|
|
|
+// if name == "" {
|
|
|
+// e.ResponseWithMsg(c, e.ERROR, "参数错误")
|
|
|
+// return
|
|
|
+// }
|
|
|
+// result, err := global.Rdb.Get(context.Background(), name).Result()
|
|
|
+// if err != nil {
|
|
|
+// simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取文件失败")
|
|
|
+// e.ResponseWithMsg(c, e.ERROR, "获取文件失败")
|
|
|
+// return
|
|
|
+// }
|
|
|
+// e.ResponseSuccess(c, result)
|
|
|
+//}
|
|
|
+
|
|
|
func GetTemplate(c *gin.Context) {
|
|
|
name := c.Query("name")
|
|
|
if name == "" {
|
|
|
e.ResponseWithMsg(c, e.ERROR, "参数错误")
|
|
|
return
|
|
|
}
|
|
|
- result, err := global.Rdb.Get(context.Background(), name).Result()
|
|
|
+ var result map[string]interface{}
|
|
|
+ err := global.MongoCon.FindOne(context.Background(), bson.M{"name": name}).Decode(&result)
|
|
|
if err != nil {
|
|
|
simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取文件失败")
|
|
|
e.ResponseWithMsg(c, e.ERROR, "获取文件失败")
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
e.ResponseSuccess(c, result)
|
|
|
}
|
|
|
|
|
@@ -169,14 +223,51 @@ func DeleteTemplate(c *gin.Context) {
|
|
|
e.ResponseWithMsg(c, e.ERROR, "参数错误")
|
|
|
return
|
|
|
}
|
|
|
- err := global.Rdb.Del(context.Background(), name).Err()
|
|
|
+ one, err := global.MongoCon.DeleteOne(context.Background(), bson.M{"name": name})
|
|
|
+ //err := global.Rdb.Del(context.Background(), name).Err()
|
|
|
if err != nil {
|
|
|
simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "删除文件失败")
|
|
|
e.ResponseWithMsg(c, e.ERROR, "删除文件失败")
|
|
|
return
|
|
|
}
|
|
|
- e.ResponseSuccess(c, e.SUCCESS)
|
|
|
+ if one.DeletedCount > 0 {
|
|
|
+ e.ResponseSuccess(c, one)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ e.ResponseWithMsg(c, e.ERROR, "删除文件失败")
|
|
|
}
|
|
|
+
|
|
|
+// func SearchTemplate(c *gin.Context) {
|
|
|
+// query := c.Query("name")
|
|
|
+// validate := validator.New()
|
|
|
+// validate.Var("name", "required")
|
|
|
+// if query == "" {
|
|
|
+// e.ResponseWithMsg(c, e.ERROR, "参数错误")
|
|
|
+// return
|
|
|
+// }
|
|
|
+// result, err := global.Rdb.Keys(context.Background(), "*"+query+"*").Result()
|
|
|
+// if err != nil {
|
|
|
+// simple_zap.Logger.Error("查询失败", zap.Error(err))
|
|
|
+// e.ResponseWithMsg(c, e.ERROR, "查询失败")
|
|
|
+// return
|
|
|
+// }
|
|
|
+// if len(result) == 0 {
|
|
|
+// simple_zap.Logger.Info("查询失败")
|
|
|
+// e.ResponseWithMsg(c, e.ERROR, "未找到文件")
|
|
|
+// return
|
|
|
+// }
|
|
|
+// m := make(map[string]any)
|
|
|
+// for _, v := range result {
|
|
|
+// re, err := global.Rdb.Get(context.Background(), v).Result()
|
|
|
+// if err != nil {
|
|
|
+// simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取文件失败")
|
|
|
+// e.ResponseWithMsg(c, e.ERROR, "获取文件失败")
|
|
|
+// return
|
|
|
+// }
|
|
|
+// m[v] = re
|
|
|
+// }
|
|
|
+// e.ResponseSuccess(c, m)
|
|
|
+// }
|
|
|
func SearchTemplate(c *gin.Context) {
|
|
|
query := c.Query("name")
|
|
|
validate := validator.New()
|
|
@@ -185,26 +276,28 @@ func SearchTemplate(c *gin.Context) {
|
|
|
e.ResponseWithMsg(c, e.ERROR, "参数错误")
|
|
|
return
|
|
|
}
|
|
|
- result, err := global.Rdb.Keys(context.Background(), "*"+query+"*").Result()
|
|
|
+ //result, err := global.Rdb.Keys(context.Background(), "*"+query+"*").Result()
|
|
|
+ fileter := bson.M{"name": bson.M{"$regex": query}}
|
|
|
+ var result []bson.M
|
|
|
+ find := options.Find()
|
|
|
+ find.SetProjection(bson.M{"_id": 0})
|
|
|
+ cursor, err := global.MongoCon.Find(context.Background(), fileter, find)
|
|
|
if err != nil {
|
|
|
simple_zap.Logger.Error("查询失败", zap.Error(err))
|
|
|
e.ResponseWithMsg(c, e.ERROR, "查询失败")
|
|
|
return
|
|
|
}
|
|
|
- if len(result) == 0 {
|
|
|
- simple_zap.Logger.Info("查询失败")
|
|
|
- e.ResponseWithMsg(c, e.ERROR, "未找到文件")
|
|
|
- return
|
|
|
- }
|
|
|
- m := make(map[string]any)
|
|
|
- for _, v := range result {
|
|
|
- re, err := global.Rdb.Get(context.Background(), v).Result()
|
|
|
- if err != nil {
|
|
|
- simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取文件失败")
|
|
|
- e.ResponseWithMsg(c, e.ERROR, "获取文件失败")
|
|
|
- return
|
|
|
- }
|
|
|
- m[v] = re
|
|
|
- }
|
|
|
- e.ResponseSuccess(c, m)
|
|
|
+ err = cursor.All(context.Background(), &result)
|
|
|
+
|
|
|
+ //m := make(map[string]any)
|
|
|
+ //for _, v := range result {
|
|
|
+ // re, err := global.Rdb.Get(context.Background(), v).Result()
|
|
|
+ // if err != nil {
|
|
|
+ // simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取文件失败")
|
|
|
+ // e.ResponseWithMsg(c, e.ERROR, "获取文件失败")
|
|
|
+ // return
|
|
|
+ // }
|
|
|
+ // m[v] = re
|
|
|
+ //}
|
|
|
+ e.ResponseSuccess(c, result)
|
|
|
}
|