123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- package controller
- import (
- "context"
- "file_upload/app/e"
- "file_upload/global"
- "file_upload/simple_zap"
- "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"
- )
- type Body struct {
- Name string
- Data any
- Type bool
- }
- //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{
- // "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
- }
- // 准备将文件信息序列化为BSON格式(MongoDB使用的数据格式)
- doc := bson.M{
- "name": file.Name,
- "type": file.Type,
- "data": file.Data,
- }
- // 检查文件是否已存在
- 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, "查询文件是否存在失败")
- // 返回错误响应
- e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
- return
- }
- if count > 0 {
- // 返回文件已存在的响应
- e.ResponseWithMsg(c, e.AlreadyExists, e.AlreadyExists.GetMsg())
- return
- }
- // 保存文件到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)
- }
- // TemplateItem 用于获取所有的模板项
- // 参数:
- // - 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)
- 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
- }
- 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)
- }
- // DeleteTemplate 删除模板
- func DeleteTemplate(c *gin.Context) {
- name := c.Query("name")
- if name == "" {
- e.ResponseWithMsg(c, e.ERROR, "参数错误")
- return
- }
- 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
- }
- 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()
- validate.Var("name", "required")
- if query == "" {
- e.ResponseWithMsg(c, e.ERROR, "参数错误")
- return
- }
- //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
- }
- 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)
- }
|