upload.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package v1
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "net/http"
  6. "property-applete-gateway/errors"
  7. "property-applete-gateway/utils"
  8. param_v1 "property-applete-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. }