binding_context.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package binding_context
  2. import (
  3. "errors"
  4. "git.sxidc.com/go-tools/utils/strutils"
  5. "git.sxidc.com/service-supports/fslog"
  6. "github.com/gin-gonic/gin"
  7. "io"
  8. "mime/multipart"
  9. )
  10. type Context struct {
  11. *gin.Context
  12. }
  13. // GetPathParams 根据给定的路径参数名字获取参数值
  14. func (c *Context) GetPathParams(pathParamNames []string) ([]string, error) {
  15. pathParamValues := make([]string, 0)
  16. for _, paramName := range pathParamNames {
  17. paramValue := c.Param(paramName)
  18. if strutils.IsStringEmpty(paramValue) {
  19. return nil, errors.New("没有传递路径参数" + paramName)
  20. }
  21. pathParamValues = append(pathParamValues, paramValue)
  22. }
  23. return pathParamValues, nil
  24. }
  25. // GetFileHeaderBytes 获取传递的文件名和文件内容
  26. func (c *Context) GetFileHeaderBytes(fileHeader *multipart.FileHeader) (string, []byte, error) {
  27. file, err := fileHeader.Open()
  28. if err != nil {
  29. return "", nil, err
  30. }
  31. defer func(file multipart.File) {
  32. err := file.Close()
  33. if err != nil {
  34. fslog.Error(err)
  35. return
  36. }
  37. }(file)
  38. contentBytes, err := io.ReadAll(file)
  39. if err != nil {
  40. return "", nil, err
  41. }
  42. return fileHeader.Filename, contentBytes, nil
  43. }