upload.go 2.6 KB

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