upload.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package v1
  4. import (
  5. "github.com/gin-gonic/gin"
  6. "github.com/jaryhe/gopkgs/tasker/httptasker"
  7. "net/http"
  8. "smart-enterprise-management-gateway/errors"
  9. param_v1 "smart-enterprise-management-gateway/param/v1"
  10. "smart-enterprise-management-gateway/utils"
  11. )
  12. // UploadFile godoc
  13. // @Summary 上传文件
  14. // @Description 上传文件
  15. // @Tags upload,v1.0
  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.0/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. }