2
0

gin.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. const KeyBodyStorage = "key_body_storage"
  18. var ErrRequestBodyTooLarge = errors.New("request body too large")
  19. func IsRequestBodyTooLargeError(err error) bool {
  20. if err == nil {
  21. return false
  22. }
  23. if errors.Is(err, ErrRequestBodyTooLarge) {
  24. return true
  25. }
  26. var mbe *http.MaxBytesError
  27. return errors.As(err, &mbe)
  28. }
  29. func GetRequestBody(c *gin.Context) ([]byte, error) {
  30. // 首先检查是否有 BodyStorage 缓存
  31. if storage, exists := c.Get(KeyBodyStorage); exists && storage != nil {
  32. if bs, ok := storage.(BodyStorage); ok {
  33. if _, err := bs.Seek(0, io.SeekStart); err != nil {
  34. return nil, fmt.Errorf("failed to seek body storage: %w", err)
  35. }
  36. return bs.Bytes()
  37. }
  38. }
  39. // 检查旧的缓存方式
  40. cached, exists := c.Get(KeyRequestBody)
  41. if exists && cached != nil {
  42. if b, ok := cached.([]byte); ok {
  43. return b, nil
  44. }
  45. }
  46. maxMB := constant.MaxRequestBodyMB
  47. if maxMB <= 0 {
  48. maxMB = 128 // 默认 128MB
  49. }
  50. maxBytes := int64(maxMB) << 20
  51. contentLength := c.Request.ContentLength
  52. // 使用新的存储系统
  53. storage, err := CreateBodyStorageFromReader(c.Request.Body, contentLength, maxBytes)
  54. _ = c.Request.Body.Close()
  55. if err != nil {
  56. if IsRequestBodyTooLargeError(err) {
  57. return nil, errors.Wrap(ErrRequestBodyTooLarge, fmt.Sprintf("request body exceeds %d MB", maxMB))
  58. }
  59. return nil, err
  60. }
  61. // 缓存存储对象
  62. c.Set(KeyBodyStorage, storage)
  63. // 获取字节数据
  64. body, err := storage.Bytes()
  65. if err != nil {
  66. return nil, err
  67. }
  68. // 同时设置旧的缓存键以保持兼容性
  69. c.Set(KeyRequestBody, body)
  70. return body, nil
  71. }
  72. // GetBodyStorage 获取请求体存储对象(用于需要多次读取的场景)
  73. func GetBodyStorage(c *gin.Context) (BodyStorage, error) {
  74. // 检查是否已有存储
  75. if storage, exists := c.Get(KeyBodyStorage); exists && storage != nil {
  76. if bs, ok := storage.(BodyStorage); ok {
  77. if _, err := bs.Seek(0, io.SeekStart); err != nil {
  78. return nil, fmt.Errorf("failed to seek body storage: %w", err)
  79. }
  80. return bs, nil
  81. }
  82. }
  83. // 如果没有,调用 GetRequestBody 创建存储
  84. _, err := GetRequestBody(c)
  85. if err != nil {
  86. return nil, err
  87. }
  88. // 再次获取存储
  89. if storage, exists := c.Get(KeyBodyStorage); exists && storage != nil {
  90. if bs, ok := storage.(BodyStorage); ok {
  91. if _, err := bs.Seek(0, io.SeekStart); err != nil {
  92. return nil, fmt.Errorf("failed to seek body storage: %w", err)
  93. }
  94. return bs, nil
  95. }
  96. }
  97. return nil, errors.New("failed to get body storage")
  98. }
  99. // CleanupBodyStorage 清理请求体存储(应在请求结束时调用)
  100. func CleanupBodyStorage(c *gin.Context) {
  101. if storage, exists := c.Get(KeyBodyStorage); exists && storage != nil {
  102. if bs, ok := storage.(BodyStorage); ok {
  103. bs.Close()
  104. }
  105. c.Set(KeyBodyStorage, nil)
  106. }
  107. }
  108. func UnmarshalBodyReusable(c *gin.Context, v any) error {
  109. requestBody, err := GetRequestBody(c)
  110. if err != nil {
  111. return err
  112. }
  113. //if DebugEnabled {
  114. // println("UnmarshalBodyReusable request body:", string(requestBody))
  115. //}
  116. contentType := c.Request.Header.Get("Content-Type")
  117. if strings.HasPrefix(contentType, "application/json") {
  118. err = Unmarshal(requestBody, v)
  119. } else if strings.Contains(contentType, gin.MIMEPOSTForm) {
  120. err = parseFormData(requestBody, v)
  121. } else if strings.Contains(contentType, gin.MIMEMultipartPOSTForm) {
  122. err = parseMultipartFormData(c, requestBody, v)
  123. } else {
  124. // skip for now
  125. // TODO: someday non json request have variant model, we will need to implementation this
  126. }
  127. if err != nil {
  128. return err
  129. }
  130. // Reset request body
  131. c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
  132. return nil
  133. }
  134. func SetContextKey(c *gin.Context, key constant.ContextKey, value any) {
  135. c.Set(string(key), value)
  136. }
  137. func GetContextKey(c *gin.Context, key constant.ContextKey) (any, bool) {
  138. return c.Get(string(key))
  139. }
  140. func GetContextKeyString(c *gin.Context, key constant.ContextKey) string {
  141. return c.GetString(string(key))
  142. }
  143. func GetContextKeyInt(c *gin.Context, key constant.ContextKey) int {
  144. return c.GetInt(string(key))
  145. }
  146. func GetContextKeyBool(c *gin.Context, key constant.ContextKey) bool {
  147. return c.GetBool(string(key))
  148. }
  149. func GetContextKeyStringSlice(c *gin.Context, key constant.ContextKey) []string {
  150. return c.GetStringSlice(string(key))
  151. }
  152. func GetContextKeyStringMap(c *gin.Context, key constant.ContextKey) map[string]any {
  153. return c.GetStringMap(string(key))
  154. }
  155. func GetContextKeyTime(c *gin.Context, key constant.ContextKey) time.Time {
  156. return c.GetTime(string(key))
  157. }
  158. func GetContextKeyType[T any](c *gin.Context, key constant.ContextKey) (T, bool) {
  159. if value, ok := c.Get(string(key)); ok {
  160. if v, ok := value.(T); ok {
  161. return v, true
  162. }
  163. }
  164. var t T
  165. return t, false
  166. }
  167. func ApiError(c *gin.Context, err error) {
  168. c.JSON(http.StatusOK, gin.H{
  169. "success": false,
  170. "message": err.Error(),
  171. })
  172. }
  173. func ApiErrorMsg(c *gin.Context, msg string) {
  174. c.JSON(http.StatusOK, gin.H{
  175. "success": false,
  176. "message": msg,
  177. })
  178. }
  179. func ApiSuccess(c *gin.Context, data any) {
  180. c.JSON(http.StatusOK, gin.H{
  181. "success": true,
  182. "message": "",
  183. "data": data,
  184. })
  185. }
  186. func ParseMultipartFormReusable(c *gin.Context) (*multipart.Form, error) {
  187. requestBody, err := GetRequestBody(c)
  188. if err != nil {
  189. return nil, err
  190. }
  191. contentType := c.Request.Header.Get("Content-Type")
  192. boundary, err := parseBoundary(contentType)
  193. if err != nil {
  194. return nil, err
  195. }
  196. reader := multipart.NewReader(bytes.NewReader(requestBody), boundary)
  197. form, err := reader.ReadForm(multipartMemoryLimit())
  198. if err != nil {
  199. return nil, err
  200. }
  201. // Reset request body
  202. c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
  203. return form, nil
  204. }
  205. func processFormMap(formMap map[string]any, v any) error {
  206. jsonData, err := Marshal(formMap)
  207. if err != nil {
  208. return err
  209. }
  210. err = Unmarshal(jsonData, v)
  211. if err != nil {
  212. return err
  213. }
  214. return nil
  215. }
  216. func parseFormData(data []byte, v any) error {
  217. values, err := url.ParseQuery(string(data))
  218. if err != nil {
  219. return err
  220. }
  221. formMap := make(map[string]any)
  222. for key, vals := range values {
  223. if len(vals) == 1 {
  224. formMap[key] = vals[0]
  225. } else {
  226. formMap[key] = vals
  227. }
  228. }
  229. return processFormMap(formMap, v)
  230. }
  231. func parseMultipartFormData(c *gin.Context, data []byte, v any) error {
  232. contentType := c.Request.Header.Get("Content-Type")
  233. boundary, err := parseBoundary(contentType)
  234. if err != nil {
  235. if errors.Is(err, errBoundaryNotFound) {
  236. return Unmarshal(data, v) // Fallback to JSON
  237. }
  238. return err
  239. }
  240. reader := multipart.NewReader(bytes.NewReader(data), boundary)
  241. form, err := reader.ReadForm(multipartMemoryLimit())
  242. if err != nil {
  243. return err
  244. }
  245. defer form.RemoveAll()
  246. formMap := make(map[string]any)
  247. for key, vals := range form.Value {
  248. if len(vals) == 1 {
  249. formMap[key] = vals[0]
  250. } else {
  251. formMap[key] = vals
  252. }
  253. }
  254. return processFormMap(formMap, v)
  255. }
  256. var errBoundaryNotFound = errors.New("multipart boundary not found")
  257. // parseBoundary extracts the multipart boundary from the Content-Type header using mime.ParseMediaType
  258. func parseBoundary(contentType string) (string, error) {
  259. if contentType == "" {
  260. return "", errBoundaryNotFound
  261. }
  262. // Boundary-UUID / boundary-------xxxxxx
  263. _, params, err := mime.ParseMediaType(contentType)
  264. if err != nil {
  265. return "", err
  266. }
  267. boundary, ok := params["boundary"]
  268. if !ok || boundary == "" {
  269. return "", errBoundaryNotFound
  270. }
  271. return boundary, nil
  272. }
  273. // multipartMemoryLimit returns the configured multipart memory limit in bytes
  274. func multipartMemoryLimit() int64 {
  275. limitMB := constant.MaxFileDownloadMB
  276. if limitMB <= 0 {
  277. limitMB = 32
  278. }
  279. return int64(limitMB) << 20
  280. }