| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package api
- import (
- "bytes"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/logger"
- "github.com/gin-gonic/gin"
- "io"
- "mime/multipart"
- "strings"
- )
- type Context struct {
- *gin.Context
- }
- // GetFileHeaderBytes 获取传递的文件名和文件内容
- func (c *Context) GetFileHeaderBytes(fileHeader *multipart.FileHeader) (string, []byte, error) {
- file, err := fileHeader.Open()
- if err != nil {
- return "", nil, err
- }
- defer func(file multipart.File) {
- err := file.Close()
- if err != nil {
- logger.GetInstance().Error(err)
- return
- }
- }(file)
- contentBytes, err := io.ReadAll(file)
- if err != nil {
- return "", nil, err
- }
- return fileHeader.Filename, contentBytes, nil
- }
- func (c *Context) ReadBody() ([]byte, error) {
- if c.Request.Body == nil {
- return make([]byte, 0), nil
- }
- body, err := io.ReadAll(c.Request.Body)
- if err != nil {
- return nil, err
- }
- defer func(Body io.ReadCloser) {
- err := Body.Close()
- if err != nil {
- logger.GetInstance().Error(err)
- return
- }
- }(c.Request.Body)
- c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
- return body, nil
- }
- func (c *Context) GetAllQueryParams() map[string]string {
- queryParams := make(map[string]string, 0)
- for key, value := range c.Request.URL.Query() {
- queryParams[key] = strings.Join(value, ",")
- }
- return queryParams
- }
- func (c *Context) GetAllPathParams() map[string]string {
- pathParams := make(map[string]string, 0)
- for _, params := range c.Params {
- pathParams[params.Key] = params.Value
- }
- return pathParams
- }
|