upload.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package v1
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "net/http"
  6. "property-company-gateway/errors"
  7. "property-company-gateway/utils"
  8. param_v1 "property-company-gateway/param/v1"
  9. "git.getensh.com/common/gopkgs/tasker/httptasker"
  10. )
  11. // UploadFile godoc
  12. // @Summary 上传文件
  13. // @Description 上传文件
  14. // @Tags upload
  15. // @Accept json
  16. // @Produce json
  17. // @Param token header string true "jwt token"
  18. // @Param file formData file false "file"
  19. // @Success 200 {object} v1.UploadResponse
  20. // @Failure 500 {object} base.HTTPError
  21. // @Router /api/v1/upload [post]
  22. func (c Controller) Upload(ctx *gin.Context) {
  23. // 解析参数
  24. parseParamTask := func() error {
  25. file, err := ctx.FormFile("file")
  26. if err != nil {
  27. fmt.Printf("upload get file:%v\n", err)
  28. return errors.SystemError
  29. }
  30. imgMime := file.Header.Get("Content-Type")
  31. dst := file.Filename
  32. f, err := file.Open()
  33. if err != nil {
  34. fmt.Printf("upload file open error:%v\n", err)
  35. return err
  36. }
  37. defer f.Close()
  38. url, err := utils.UploadToMinio(dst, f, file.Size, imgMime)
  39. if err != nil {
  40. fmt.Printf("upload to minio error:%v\n", err)
  41. return err
  42. }
  43. // 响应数据
  44. resp := &param_v1.UploadResponse{}
  45. resp.Data = url
  46. ctx.JSON(http.StatusOK, resp)
  47. return nil
  48. }
  49. httptasker.Exec(ctx, parseParamTask)
  50. }
  51. // UploadFile godoc
  52. // @Summary 上传文件
  53. // @Description 上传文件
  54. // @Tags upload
  55. // @Accept json
  56. // @Produce json
  57. // @Param token header string true "jwt token"
  58. // @Param file formData file false "file"
  59. // @Success 200 {object} v1.UploadWxResponse
  60. // @Failure 500 {object} base.HTTPError
  61. // @Router /api/v1/upload_wx [post]
  62. func (c Controller) UploadWx(ctx *gin.Context) {
  63. // 解析参数
  64. parseParamTask := func() error {
  65. file, err := ctx.FormFile("file")
  66. if err != nil {
  67. fmt.Printf("upload get file:%v\n", err)
  68. return errors.SystemError
  69. }
  70. imgMime := file.Header.Get("Content-Type")
  71. dst := file.Filename
  72. f, err := file.Open()
  73. if err != nil {
  74. fmt.Printf("upload file open error:%v\n", err)
  75. return err
  76. }
  77. defer f.Close()
  78. url, err := utils.UploadToMinio(dst, f, file.Size, imgMime)
  79. if err != nil {
  80. fmt.Printf("upload to minio error:%v\n", err)
  81. return err
  82. }
  83. f.Seek(0, 0)
  84. mediaid, err := utils.WxUpload(f, file.Filename, utils.GetObjName(url))
  85. if err != nil {
  86. return err
  87. }
  88. // 响应数据
  89. resp := &param_v1.UploadWxResponse{}
  90. resp.Data.MediaId = mediaid
  91. resp.Data.Url = url
  92. ctx.JSON(http.StatusOK, resp)
  93. return nil
  94. }
  95. httptasker.Exec(ctx, parseParamTask)
  96. }