file.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package controller
  2. import (
  3. "context"
  4. "file_upload/app/e"
  5. "file_upload/global"
  6. "file_upload/simple_zap"
  7. "github.com/gin-gonic/gin"
  8. "github.com/go-playground/validator/v10"
  9. "go.mongodb.org/mongo-driver/bson"
  10. "go.mongodb.org/mongo-driver/mongo/options"
  11. "go.uber.org/zap"
  12. )
  13. type Body struct {
  14. Name string
  15. Data any
  16. Type bool
  17. }
  18. //func SaveFile(c *gin.Context) {
  19. // file := Body{}
  20. // err := c.BindJSON(&file)
  21. // if err != nil {
  22. // simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取参数失败")
  23. // e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  24. // return
  25. // }
  26. // m := map[string]any{
  27. // "type": file.Type,
  28. // "data": file.Data,
  29. // }
  30. // marshal, err := sonic.Marshal(m)
  31. // if err != nil {
  32. // simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "序列化失败")
  33. // e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
  34. // return
  35. // }
  36. // result, err := global.Rdb.Exists(context.Background(), file.Name).Result()
  37. // if result == 1 {
  38. // e.ResponseWithMsg(c, e.AlreadyExists, e.AlreadyExists.GetMsg())
  39. // return
  40. // }
  41. // set := global.Rdb.Set(context.Background(), file.Name, marshal, 0)
  42. // if set.Err() != nil {
  43. // simple_zap.WithCtx(context.Background()).Sugar().Warn(set.Err(), "保存文件失败")
  44. // e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
  45. // return
  46. // }
  47. // e.ResponseSuccess(c, e.SUCCESS)
  48. //}
  49. // SaveFile 保存文件到MongoDB
  50. // 参数:
  51. // - c *gin.Context: Gin框架的上下文对象,用于处理HTTP请求和响应
  52. // 无返回值
  53. func SaveFile(c *gin.Context) {
  54. // 解析请求体中的文件信息
  55. file := Body{}
  56. err := c.BindJSON(&file)
  57. if err != nil {
  58. // 日志记录参数解析失败
  59. simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取参数失败")
  60. // 返回参数解析失败的响应
  61. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  62. return
  63. }
  64. // 准备将文件信息序列化为BSON格式(MongoDB使用的数据格式)
  65. doc := bson.M{
  66. "name": file.Name,
  67. "type": file.Type,
  68. "data": file.Data,
  69. }
  70. // 检查文件是否已存在
  71. filter := bson.M{"name": file.Name}
  72. count, err := global.MongoCon.CountDocuments(context.TODO(), filter)
  73. if err != nil {
  74. // 日志记录查询失败
  75. simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "查询文件是否存在失败")
  76. // 返回错误响应
  77. e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
  78. return
  79. }
  80. if count > 0 {
  81. // 返回文件已存在的响应
  82. e.ResponseWithMsg(c, e.AlreadyExists, e.AlreadyExists.GetMsg())
  83. return
  84. }
  85. // 保存文件到MongoDB
  86. _, err = global.MongoCon.InsertOne(context.TODO(), doc)
  87. if err != nil {
  88. // 日志记录保存文件失败
  89. simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "保存文件失败")
  90. // 返回保存文件失败的响应
  91. e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
  92. return
  93. }
  94. // 返回文件保存成功的响应
  95. e.ResponseSuccess(c, e.SUCCESS)
  96. }
  97. // TemplateItem 用于获取所有的模板项
  98. // 参数:
  99. // - c *gin.Context: Gin框架的上下文对象,用于处理HTTP请求和响应
  100. // 返回值:
  101. // - 无
  102. //
  103. // func TemplateItem(c *gin.Context) {
  104. // type Types struct {
  105. // Type bool `json:"type"`
  106. // }
  107. // t := &Types{}
  108. // c.BindJSON(&t)
  109. // var allKeys []string // 存储所有检索到的模板项的键名
  110. // var cursor uint64 = 0 // 用于分页查询的游标
  111. // // 循环检索模板项,直到遍历完所有项或出现错误
  112. // for {
  113. // keys, newCursor, err := global.Rdb.Scan(context.Background(), cursor, "*", 100).Result() // 每次检索最多100个键名
  114. // if err != nil {
  115. // // 记录日志并返回错误信息
  116. // simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取模板失败")
  117. // e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
  118. // return
  119. // }
  120. // allKeys = append(allKeys, keys...) // 将检索到的键名添加到allKeys列表
  121. // cursor = newCursor // 更新游标
  122. // if cursor == 0 {
  123. // break // 如果游标为0,表示已遍历完所有项,退出循环
  124. // }
  125. // }
  126. // m := make(map[string]any) // 创建一个map用于存储模板项的键名和对应的值
  127. // // 遍历所有键名,获取其对应的值
  128. // for _, key := range allKeys {
  129. // result, err := global.Rdb.Get(context.Background(), key).Result()
  130. // if err != nil {
  131. // // 记录日志并返回错误信息
  132. // simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取模板失败")
  133. // e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
  134. // return
  135. // }
  136. // file := model.Root{}
  137. // //marshal, err := sonic.Marshal(result)
  138. // err = sonic.Unmarshal([]byte(result), &file)
  139. // if err != nil {
  140. // // 记录日志并返回错误信息
  141. // simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "序列化失败")
  142. // e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
  143. // return
  144. // }
  145. // if t.Type == file.Type {
  146. // m[key] = result // 将键名和对应的值添加到map中
  147. // }
  148. // }
  149. // // 返回所有模板项的键值对
  150. // e.ResponseSuccess(c, m)
  151. // }
  152. func TemplateItem(c *gin.Context) {
  153. type Types struct {
  154. Type bool `json:"type"`
  155. }
  156. t := &Types{}
  157. c.BindJSON(&t)
  158. filter := bson.M{
  159. "type": t.Type,
  160. }
  161. var result []bson.M
  162. opts := options.Find()
  163. opts.SetProjection(bson.M{"_id": 0})
  164. cur, err := global.MongoCon.Find(context.Background(), filter, opts)
  165. if err != nil {
  166. // 记录日志并返回错误信息
  167. simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取模板失败")
  168. e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
  169. return
  170. }
  171. err = cur.All(context.Background(), &result)
  172. if err != nil {
  173. // 记录日志并返回错误信息
  174. simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取模板失败")
  175. e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
  176. return
  177. }
  178. e.ResponseSuccess(c, result)
  179. }
  180. // GetTemplate 从服务器获取模板
  181. //func GetTemplate(c *gin.Context) {
  182. // name := c.Query("name")
  183. // if name == "" {
  184. // e.ResponseWithMsg(c, e.ERROR, "参数错误")
  185. // return
  186. // }
  187. // result, err := global.Rdb.Get(context.Background(), name).Result()
  188. // if err != nil {
  189. // simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取文件失败")
  190. // e.ResponseWithMsg(c, e.ERROR, "获取文件失败")
  191. // return
  192. // }
  193. // e.ResponseSuccess(c, result)
  194. //}
  195. func GetTemplate(c *gin.Context) {
  196. name := c.Query("name")
  197. if name == "" {
  198. e.ResponseWithMsg(c, e.ERROR, "参数错误")
  199. return
  200. }
  201. var result map[string]interface{}
  202. err := global.MongoCon.FindOne(context.Background(), bson.M{"name": name}).Decode(&result)
  203. if err != nil {
  204. simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取文件失败")
  205. e.ResponseWithMsg(c, e.ERROR, "获取文件失败")
  206. return
  207. }
  208. e.ResponseSuccess(c, result)
  209. }
  210. // DeleteTemplate 删除模板
  211. func DeleteTemplate(c *gin.Context) {
  212. name := c.Query("name")
  213. if name == "" {
  214. e.ResponseWithMsg(c, e.ERROR, "参数错误")
  215. return
  216. }
  217. one, err := global.MongoCon.DeleteOne(context.Background(), bson.M{"name": name})
  218. //err := global.Rdb.Del(context.Background(), name).Err()
  219. if err != nil {
  220. simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "删除文件失败")
  221. e.ResponseWithMsg(c, e.ERROR, "删除文件失败")
  222. return
  223. }
  224. if one.DeletedCount > 0 {
  225. e.ResponseSuccess(c, one)
  226. return
  227. }
  228. e.ResponseWithMsg(c, e.ERROR, "删除文件失败")
  229. }
  230. // func SearchTemplate(c *gin.Context) {
  231. // query := c.Query("name")
  232. // validate := validator.New()
  233. // validate.Var("name", "required")
  234. // if query == "" {
  235. // e.ResponseWithMsg(c, e.ERROR, "参数错误")
  236. // return
  237. // }
  238. // result, err := global.Rdb.Keys(context.Background(), "*"+query+"*").Result()
  239. // if err != nil {
  240. // simple_zap.Logger.Error("查询失败", zap.Error(err))
  241. // e.ResponseWithMsg(c, e.ERROR, "查询失败")
  242. // return
  243. // }
  244. // if len(result) == 0 {
  245. // simple_zap.Logger.Info("查询失败")
  246. // e.ResponseWithMsg(c, e.ERROR, "未找到文件")
  247. // return
  248. // }
  249. // m := make(map[string]any)
  250. // for _, v := range result {
  251. // re, err := global.Rdb.Get(context.Background(), v).Result()
  252. // if err != nil {
  253. // simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取文件失败")
  254. // e.ResponseWithMsg(c, e.ERROR, "获取文件失败")
  255. // return
  256. // }
  257. // m[v] = re
  258. // }
  259. // e.ResponseSuccess(c, m)
  260. // }
  261. func SearchTemplate(c *gin.Context) {
  262. query := c.Query("name")
  263. validate := validator.New()
  264. validate.Var("name", "required")
  265. if query == "" {
  266. e.ResponseWithMsg(c, e.ERROR, "参数错误")
  267. return
  268. }
  269. //result, err := global.Rdb.Keys(context.Background(), "*"+query+"*").Result()
  270. fileter := bson.M{"name": bson.M{"$regex": query}}
  271. var result []bson.M
  272. find := options.Find()
  273. find.SetProjection(bson.M{"_id": 0})
  274. cursor, err := global.MongoCon.Find(context.Background(), fileter, find)
  275. if err != nil {
  276. simple_zap.Logger.Error("查询失败", zap.Error(err))
  277. e.ResponseWithMsg(c, e.ERROR, "查询失败")
  278. return
  279. }
  280. err = cursor.All(context.Background(), &result)
  281. //m := make(map[string]any)
  282. //for _, v := range result {
  283. // re, err := global.Rdb.Get(context.Background(), v).Result()
  284. // if err != nil {
  285. // simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取文件失败")
  286. // e.ResponseWithMsg(c, e.ERROR, "获取文件失败")
  287. // return
  288. // }
  289. // m[v] = re
  290. //}
  291. e.ResponseSuccess(c, result)
  292. }