123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- package v1
- import (
- "fmt"
- "git.getensh.com/common/gopkgs/tasker/httptasker"
- "github.com/gin-gonic/gin"
- "net/http"
- "property-household-gateway/errors"
- param_v1 "property-household-gateway/param/v1"
- "property-household-gateway/parser"
- "property-household-gateway/utils"
- )
- // UploadFile godoc
- // @Summary 上传文件
- // @Description 上传文件
- // @Tags upload
- // @Accept json
- // @Produce json
- // @Param token header string true "jwt token"
- // @Param file formData file false "file"
- // @Success 200 {object} v1.UploadResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/upload [post]
- func (c Controller) Upload(ctx *gin.Context) {
- // 解析参数
- parseParamTask := func() error {
- file, err := ctx.FormFile("file")
- if err != nil {
- fmt.Printf("upload get file:%v\n", err)
- return errors.SystemError
- }
- imgMime := file.Header.Get("Content-Type")
- dst := file.Filename
- f, err := file.Open()
- if err != nil {
- fmt.Printf("upload file open error:%v\n", err)
- return err
- }
- defer f.Close()
- url, err := utils.UploadToMinio(dst, f, file.Size, imgMime)
- if err != nil {
- fmt.Printf("upload to minio error:%v\n", err)
- return err
- }
- // 响应数据
- resp := ¶m_v1.UploadResponse{}
- resp.Data = url
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- httptasker.Exec(ctx, parseParamTask)
- }
- // UploadFile godoc
- // @Summary 上传文件(openim聊天专用)
- // @Description 上传文件(openim聊天专用)
- // @Tags upload
- // @Accept json
- // @Produce json
- // @Param token header string true "jwt token"
- // @Param file formData file false "file"
- // @Success 200 {object} v1.UploadResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/openim_upload [post]
- func (c Controller) OpenimUpload(ctx *gin.Context) {
- // 解析参数
- parseParamTask := func() error {
- file, err := ctx.FormFile("file")
- if err != nil {
- fmt.Printf("upload get file:%v\n", err)
- return errors.SystemError
- }
- imgMime := file.Header.Get("Content-Type")
- dst := file.Filename
- f, err := file.Open()
- if err != nil {
- fmt.Printf("upload file open error:%v\n", err)
- return err
- }
- defer f.Close()
- url, err := utils.UploadToMinioNew(dst, f, file.Size, imgMime, parser.Conf.Oss.OpenimBucket, false)
- if err != nil {
- fmt.Printf("upload to minio error:%v\n", err)
- return err
- }
- // 响应数据
- resp := ¶m_v1.UploadResponse{}
- resp.Data = url
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- httptasker.Exec(ctx, parseParamTask)
- }
|