banner.go 2.6 KB

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