binding_context.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package binding_context
  2. import (
  3. "git.sxidc.com/service-supports/fserr"
  4. "git.sxidc.com/service-supports/fslog"
  5. "github.com/gin-gonic/gin"
  6. "io"
  7. "lcp/errcode"
  8. "lcp/utils"
  9. "mime/multipart"
  10. )
  11. type Context struct {
  12. *gin.Context
  13. }
  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 utils.IsStringEmpty(paramValue) {
  19. return nil, fserr.WithCode(nil, errcode.ErrCommon, fserr.MsgOption("没有传递路径参数"+paramName))
  20. }
  21. pathParamValues = append(pathParamValues, paramValue)
  22. }
  23. return pathParamValues, nil
  24. }
  25. func (c *Context) GetFileHeaderBytes(fileHeader *multipart.FileHeader) (string, []byte, error) {
  26. file, err := fileHeader.Open()
  27. if err != nil {
  28. return "", nil, err
  29. }
  30. defer func(file multipart.File) {
  31. err := file.Close()
  32. if err != nil {
  33. fslog.Error(err)
  34. return
  35. }
  36. }(file)
  37. contentBytes, err := io.ReadAll(file)
  38. if err != nil {
  39. return "", nil, err
  40. }
  41. return fileHeader.Filename, contentBytes, nil
  42. }