gin.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. // ApiErrorI18n returns a translated error message based on the user's language preference
  187. // key is the i18n message key, args is optional template data
  188. func ApiErrorI18n(c *gin.Context, key string, args ...map[string]any) {
  189. msg := TranslateMessage(c, key, args...)
  190. c.JSON(http.StatusOK, gin.H{
  191. "success": false,
  192. "message": msg,
  193. })
  194. }
  195. // ApiSuccessI18n returns a translated success message based on the user's language preference
  196. func ApiSuccessI18n(c *gin.Context, key string, data any, args ...map[string]any) {
  197. msg := TranslateMessage(c, key, args...)
  198. c.JSON(http.StatusOK, gin.H{
  199. "success": true,
  200. "message": msg,
  201. "data": data,
  202. })
  203. }
  204. // TranslateMessage is a helper function that calls i18n.T
  205. // This function is defined here to avoid circular imports
  206. // The actual implementation will be set during init
  207. var TranslateMessage func(c *gin.Context, key string, args ...map[string]any) string
  208. func init() {
  209. // Default implementation that returns the key as-is
  210. // This will be replaced by i18n.T during i18n initialization
  211. TranslateMessage = func(c *gin.Context, key string, args ...map[string]any) string {
  212. return key
  213. }
  214. }
  215. func ParseMultipartFormReusable(c *gin.Context) (*multipart.Form, error) {
  216. requestBody, err := GetRequestBody(c)
  217. if err != nil {
  218. return nil, err
  219. }
  220. contentType := c.Request.Header.Get("Content-Type")
  221. boundary, err := parseBoundary(contentType)
  222. if err != nil {
  223. return nil, err
  224. }
  225. reader := multipart.NewReader(bytes.NewReader(requestBody), boundary)
  226. form, err := reader.ReadForm(multipartMemoryLimit())
  227. if err != nil {
  228. return nil, err
  229. }
  230. // Reset request body
  231. c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
  232. return form, nil
  233. }
  234. func processFormMap(formMap map[string]any, v any) error {
  235. jsonData, err := Marshal(formMap)
  236. if err != nil {
  237. return err
  238. }
  239. err = Unmarshal(jsonData, v)
  240. if err != nil {
  241. return err
  242. }
  243. return nil
  244. }
  245. func parseFormData(data []byte, v any) error {
  246. values, err := url.ParseQuery(string(data))
  247. if err != nil {
  248. return err
  249. }
  250. formMap := make(map[string]any)
  251. for key, vals := range values {
  252. if len(vals) == 1 {
  253. formMap[key] = vals[0]
  254. } else {
  255. formMap[key] = vals
  256. }
  257. }
  258. return processFormMap(formMap, v)
  259. }
  260. func parseMultipartFormData(c *gin.Context, data []byte, v any) error {
  261. contentType := c.Request.Header.Get("Content-Type")
  262. boundary, err := parseBoundary(contentType)
  263. if err != nil {
  264. if errors.Is(err, errBoundaryNotFound) {
  265. return Unmarshal(data, v) // Fallback to JSON
  266. }
  267. return err
  268. }
  269. reader := multipart.NewReader(bytes.NewReader(data), boundary)
  270. form, err := reader.ReadForm(multipartMemoryLimit())
  271. if err != nil {
  272. return err
  273. }
  274. defer form.RemoveAll()
  275. formMap := make(map[string]any)
  276. for key, vals := range form.Value {
  277. if len(vals) == 1 {
  278. formMap[key] = vals[0]
  279. } else {
  280. formMap[key] = vals
  281. }
  282. }
  283. return processFormMap(formMap, v)
  284. }
  285. var errBoundaryNotFound = errors.New("multipart boundary not found")
  286. // parseBoundary extracts the multipart boundary from the Content-Type header using mime.ParseMediaType
  287. func parseBoundary(contentType string) (string, error) {
  288. if contentType == "" {
  289. return "", errBoundaryNotFound
  290. }
  291. // Boundary-UUID / boundary-------xxxxxx
  292. _, params, err := mime.ParseMediaType(contentType)
  293. if err != nil {
  294. return "", err
  295. }
  296. boundary, ok := params["boundary"]
  297. if !ok || boundary == "" {
  298. return "", errBoundaryNotFound
  299. }
  300. return boundary, nil
  301. }
  302. // multipartMemoryLimit returns the configured multipart memory limit in bytes
  303. func multipartMemoryLimit() int64 {
  304. limitMB := constant.MaxFileDownloadMB
  305. if limitMB <= 0 {
  306. limitMB = 32
  307. }
  308. return int64(limitMB) << 20
  309. }