file.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package controller
  2. import (
  3. "bytes"
  4. "context"
  5. e2 "file_upload/backend/app/e"
  6. "file_upload/backend/global"
  7. "file_upload/backend/simple_zap"
  8. "file_upload/backend/utils"
  9. "fmt"
  10. "github.com/gin-gonic/gin"
  11. "github.com/go-playground/validator/v10"
  12. "go.mongodb.org/mongo-driver/bson"
  13. "go.mongodb.org/mongo-driver/mongo/options"
  14. "go.uber.org/zap"
  15. "io/ioutil"
  16. "net/http"
  17. "path"
  18. )
  19. type Template struct {
  20. Name string `json:"name" validate:"required"`
  21. Data any `json:"data" validate:"required"`
  22. Type int `json:"type" validate:"required"`
  23. ImageUrl string `json:"imageUrl" validate:"required"`
  24. }
  25. // SaveTemplate 保存文件到MongoDB
  26. // 参数:
  27. // - c *gin.Context: Gin框架的上下文对象,用于处理HTTP请求和响应
  28. // 无返回值
  29. func SaveTemplate(c *gin.Context) {
  30. // 解析请求体中的文件信息
  31. file := Template{}
  32. err := c.BindJSON(&file)
  33. uuid := c.Query("uuid")
  34. if uuid == "" {
  35. uuid = "test"
  36. }
  37. if err != nil {
  38. // 日志记录参数解析失败
  39. simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取参数失败")
  40. // 返回参数解析失败的响应
  41. e2.ResponseWithMsg(c, e2.JSONParsingFailed, e2.JSONParsingFailed.GetMsg())
  42. return
  43. }
  44. // 检查文件是否已存在
  45. filter := bson.M{"name": file.Name}
  46. count, err := global.MongoCon.CountDocuments(context.TODO(), filter)
  47. if err != nil {
  48. // 日志记录查询失败
  49. simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "查询文件是否存在失败")
  50. // 返回错误响应
  51. e2.ResponseWithMsg(c, e2.ERROR, e2.ERROR.GetMsg())
  52. return
  53. }
  54. if count > 0 {
  55. // 返回文件已存在的响应
  56. e2.ResponseWithMsg(c, e2.AlreadyExists, e2.AlreadyExists.GetMsg())
  57. return
  58. }
  59. err = utils.ParseBase64ImageString(file.ImageUrl, file.Name)
  60. if err != nil {
  61. simple_zap.WithCtx(context.TODO()).Sugar().Warn(err, "base64解码失败")
  62. e2.ResponseWithMsg(c, e2.ERROR, "图片转换失败")
  63. panic(err)
  64. return
  65. }
  66. var url = global.DownloadSetting.Imageurl + file.Name + ".png"
  67. doc := bson.M{
  68. "name": file.Name,
  69. "type": file.Type,
  70. "data": file.Data,
  71. "uuid": uuid,
  72. "url": url,
  73. }
  74. // 保存文件到MongoDB
  75. _, err = global.MongoCon.InsertOne(context.TODO(), doc)
  76. if err != nil {
  77. // 日志记录保存文件失败
  78. simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "保存文件失败")
  79. // 返回保存文件失败的响应
  80. e2.ResponseWithMsg(c, e2.ERROR, e2.ERROR.GetMsg())
  81. return
  82. }
  83. // 返回文件保存成功的响应
  84. e2.ResponseSuccess(c, e2.SUCCESS)
  85. }
  86. // TemplateItem 获取所有模板
  87. func TemplateItem(c *gin.Context) {
  88. type Types struct {
  89. Type int `json:"type"`
  90. }
  91. uuid := c.Query("uuid")
  92. if uuid == "undefined" {
  93. uuid = "test"
  94. }
  95. t := &Types{}
  96. c.BindJSON(&t)
  97. filter := bson.M{
  98. "type": t.Type,
  99. "uuid": uuid,
  100. }
  101. var result []bson.M
  102. opts := options.Find()
  103. opts.SetProjection(bson.M{
  104. "_id": 0,
  105. "data": 0,
  106. })
  107. cur, err := global.MongoCon.Find(context.Background(), filter, opts)
  108. if err != nil {
  109. // 记录日志并返回错误信息
  110. simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取模板失败")
  111. e2.ResponseWithMsg(c, e2.ERROR, e2.ERROR.GetMsg())
  112. return
  113. }
  114. err = cur.All(context.Background(), &result)
  115. if err != nil {
  116. // 记录日志并返回错误信息
  117. simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取模板失败")
  118. e2.ResponseWithMsg(c, e2.ERROR, e2.ERROR.GetMsg())
  119. return
  120. }
  121. if result == nil {
  122. e2.ResponseWithMsg(c, e2.ERROR, "没有数据")
  123. return
  124. }
  125. e2.ResponseSuccess(c, result)
  126. }
  127. // GetTemplate 获取模板
  128. func GetTemplate(c *gin.Context) {
  129. name := c.Query("name")
  130. if name == "" {
  131. e2.ResponseWithMsg(c, e2.ERROR, "参数错误")
  132. return
  133. }
  134. var result map[string]interface{}
  135. err := global.MongoCon.FindOne(context.Background(), bson.M{"name": name}).Decode(&result)
  136. if err != nil {
  137. simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取文件失败")
  138. e2.ResponseWithMsg(c, e2.ERROR, "获取文件失败")
  139. return
  140. }
  141. e2.ResponseSuccess(c, result)
  142. }
  143. // DeleteTemplate 删除模板
  144. func DeleteTemplate(c *gin.Context) {
  145. name := c.Query("name")
  146. //uuid := c.Query("uuid")
  147. //if uuid == "" {
  148. // e2.ResponseWithMsg(c, e2.ERROR, "uuid错误")
  149. // return
  150. //}
  151. if name == "" {
  152. e2.ResponseWithMsg(c, e2.ERROR, "参数错误")
  153. return
  154. }
  155. one, err := global.MongoCon.DeleteOne(context.Background(), bson.M{"name": name})
  156. if err != nil {
  157. simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "删除文件失败")
  158. e2.ResponseWithMsg(c, e2.ERROR, "删除文件失败")
  159. return
  160. }
  161. if one.DeletedCount > 0 {
  162. e2.ResponseSuccess(c, one)
  163. return
  164. }
  165. e2.ResponseWithMsg(c, e2.ERROR, "删除文件失败")
  166. }
  167. // SearchTemplate 搜索模板
  168. func SearchTemplate(c *gin.Context) {
  169. query := c.Query("name")
  170. uuid := c.Query("uuid")
  171. if uuid == "" {
  172. uuid = "test"
  173. }
  174. validate := validator.New()
  175. validate.Var("name", "required")
  176. if query == "" {
  177. e2.ResponseWithMsg(c, e2.ERROR, "参数不弄为空")
  178. return
  179. }
  180. //result, err := global.Rdb.Keys(context.Background(), "*"+query+"*").Result()
  181. fileter := bson.M{"name": bson.M{"$regex": query}, "uuid": uuid}
  182. var result []bson.M
  183. find := options.Find()
  184. find.SetProjection(bson.M{
  185. "_id": 0,
  186. "data": 0,
  187. })
  188. cursor, err := global.MongoCon.Find(context.Background(), fileter, find)
  189. if err != nil {
  190. simple_zap.Logger.Error("查询失败", zap.Error(err))
  191. e2.ResponseWithMsg(c, e2.ERROR, "查询失败")
  192. return
  193. }
  194. err = cursor.All(context.Background(), &result)
  195. e2.ResponseSuccess(c, result)
  196. }
  197. // GetImage 获得图片
  198. func GetImage(c *gin.Context) {
  199. typs := c.Query("type")
  200. name := c.Query("filename")
  201. if name == "" || typs != "upload" {
  202. e2.ResponseWithMsg(c, e2.ERROR, "参数错误")
  203. return
  204. }
  205. // 指定图片所在目录的路径
  206. imageDirPath := global.DownloadSetting.Path
  207. // 构建图片文件的完整路径
  208. imageFilePath := fmt.Sprintf("%s/%s", imageDirPath, name)
  209. file, err := ioutil.ReadFile(imageFilePath)
  210. if err != nil {
  211. simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取图片失败")
  212. e2.ResponseWithMsg(c, e2.ERROR, "获取图片失败")
  213. return
  214. }
  215. // 根据文件扩展名确定 Content-Type
  216. ext := path.Ext(name)
  217. contentType := ""
  218. switch ext {
  219. case ".jpg", ".jpeg":
  220. contentType = "image/jpeg"
  221. case ".png":
  222. contentType = "image/png"
  223. // 添加更多类型...
  224. default:
  225. c.AbortWithStatus(http.StatusUnsupportedMediaType)
  226. return
  227. }
  228. c.DataFromReader(http.StatusOK, int64(len(file)), contentType, bytes.NewReader(file), nil)
  229. }