gin.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package common
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "mime"
  7. "mime/multipart"
  8. "net/http"
  9. "net/url"
  10. "strings"
  11. "time"
  12. "github.com/QuantumNous/new-api/constant"
  13. "github.com/pkg/errors"
  14. "github.com/gin-gonic/gin"
  15. )
  16. const KeyRequestBody = "key_request_body"
  17. var ErrRequestBodyTooLarge = errors.New("request body too large")
  18. func IsRequestBodyTooLargeError(err error) bool {
  19. if err == nil {
  20. return false
  21. }
  22. if errors.Is(err, ErrRequestBodyTooLarge) {
  23. return true
  24. }
  25. var mbe *http.MaxBytesError
  26. return errors.As(err, &mbe)
  27. }
  28. func GetRequestBody(c *gin.Context) ([]byte, error) {
  29. cached, exists := c.Get(KeyRequestBody)
  30. if exists && cached != nil {
  31. if b, ok := cached.([]byte); ok {
  32. return b, nil
  33. }
  34. }
  35. maxMB := constant.MaxRequestBodyMB
  36. if maxMB < 0 {
  37. // no limit
  38. body, err := io.ReadAll(c.Request.Body)
  39. _ = c.Request.Body.Close()
  40. if err != nil {
  41. return nil, err
  42. }
  43. c.Set(KeyRequestBody, body)
  44. return body, nil
  45. }
  46. maxBytes := int64(maxMB) << 20
  47. limited := io.LimitReader(c.Request.Body, maxBytes+1)
  48. body, err := io.ReadAll(limited)
  49. if err != nil {
  50. _ = c.Request.Body.Close()
  51. if IsRequestBodyTooLargeError(err) {
  52. return nil, errors.Wrap(ErrRequestBodyTooLarge, fmt.Sprintf("request body exceeds %d MB", maxMB))
  53. }
  54. return nil, err
  55. }
  56. _ = c.Request.Body.Close()
  57. if int64(len(body)) > maxBytes {
  58. return nil, errors.Wrap(ErrRequestBodyTooLarge, fmt.Sprintf("request body exceeds %d MB", maxMB))
  59. }
  60. c.Set(KeyRequestBody, body)
  61. return body, nil
  62. }
  63. func UnmarshalBodyReusable(c *gin.Context, v any) error {
  64. requestBody, err := GetRequestBody(c)
  65. if err != nil {
  66. return err
  67. }
  68. //if DebugEnabled {
  69. // println("UnmarshalBodyReusable request body:", string(requestBody))
  70. //}
  71. contentType := c.Request.Header.Get("Content-Type")
  72. if strings.HasPrefix(contentType, "application/json") {
  73. err = Unmarshal(requestBody, v)
  74. } else if strings.Contains(contentType, gin.MIMEPOSTForm) {
  75. err = parseFormData(requestBody, v)
  76. } else if strings.Contains(contentType, gin.MIMEMultipartPOSTForm) {
  77. err = parseMultipartFormData(c, requestBody, v)
  78. } else {
  79. // skip for now
  80. // TODO: someday non json request have variant model, we will need to implementation this
  81. }
  82. if err != nil {
  83. return err
  84. }
  85. // Reset request body
  86. c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
  87. return nil
  88. }
  89. func SetContextKey(c *gin.Context, key constant.ContextKey, value any) {
  90. c.Set(string(key), value)
  91. }
  92. func GetContextKey(c *gin.Context, key constant.ContextKey) (any, bool) {
  93. return c.Get(string(key))
  94. }
  95. func GetContextKeyString(c *gin.Context, key constant.ContextKey) string {
  96. return c.GetString(string(key))
  97. }
  98. func GetContextKeyInt(c *gin.Context, key constant.ContextKey) int {
  99. return c.GetInt(string(key))
  100. }
  101. func GetContextKeyBool(c *gin.Context, key constant.ContextKey) bool {
  102. return c.GetBool(string(key))
  103. }
  104. func GetContextKeyStringSlice(c *gin.Context, key constant.ContextKey) []string {
  105. return c.GetStringSlice(string(key))
  106. }
  107. func GetContextKeyStringMap(c *gin.Context, key constant.ContextKey) map[string]any {
  108. return c.GetStringMap(string(key))
  109. }
  110. func GetContextKeyTime(c *gin.Context, key constant.ContextKey) time.Time {
  111. return c.GetTime(string(key))
  112. }
  113. func GetContextKeyType[T any](c *gin.Context, key constant.ContextKey) (T, bool) {
  114. if value, ok := c.Get(string(key)); ok {
  115. if v, ok := value.(T); ok {
  116. return v, true
  117. }
  118. }
  119. var t T
  120. return t, false
  121. }
  122. func ApiError(c *gin.Context, err error) {
  123. c.JSON(http.StatusOK, gin.H{
  124. "success": false,
  125. "message": err.Error(),
  126. })
  127. }
  128. func ApiErrorMsg(c *gin.Context, msg string) {
  129. c.JSON(http.StatusOK, gin.H{
  130. "success": false,
  131. "message": msg,
  132. })
  133. }
  134. func ApiSuccess(c *gin.Context, data any) {
  135. c.JSON(http.StatusOK, gin.H{
  136. "success": true,
  137. "message": "",
  138. "data": data,
  139. })
  140. }
  141. func ParseMultipartFormReusable(c *gin.Context) (*multipart.Form, error) {
  142. requestBody, err := GetRequestBody(c)
  143. if err != nil {
  144. return nil, err
  145. }
  146. contentType := c.Request.Header.Get("Content-Type")
  147. boundary, err := parseBoundary(contentType)
  148. if err != nil {
  149. return nil, err
  150. }
  151. reader := multipart.NewReader(bytes.NewReader(requestBody), boundary)
  152. form, err := reader.ReadForm(multipartMemoryLimit())
  153. if err != nil {
  154. return nil, err
  155. }
  156. // Reset request body
  157. c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
  158. return form, nil
  159. }
  160. func processFormMap(formMap map[string]any, v any) error {
  161. jsonData, err := Marshal(formMap)
  162. if err != nil {
  163. return err
  164. }
  165. err = Unmarshal(jsonData, v)
  166. if err != nil {
  167. return err
  168. }
  169. return nil
  170. }
  171. func parseFormData(data []byte, v any) error {
  172. values, err := url.ParseQuery(string(data))
  173. if err != nil {
  174. return err
  175. }
  176. formMap := make(map[string]any)
  177. for key, vals := range values {
  178. if len(vals) == 1 {
  179. formMap[key] = vals[0]
  180. } else {
  181. formMap[key] = vals
  182. }
  183. }
  184. return processFormMap(formMap, v)
  185. }
  186. func parseMultipartFormData(c *gin.Context, data []byte, v any) error {
  187. contentType := c.Request.Header.Get("Content-Type")
  188. boundary, err := parseBoundary(contentType)
  189. if err != nil {
  190. if errors.Is(err, errBoundaryNotFound) {
  191. return Unmarshal(data, v) // Fallback to JSON
  192. }
  193. return err
  194. }
  195. reader := multipart.NewReader(bytes.NewReader(data), boundary)
  196. form, err := reader.ReadForm(multipartMemoryLimit())
  197. if err != nil {
  198. return err
  199. }
  200. defer form.RemoveAll()
  201. formMap := make(map[string]any)
  202. for key, vals := range form.Value {
  203. if len(vals) == 1 {
  204. formMap[key] = vals[0]
  205. } else {
  206. formMap[key] = vals
  207. }
  208. }
  209. return processFormMap(formMap, v)
  210. }
  211. var errBoundaryNotFound = errors.New("multipart boundary not found")
  212. // parseBoundary extracts the multipart boundary from the Content-Type header using mime.ParseMediaType
  213. func parseBoundary(contentType string) (string, error) {
  214. if contentType == "" {
  215. return "", errBoundaryNotFound
  216. }
  217. // Boundary-UUID / boundary-------xxxxxx
  218. _, params, err := mime.ParseMediaType(contentType)
  219. if err != nil {
  220. return "", err
  221. }
  222. boundary, ok := params["boundary"]
  223. if !ok || boundary == "" {
  224. return "", errBoundaryNotFound
  225. }
  226. return boundary, nil
  227. }
  228. // multipartMemoryLimit returns the configured multipart memory limit in bytes
  229. func multipartMemoryLimit() int64 {
  230. limitMB := constant.MaxFileDownloadMB
  231. if limitMB <= 0 {
  232. limitMB = 32
  233. }
  234. return int64(limitMB) << 20
  235. }