banner.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package controllers
  2. import (
  3. "eta/eta_api/models"
  4. "eta/eta_api/services"
  5. "eta/eta_api/utils"
  6. "os"
  7. "path"
  8. "time"
  9. )
  10. // banner
  11. type BannerController struct {
  12. BaseAuthController
  13. }
  14. // @Title 图片上传
  15. // @Description 图片上传接口
  16. // @Param File query file true "文件"
  17. // @Success 200 新增成功
  18. // @router /upload [post]
  19. func (this *BannerController) Upload() {
  20. br := new(models.BaseResponse).Init()
  21. defer func() {
  22. this.Data["json"] = br
  23. this.ServeJSON()
  24. }()
  25. f, h, err := this.GetFile("file")
  26. if err != nil {
  27. br.Msg = "获取资源信息失败"
  28. br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
  29. return
  30. }
  31. ext := path.Ext(h.Filename)
  32. dateDir := time.Now().Format("20060102")
  33. uploadDir := utils.STATIC_DIR + "hongze/" + dateDir
  34. err = os.MkdirAll(uploadDir, utils.DIR_MOD)
  35. if err != nil {
  36. br.Msg = "存储目录创建失败"
  37. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  38. return
  39. }
  40. randStr := utils.GetRandStringNoSpecialChar(28)
  41. fileName := randStr + ext
  42. fpath := uploadDir + "/" + fileName
  43. defer f.Close() //关闭上传文件
  44. err = this.SaveToFile("file", fpath)
  45. if err != nil {
  46. br.Msg = "文件上传失败"
  47. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  48. return
  49. }
  50. resourceUrl := ``
  51. //resourceUrl, err = services.UploadImgToMinIoTest(fileName, fpath)
  52. //if err != nil {
  53. // br.Msg = "文件上传失败"
  54. // br.ErrMsg = "文件上传失败,Err:" + err.Error()
  55. // return
  56. //}
  57. ossClient := services.NewOssClient()
  58. if ossClient == nil {
  59. br.Msg = "上传失败"
  60. br.ErrMsg = "初始化OSS服务失败"
  61. return
  62. }
  63. resourceUrl, err = ossClient.UploadFile(fileName, fpath, "")
  64. if err != nil {
  65. br.Msg = "文件上传失败"
  66. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  67. return
  68. }
  69. ////上传到阿里云 和 minio
  70. //if utils.ObjectStorageClient == "minio" {
  71. // resourceUrl, err = services.UploadImgToMinIo(fileName, fpath)
  72. // if err != nil {
  73. // br.Msg = "文件上传失败"
  74. // br.ErrMsg = "文件上传失败,Err:" + err.Error()
  75. // return
  76. // }
  77. //} else {
  78. // resourceUrl, err = services.UploadAliyunV2(fileName, fpath)
  79. // if err != nil {
  80. // br.Msg = "文件上传失败"
  81. // br.ErrMsg = "文件上传失败,Err:" + err.Error()
  82. // return
  83. // }
  84. //}
  85. defer func() {
  86. os.Remove(fpath)
  87. }()
  88. item := new(models.Resource)
  89. item.ResourceUrl = resourceUrl
  90. item.ResourceType = 1
  91. item.CreateTime = time.Now()
  92. newId, err := models.AddResource(item)
  93. if err != nil {
  94. br.Msg = "资源上传失败"
  95. br.ErrMsg = "资源上传失败,Err:" + err.Error()
  96. return
  97. }
  98. resp := new(models.ResourceResp)
  99. resp.Id = newId
  100. resp.ResourceUrl = resourceUrl
  101. br.Msg = "上传成功"
  102. br.Ret = 200
  103. br.Success = true
  104. br.Data = resp
  105. return
  106. }