gin.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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) (io.Seeker, 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, nil
  37. }
  38. }
  39. // 检查旧的缓存方式
  40. cached, exists := c.Get(KeyRequestBody)
  41. if exists && cached != nil {
  42. if b, ok := cached.([]byte); ok {
  43. bs, err := CreateBodyStorage(b)
  44. if err != nil {
  45. return nil, err
  46. }
  47. c.Set(KeyBodyStorage, bs)
  48. return bs, nil
  49. }
  50. }
  51. maxMB := constant.MaxRequestBodyMB
  52. if maxMB <= 0 {
  53. maxMB = 128 // 默认 128MB
  54. }
  55. maxBytes := int64(maxMB) << 20
  56. contentLength := c.Request.ContentLength
  57. // 使用新的存储系统
  58. storage, err := CreateBodyStorageFromReader(c.Request.Body, contentLength, maxBytes)
  59. _ = c.Request.Body.Close()
  60. if err != nil {
  61. if IsRequestBodyTooLargeError(err) {
  62. return nil, errors.Wrap(ErrRequestBodyTooLarge, fmt.Sprintf("request body exceeds %d MB", maxMB))
  63. }
  64. return nil, err
  65. }
  66. // 缓存存储对象
  67. c.Set(KeyBodyStorage, storage)
  68. return storage, nil
  69. }
  70. // GetBodyStorage 获取请求体存储对象(用于需要多次读取的场景)
  71. func GetBodyStorage(c *gin.Context) (BodyStorage, error) {
  72. seeker, err := GetRequestBody(c)
  73. if err != nil {
  74. return nil, err
  75. }
  76. bs, ok := seeker.(BodyStorage)
  77. if !ok {
  78. return nil, errors.New("unexpected body storage type")
  79. }
  80. return bs, nil
  81. }
  82. // CleanupBodyStorage 清理请求体存储(应在请求结束时调用)
  83. func CleanupBodyStorage(c *gin.Context) {
  84. if storage, exists := c.Get(KeyBodyStorage); exists && storage != nil {
  85. if bs, ok := storage.(BodyStorage); ok {
  86. bs.Close()
  87. }
  88. c.Set(KeyBodyStorage, nil)
  89. }
  90. }
  91. func UnmarshalBodyReusable(c *gin.Context, v any) error {
  92. storage, err := GetBodyStorage(c)
  93. if err != nil {
  94. return err
  95. }
  96. requestBody, err := storage.Bytes()
  97. if err != nil {
  98. return err
  99. }
  100. contentType := c.Request.Header.Get("Content-Type")
  101. if strings.HasPrefix(contentType, "application/json") {
  102. err = Unmarshal(requestBody, v)
  103. } else if strings.Contains(contentType, gin.MIMEPOSTForm) {
  104. err = parseFormData(requestBody, v)
  105. } else if strings.Contains(contentType, gin.MIMEMultipartPOSTForm) {
  106. err = parseMultipartFormData(c, requestBody, v)
  107. } else {
  108. // skip for now
  109. // TODO: someday non json request have variant model, we will need to implementation this
  110. }
  111. if err != nil {
  112. return err
  113. }
  114. // Reset request body
  115. if _, seekErr := storage.Seek(0, io.SeekStart); seekErr != nil {
  116. return seekErr
  117. }
  118. c.Request.Body = io.NopCloser(storage)
  119. return nil
  120. }
  121. func SetContextKey(c *gin.Context, key constant.ContextKey, value any) {
  122. c.Set(string(key), value)
  123. }
  124. func GetContextKey(c *gin.Context, key constant.ContextKey) (any, bool) {
  125. return c.Get(string(key))
  126. }
  127. func GetContextKeyString(c *gin.Context, key constant.ContextKey) string {
  128. return c.GetString(string(key))
  129. }
  130. func GetContextKeyInt(c *gin.Context, key constant.ContextKey) int {
  131. return c.GetInt(string(key))
  132. }
  133. func GetContextKeyBool(c *gin.Context, key constant.ContextKey) bool {
  134. return c.GetBool(string(key))
  135. }
  136. func GetContextKeyStringSlice(c *gin.Context, key constant.ContextKey) []string {
  137. return c.GetStringSlice(string(key))
  138. }
  139. func GetContextKeyStringMap(c *gin.Context, key constant.ContextKey) map[string]any {
  140. return c.GetStringMap(string(key))
  141. }
  142. func GetContextKeyTime(c *gin.Context, key constant.ContextKey) time.Time {
  143. return c.GetTime(string(key))
  144. }
  145. func GetContextKeyType[T any](c *gin.Context, key constant.ContextKey) (T, bool) {
  146. if value, ok := c.Get(string(key)); ok {
  147. if v, ok := value.(T); ok {
  148. return v, true
  149. }
  150. }
  151. var t T
  152. return t, false
  153. }
  154. func ApiError(c *gin.Context, err error) {
  155. c.JSON(http.StatusOK, gin.H{
  156. "success": false,
  157. "message": err.Error(),
  158. })
  159. }
  160. func ApiErrorMsg(c *gin.Context, msg string) {
  161. c.JSON(http.StatusOK, gin.H{
  162. "success": false,
  163. "message": msg,
  164. })
  165. }
  166. func ApiSuccess(c *gin.Context, data any) {
  167. c.JSON(http.StatusOK, gin.H{
  168. "success": true,
  169. "message": "",
  170. "data": data,
  171. })
  172. }
  173. // ApiErrorI18n returns a translated error message based on the user's language preference
  174. // key is the i18n message key, args is optional template data
  175. func ApiErrorI18n(c *gin.Context, key string, args ...map[string]any) {
  176. msg := TranslateMessage(c, key, args...)
  177. c.JSON(http.StatusOK, gin.H{
  178. "success": false,
  179. "message": msg,
  180. })
  181. }
  182. // ApiSuccessI18n returns a translated success message based on the user's language preference
  183. func ApiSuccessI18n(c *gin.Context, key string, data any, args ...map[string]any) {
  184. msg := TranslateMessage(c, key, args...)
  185. c.JSON(http.StatusOK, gin.H{
  186. "success": true,
  187. "message": msg,
  188. "data": data,
  189. })
  190. }
  191. // TranslateMessage is a helper function that calls i18n.T
  192. // This function is defined here to avoid circular imports
  193. // The actual implementation will be set during init
  194. var TranslateMessage func(c *gin.Context, key string, args ...map[string]any) string
  195. func init() {
  196. // Default implementation that returns the key as-is
  197. // This will be replaced by i18n.T during i18n initialization
  198. TranslateMessage = func(c *gin.Context, key string, args ...map[string]any) string {
  199. return key
  200. }
  201. }
  202. func ParseMultipartFormReusable(c *gin.Context) (*multipart.Form, error) {
  203. storage, err := GetBodyStorage(c)
  204. if err != nil {
  205. return nil, err
  206. }
  207. requestBody, err := storage.Bytes()
  208. if err != nil {
  209. return nil, err
  210. }
  211. // Use the original Content-Type saved on first call to avoid boundary
  212. // mismatch when callers overwrite c.Request.Header after multipart rebuild.
  213. var contentType string
  214. if saved, ok := c.Get("_original_multipart_ct"); ok {
  215. contentType = saved.(string)
  216. } else {
  217. contentType = c.Request.Header.Get("Content-Type")
  218. c.Set("_original_multipart_ct", contentType)
  219. }
  220. boundary, err := parseBoundary(contentType)
  221. if err != nil {
  222. return nil, err
  223. }
  224. reader := multipart.NewReader(bytes.NewReader(requestBody), boundary)
  225. form, err := reader.ReadForm(multipartMemoryLimit())
  226. if err != nil {
  227. return nil, err
  228. }
  229. // Reset request body
  230. if _, seekErr := storage.Seek(0, io.SeekStart); seekErr != nil {
  231. return nil, seekErr
  232. }
  233. c.Request.Body = io.NopCloser(storage)
  234. return form, nil
  235. }
  236. func processFormMap(formMap map[string]any, v any) error {
  237. jsonData, err := Marshal(formMap)
  238. if err != nil {
  239. return err
  240. }
  241. err = Unmarshal(jsonData, v)
  242. if err != nil {
  243. return err
  244. }
  245. return nil
  246. }
  247. func parseFormData(data []byte, v any) error {
  248. values, err := url.ParseQuery(string(data))
  249. if err != nil {
  250. return err
  251. }
  252. formMap := make(map[string]any)
  253. for key, vals := range values {
  254. if len(vals) == 1 {
  255. formMap[key] = vals[0]
  256. } else {
  257. formMap[key] = vals
  258. }
  259. }
  260. return processFormMap(formMap, v)
  261. }
  262. func parseMultipartFormData(c *gin.Context, data []byte, v any) error {
  263. var contentType string
  264. if saved, ok := c.Get("_original_multipart_ct"); ok {
  265. contentType = saved.(string)
  266. } else {
  267. contentType = c.Request.Header.Get("Content-Type")
  268. c.Set("_original_multipart_ct", contentType)
  269. }
  270. boundary, err := parseBoundary(contentType)
  271. if err != nil {
  272. if errors.Is(err, errBoundaryNotFound) {
  273. return Unmarshal(data, v) // Fallback to JSON
  274. }
  275. return err
  276. }
  277. reader := multipart.NewReader(bytes.NewReader(data), boundary)
  278. form, err := reader.ReadForm(multipartMemoryLimit())
  279. if err != nil {
  280. return err
  281. }
  282. defer form.RemoveAll()
  283. formMap := make(map[string]any)
  284. for key, vals := range form.Value {
  285. if len(vals) == 1 {
  286. formMap[key] = vals[0]
  287. } else {
  288. formMap[key] = vals
  289. }
  290. }
  291. return processFormMap(formMap, v)
  292. }
  293. var errBoundaryNotFound = errors.New("multipart boundary not found")
  294. // parseBoundary extracts the multipart boundary from the Content-Type header using mime.ParseMediaType
  295. func parseBoundary(contentType string) (string, error) {
  296. if contentType == "" {
  297. return "", errBoundaryNotFound
  298. }
  299. // Boundary-UUID / boundary-------xxxxxx
  300. _, params, err := mime.ParseMediaType(contentType)
  301. if err != nil {
  302. return "", err
  303. }
  304. boundary, ok := params["boundary"]
  305. if !ok || boundary == "" {
  306. return "", errBoundaryNotFound
  307. }
  308. return boundary, nil
  309. }
  310. // multipartMemoryLimit returns the configured multipart memory limit in bytes
  311. func multipartMemoryLimit() int64 {
  312. limitMB := constant.MaxFileDownloadMB
  313. if limitMB <= 0 {
  314. limitMB = 32
  315. }
  316. return int64(limitMB) << 20
  317. }