banner.go 2.8 KB

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