upload.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2019 githup.com. All rights reserved.
  2. // Use of this source code is governed by githup.com.
  3. package v1
  4. import (
  5. "net/http"
  6. "smart-site-management-gateway/errors"
  7. param_v1 "smart-site-management-gateway/param/v1"
  8. "smart-site-management-gateway/utils"
  9. "github.com/gin-gonic/gin"
  10. "github.com/jaryhe/gopkgs/tasker/httptasker"
  11. )
  12. // UploadFile godoc
  13. // @Summary 上传文件
  14. // @Description 上传文件
  15. // @Tags 上传文件
  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. 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. return err
  35. }
  36. defer f.Close()
  37. url, err := utils.UploadToMinio(dst, f, file.Size, imgMime)
  38. if err != nil {
  39. return err
  40. }
  41. // 响应数据
  42. resp := &param_v1.UploadResponse{}
  43. resp.Data = url
  44. ctx.JSON(http.StatusOK, resp)
  45. return nil
  46. }
  47. httptasker.Exec(ctx, parseParamTask)
  48. }