upload.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. "github.com/gin-gonic/gin"
  6. "github.com/jaryhe/gopkgs/tasker/httptasker"
  7. "net/http"
  8. "smart-supplier-management-gateway/errors"
  9. param_v1 "smart-supplier-management-gateway/param/v1"
  10. "smart-supplier-management-gateway/utils"
  11. )
  12. // UploadFile godoc
  13. // @Summary 上传文件
  14. // @Description 上传文件
  15. // @Tags upload
  16. // @Accept json
  17. // @Produce json
  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. return errors.SystemError
  28. }
  29. imgMime :=file.Header.Get("Content-Type")
  30. dst := file.Filename
  31. f, err := file.Open()
  32. if err != nil {
  33. return err
  34. }
  35. defer f.Close()
  36. url, err := utils.UploadToMinio(dst, f, file.Size, imgMime)
  37. if err != nil {
  38. return err
  39. }
  40. // 响应数据
  41. resp := &param_v1.UploadResponse{}
  42. resp.Data = url
  43. ctx.JSON(http.StatusOK, resp)
  44. return nil
  45. }
  46. httptasker.Exec(ctx, parseParamTask)
  47. }