resource.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package controllers
  2. import (
  3. "eta/eta_forum_admin/models"
  4. "eta/eta_forum_admin/models/system"
  5. "eta/eta_forum_admin/services"
  6. "eta/eta_forum_admin/utils"
  7. "github.com/h2non/filetype"
  8. "io/ioutil"
  9. "os"
  10. "path"
  11. "time"
  12. )
  13. type ResourceController struct {
  14. BaseAuthController
  15. }
  16. // @Title 图片上传
  17. // @Description 图片上传接口
  18. // @Param File query file true "文件"
  19. // @Success 200 新增成功
  20. // @router /image/upload [post]
  21. func (this *ResourceController) ImageUpload() {
  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 + "image/" + 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. // 上传到阿里云
  87. ossDir := utils.UploadDir + "images/"
  88. savePath := ossDir + time.Now().Format("200601/20060102/") + fileName
  89. resourceUrl, err = ossClient.UploadFile(fileName, fpath, savePath)
  90. if err != nil {
  91. br.Msg = "文件上传失败"
  92. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  93. return
  94. }
  95. resp := new(system.ResourceResp)
  96. resp.ResourceUrl = resourceUrl
  97. br.Msg = "上传成功"
  98. br.Ret = 200
  99. br.Success = true
  100. br.Data = resp
  101. return
  102. }
  103. // @Title 获取STSToken
  104. // @Description 获取STSToken
  105. // @Success 200 获取成功
  106. // @router /oss/get_sts_token [get]
  107. func (this *ResourceController) OssSTSToken() {
  108. br := new(models.BaseResponse).Init()
  109. defer func() {
  110. this.Data["json"] = br
  111. this.ServeJSON()
  112. }()
  113. ossClient := services.NewOssClient()
  114. if ossClient == nil {
  115. br.Msg = "上传失败"
  116. br.ErrMsg = "初始化OSS服务失败"
  117. return
  118. }
  119. resp, e := ossClient.GetUploadToken()
  120. if e != nil {
  121. br.Msg = "获取失败"
  122. br.ErrMsg = "获取OSS上传Token失败, Err: " + e.Error()
  123. return
  124. }
  125. br.Data = resp
  126. br.Msg = "获取成功"
  127. br.Ret = 200
  128. br.Success = true
  129. //source, _ := this.GetInt("StorageSource")
  130. //
  131. //if source == utils.STORAGESOURCE_OSS {
  132. // resp, err := services.GetOssSTSToken()
  133. // if err != nil {
  134. // br.Msg = "获取失败"
  135. // br.ErrMsg = "获取STSToken失败, Err: " + err.Error()
  136. // return
  137. // }
  138. // br.Data = resp
  139. // br.Msg = "获取成功"
  140. // br.Ret = 200
  141. // br.Success = true
  142. //} else if source == utils.STORAGESOURCE_MINIO {
  143. // resp, err := services.GetMinIOSTSToken()
  144. // if err != nil {
  145. // br.Msg = "获取失败"
  146. // br.ErrMsg = "获取STSToken失败, Err: " + err.Error()
  147. // return
  148. // }
  149. // br.Data = resp
  150. // br.Msg = "获取成功"
  151. // br.Ret = 200
  152. // br.Success = true
  153. //}
  154. }