upload.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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-household-gateway/errors"
  8. param_v1 "property-household-gateway/param/v1"
  9. "property-household-gateway/parser"
  10. "property-household-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. url, err := utils.UploadToMinio(dst, f, file.Size, imgMime)
  40. if err != nil {
  41. fmt.Printf("upload to minio error:%v\n", err)
  42. return err
  43. }
  44. // 响应数据
  45. resp := &param_v1.UploadResponse{}
  46. resp.Data = url
  47. ctx.JSON(http.StatusOK, resp)
  48. return nil
  49. }
  50. httptasker.Exec(ctx, parseParamTask)
  51. }
  52. // UploadFile godoc
  53. // @Summary 上传文件(openim聊天专用)
  54. // @Description 上传文件(openim聊天专用)
  55. // @Tags upload
  56. // @Accept json
  57. // @Produce json
  58. // @Param token header string true "jwt token"
  59. // @Param file formData file false "file"
  60. // @Success 200 {object} v1.UploadResponse
  61. // @Failure 500 {object} base.HTTPError
  62. // @Router /api/v1/openim_upload [post]
  63. func (c Controller) OpenimUpload(ctx *gin.Context) {
  64. // 解析参数
  65. parseParamTask := func() error {
  66. file, err := ctx.FormFile("file")
  67. if err != nil {
  68. fmt.Printf("upload get file:%v\n", err)
  69. return errors.SystemError
  70. }
  71. imgMime := file.Header.Get("Content-Type")
  72. dst := file.Filename
  73. f, err := file.Open()
  74. if err != nil {
  75. fmt.Printf("upload file open error:%v\n", err)
  76. return err
  77. }
  78. defer f.Close()
  79. url, err := utils.UploadToMinioNew(dst, f, file.Size, imgMime, parser.Conf.Oss.OpenimBucket, false)
  80. if err != nil {
  81. fmt.Printf("upload to minio error:%v\n", err)
  82. return err
  83. }
  84. // 响应数据
  85. resp := &param_v1.UploadResponse{}
  86. resp.Data = url
  87. ctx.JSON(http.StatusOK, resp)
  88. return nil
  89. }
  90. httptasker.Exec(ctx, parseParamTask)
  91. }