binding_context.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package binding_context
  2. import (
  3. "errors"
  4. "git.sxidc.com/go-tools/api_binding/utils"
  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. func (c *Context) GetPathParams(pathParamNames []string) ([]string, error) {
  14. pathParamValues := make([]string, 0)
  15. for _, paramName := range pathParamNames {
  16. paramValue := c.Param(paramName)
  17. if utils.IsStringEmpty(paramValue) {
  18. return nil, errors.New("没有传递路径参数" + paramName)
  19. }
  20. pathParamValues = append(pathParamValues, paramValue)
  21. }
  22. return pathParamValues, nil
  23. }
  24. func (c *Context) GetFileHeaderBytes(fileHeader *multipart.FileHeader) (string, []byte, error) {
  25. file, err := fileHeader.Open()
  26. if err != nil {
  27. return "", nil, err
  28. }
  29. defer func(file multipart.File) {
  30. err := file.Close()
  31. if err != nil {
  32. fslog.Error(err)
  33. return
  34. }
  35. }(file)
  36. contentBytes, err := io.ReadAll(file)
  37. if err != nil {
  38. return "", nil, err
  39. }
  40. return fileHeader.Filename, contentBytes, nil
  41. }