Skip to content

fix: avoid get model consuming body - #1994

Merged
seefs001 merged 1 commit into
QuantumNous:mainfrom
feitianbubu:pr/fix-video-model
Oct 10, 2025
Merged

fix: avoid get model consuming body#1994
seefs001 merged 1 commit into
QuantumNous:mainfrom
feitianbubu:pr/fix-video-model

Conversation

@feitianbubu

@feitianbubu feitianbubu commented Oct 10, 2025

Copy link
Copy Markdown
Member

修复postForm导致form无法被重复获取的问题

Summary by CodeRabbit

  • New Features
    • Improved compatibility with multipart form uploads on POST /v1/videos, enabling reliable extraction of the model parameter.
  • Bug Fixes
    • Clearer, localized error messages when upload parsing fails.
    • Ensures temporary upload data is properly cleaned up after requests.
  • Refactor
    • Unified and hardened request parsing for video uploads to enhance stability and reduce parsing errors.

@coderabbitai

coderabbitai Bot commented Oct 10, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

POST /v1/videos now parses multipart form data using common.ParseMultipartFormReusable, handles parsing errors with a Chinese message, defers cleanup via RemoveAll, and reads the model from form.Value["model"] into modelRequest.Model before proceeding with existing relay_mode logic.

Changes

Cohort / File(s) Summary of changes
Multipart form parsing for /v1/videos
middleware/distributor.go
Replaced c.PostForm("model") with common.ParseMultipartFormReusable; added error handling (Chinese message), deferred form.RemoveAll cleanup, and extraction of model from form.Value["model"]; preserved existing early-return and relay_mode behavior.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor C as Client
  participant M as Middleware (distributor)
  participant P as common.ParseMultipartFormReusable
  participant H as Downstream handler

  C->>M: POST /v1/videos (multipart)
  M->>P: ParseMultipartFormReusable(req)
  alt Parse fails
    P-->>M: error
    M-->>C: 4xx/5xx with Chinese error message
  else Parse succeeds
    P-->>M: form
    note right of M: defer form.RemoveAll()
    M->>M: model = form.Value["model"][0] (if present)
    M->>H: Continue with relay_mode logic
    H-->>C: Response
  end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

I nibble bytes with careful cheer,
A form unfolds, all crisp and clear.
I parse, I clean, then hop along—
The model found, the wiring strong.
With tidy paws and swift delight,
This bunny ships the POST just right. 🐇✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title accurately reflects the key fix to avoid consuming the request body when retrieving the model, matching the change in parsing logic.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5f5b942 and 3c57cfb.

📒 Files selected for processing (1)
  • middleware/distributor.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
middleware/distributor.go (1)
common/gin.go (1)
  • ParseMultipartFormReusable (118-139)
🔇 Additional comments (1)
middleware/distributor.go (1)

177-186: Confirm downstream multipart reuse
Downstream handlers in relay/common/relay_utils.go, relay/helper/valid_request.go, and various channel adaptors invoke c.MultipartForm() or c.Request.ParseMultipartForm; since ParseMultipartFormReusable sets c.Request.MultipartForm and resets the body, each downstream call will reuse the parsed form. Verify via integration tests or manual testing that the /v1/videos submit flow functions as intended.

Comment thread middleware/distributor.go
Comment on lines +177 to +186
form, err := common.ParseMultipartFormReusable(c)
if err != nil {
return nil, false, errors.New("无效的video请求, " + err.Error())
}
defer form.RemoveAll()
if form != nil {
if values, ok := form.Value["model"]; ok && len(values) > 0 {
modelRequest.Model = values[0]
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

Move defer inside nil check for consistency and safety.

The code registers defer form.RemoveAll() on line 181 before checking if form is nil on line 182. While ParseMultipartFormReusable should return a valid form when err == nil, the defensive nil check on line 182 suggests uncertainty. If form were somehow nil at line 181, the deferred RemoveAll() would panic on function return.

Apply this diff to move the defer inside the nil check:

 form, err := common.ParseMultipartFormReusable(c)
 if err != nil {
 	return nil, false, errors.New("无效的video请求, " + err.Error())
 }
-defer form.RemoveAll()
 if form != nil {
+	defer form.RemoveAll()
 	if values, ok := form.Value["model"]; ok && len(values) > 0 {
 		modelRequest.Model = values[0]
 	}
 }

Alternatively, if you're confident form is never nil when err == nil, remove the nil check entirely:

 form, err := common.ParseMultipartFormReusable(c)
 if err != nil {
 	return nil, false, errors.New("无效的video请求, " + err.Error())
 }
 defer form.RemoveAll()
-if form != nil {
-	if values, ok := form.Value["model"]; ok && len(values) > 0 {
-		modelRequest.Model = values[0]
-	}
+if values, ok := form.Value["model"]; ok && len(values) > 0 {
+	modelRequest.Model = values[0]
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
form, err := common.ParseMultipartFormReusable(c)
if err != nil {
return nil, false, errors.New("无效的video请求, " + err.Error())
}
defer form.RemoveAll()
if form != nil {
if values, ok := form.Value["model"]; ok && len(values) > 0 {
modelRequest.Model = values[0]
}
}
form, err := common.ParseMultipartFormReusable(c)
if err != nil {
return nil, false, errors.New("无效的video请求, " + err.Error())
}
if form != nil {
defer form.RemoveAll()
if values, ok := form.Value["model"]; ok && len(values) > 0 {
modelRequest.Model = values[0]
}
}
🤖 Prompt for AI Agents
In middleware/distributor.go around lines 177 to 186, the code defers
form.RemoveAll() before checking if form is nil which can panic if form is
unexpectedly nil; move the defer inside the subsequent if form != nil { ... }
block (i.e., first check form != nil, then call defer form.RemoveAll() and
proceed to read form.Value["model"]) — alternatively, if you guarantee
ParseMultipartFormReusable never returns nil when err == nil, simply remove the
nil check and keep the defer where it is.

@seefs001
seefs001 merged commit 7de0299 into QuantumNous:main Oct 10, 2025
1 check passed
x22x22 pushed a commit to x22x22/new-api that referenced this pull request Apr 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants