file.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package controller
  2. import (
  3. "context"
  4. "file_upload/app/e"
  5. "file_upload/app/model"
  6. "file_upload/global"
  7. "file_upload/simple_zap"
  8. "github.com/bytedance/sonic"
  9. "github.com/gin-gonic/gin"
  10. )
  11. type Body struct {
  12. Name string
  13. Data any
  14. Type bool
  15. }
  16. // func FuileUpload(c *gin.Context) {
  17. // const maxMemory = 1 << 20
  18. // if err := c.Request.ParseMultipartForm(maxMemory); err != nil {
  19. // e.ResponseWithMsg(c, e.ERROR, "获取文件失败")
  20. // simple_zap.WithCtx(context.Background()).Sugar().Warn(err)
  21. // return
  22. // }
  23. // file, err := c.FormFile("previewImage")
  24. // formFile, err := c.FormFile("excalidrawLib")
  25. // if err != nil {
  26. // e.ResponseWithMsg(c, e.ERROR, "获取文件失败")
  27. // simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取文件失败")
  28. // return
  29. // }
  30. // title := c.PostForm("name")
  31. // if err := c.SaveUploadedFile(file, "./upload/"+file.Filename+title+"."+"png"); err != nil {
  32. // e.ResponseWithMsg(c, e.ERROR, "保存文件失败")
  33. // simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "保存文件失败")
  34. // return
  35. // }
  36. // if err := c.SaveUploadedFile(formFile, "./upload/"+formFile.Filename+title+"."+"excalidrawlib"); err != nil {
  37. // e.ResponseWithMsg(c, e.ERROR, "保存文件失败")
  38. // simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "保存文件失败")
  39. // return
  40. // }
  41. // e.ResponseSuccess(c, e.SUCCESS)
  42. // }
  43. //
  44. // func FileDwon(c *gin.Context) {
  45. // filname := "cec.json"
  46. // filpath := "./upload/" + filname
  47. // if _, err := os.Stat(filpath); os.IsNotExist(err) {
  48. // simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "文件获取失败")
  49. // c.AbortWithStatus(http.StatusNotFound)
  50. // return
  51. // }
  52. // // 设置 Content-Disposition 响应头,提供默认的文件名
  53. // c.Header("Content-Disposition", "attachment; filename="+filname)
  54. // // 发送文件内容
  55. // c.File(filpath)
  56. // }
  57. func SaveFile(c *gin.Context) {
  58. file := Body{}
  59. err := c.BindJSON(&file)
  60. if err != nil {
  61. simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取参数失败")
  62. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  63. return
  64. }
  65. m := map[string]any{
  66. "type": file.Type,
  67. "data": file.Data,
  68. }
  69. marshal, err := sonic.Marshal(m)
  70. if err != nil {
  71. simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "序列化失败")
  72. e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
  73. return
  74. }
  75. result, err := global.Rdb.Exists(context.Background(), file.Name).Result()
  76. if result == 1 {
  77. e.ResponseWithMsg(c, e.AlreadyExists, e.AlreadyExists.GetMsg())
  78. return
  79. }
  80. set := global.Rdb.Set(context.Background(), file.Name, marshal, 0)
  81. if set.Err() != nil {
  82. simple_zap.WithCtx(context.Background()).Sugar().Warn(set.Err(), "保存文件失败")
  83. e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
  84. return
  85. }
  86. e.ResponseSuccess(c, e.SUCCESS)
  87. }
  88. // TemplateItem 用于获取所有的模板项
  89. // 参数:
  90. // - c *gin.Context: Gin框架的上下文对象,用于处理HTTP请求和响应
  91. // 返回值:
  92. // - 无
  93. func TemplateItem(c *gin.Context) {
  94. type Types struct {
  95. Type bool `json:"type"`
  96. }
  97. t := &Types{}
  98. c.BindJSON(&t)
  99. var allKeys []string // 存储所有检索到的模板项的键名
  100. var cursor uint64 = 0 // 用于分页查询的游标
  101. // 循环检索模板项,直到遍历完所有项或出现错误
  102. for {
  103. keys, newCursor, err := global.Rdb.Scan(context.Background(), cursor, "*", 100).Result() // 每次检索最多100个键名
  104. if err != nil {
  105. // 记录日志并返回错误信息
  106. simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取模板失败")
  107. e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
  108. return
  109. }
  110. allKeys = append(allKeys, keys...) // 将检索到的键名添加到allKeys列表
  111. cursor = newCursor // 更新游标
  112. if cursor == 0 {
  113. break // 如果游标为0,表示已遍历完所有项,退出循环
  114. }
  115. }
  116. m := make(map[string]any) // 创建一个map用于存储模板项的键名和对应的值
  117. // 遍历所有键名,获取其对应的值
  118. for _, key := range allKeys {
  119. result, err := global.Rdb.Get(context.Background(), key).Result()
  120. if err != nil {
  121. // 记录日志并返回错误信息
  122. simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取模板失败")
  123. e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
  124. return
  125. }
  126. file := model.Root{}
  127. //marshal, err := sonic.Marshal(result)
  128. err = sonic.Unmarshal([]byte(result), &file)
  129. if err != nil {
  130. // 记录日志并返回错误信息
  131. simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "序列化失败")
  132. e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
  133. return
  134. }
  135. if t.Type == file.Type {
  136. m[key] = result // 将键名和对应的值添加到map中
  137. }
  138. }
  139. // 返回所有模板项的键值对
  140. e.ResponseSuccess(c, m)
  141. }
  142. // GetTemplate 从服务器获取模板
  143. func GetTemplate(c *gin.Context) {
  144. name := c.Query("name")
  145. if name == "" {
  146. e.ResponseWithMsg(c, e.ERROR, "参数错误")
  147. return
  148. }
  149. result, err := global.Rdb.Get(context.Background(), name).Result()
  150. if err != nil {
  151. simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取文件失败")
  152. e.ResponseWithMsg(c, e.ERROR, "获取文件失败")
  153. return
  154. }
  155. e.ResponseSuccess(c, result)
  156. }
  157. func DeleteTemplate(c *gin.Context) {
  158. name := c.Query("name")
  159. if name == "" {
  160. e.ResponseWithMsg(c, e.ERROR, "参数错误")
  161. return
  162. }
  163. err := global.Rdb.Del(context.Background(), name).Err()
  164. if err != nil {
  165. simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "删除文件失败")
  166. e.ResponseWithMsg(c, e.ERROR, "删除文件失败")
  167. return
  168. }
  169. e.ResponseSuccess(c, e.SUCCESS)
  170. }
  171. //// SaveUsernameToLocalStorage 保存用户名到本地
  172. //func SaveUsernameToLocalStorage(c *gin.Context) {
  173. // usernam := c.PostForm("username")
  174. // simple_zap.WithCtx(context.Background()).Sugar().Info(usernam)
  175. // global.Rdb.Set(context.Background(), "username", usernam, 0)
  176. // e.ResponseSuccess(c, e.SUCCESS)
  177. //}
  178. //
  179. //// SerializedItems 保存素材到redis数据库
  180. //func SerializedItems(c *gin.Context) {
  181. // type Params struct {
  182. // Params string
  183. // }
  184. // serializedItem := Params{}
  185. // err := c.BindJSON(&serializedItem)
  186. // if err != nil {
  187. // simple_zap.WithCtx(context.Background()).Sugar().Warn("获取参数失败")
  188. // }
  189. // fmt.Println("serializedItems:", serializedItem.Params)
  190. // global.Rdb.Set(context.Background(), "serializedItems", serializedItem.Params, 0)
  191. // e.ResponseSuccess(c, e.SUCCESS)
  192. //}
  193. //// GetSerializedItems 从redis数据库获取素材信息
  194. //func GetSerializedItems(c *gin.Context) {
  195. // get := global.Rdb.Get(context.Background(), "serializedItems")
  196. // fmt.Println("GetserializedItems:", get.Val())
  197. // e.ResponseSuccess(c, get.Val())
  198. //}