fix: preserve filenames for file uploads#730
fix: preserve filenames for file uploads#730rendylong wants to merge 1 commit intolarksuite:mainfrom
Conversation
📝 WalkthroughWalkthroughThe Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
internal/cmdutil/fileupload.go (1)
111-119:⚠️ Potential issue | 🟠 MajorAdd explicit cleanup for file handles opened in
BuildFormdata.The opened file from
fileIO.Open(filePath)is passed directly tofd.AddFile()but never closed. The Lark SDK does not close files passed toFormdata.AddFile, so the caller must handle cleanup. Currently, the file handle escapesBuildFormdatavia the returnedFormdataand leaks until process exit.Ensure file closure either by:
- Using a cleanup callback mechanism in the returned
Formdatawrapper- Having callers explicitly close the file after the request completes
- Documenting the ownership contract clearly if callers are responsible
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@internal/cmdutil/fileupload.go` around lines 111 - 119, The BuildFormdata function currently opens files with fileIO.Open and passes the *os.File to fd.AddFile (fd.AddFile) but never closes them, leaking file descriptors; modify BuildFormdata to track opened files and expose a cleanup mechanism on the returned Formdata (e.g., a Close or Cleanup method on the Formdata wrapper) that iterates over tracked files and calls Close, or alternatively document and require callers to call a new CloseFiles helper; ensure the file-opening path in BuildFormdata records each opened handle (from fileIO.Open) so the cleanup method can close them after the HTTP request completes.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@internal/cmdutil/fileupload.go`:
- Around line 111-119: The BuildFormdata function currently opens files with
fileIO.Open and passes the *os.File to fd.AddFile (fd.AddFile) but never closes
them, leaking file descriptors; modify BuildFormdata to track opened files and
expose a cleanup mechanism on the returned Formdata (e.g., a Close or Cleanup
method on the Formdata wrapper) that iterates over tracked files and calls
Close, or alternatively document and require callers to call a new CloseFiles
helper; ensure the file-opening path in BuildFormdata records each opened handle
(from fileIO.Open) so the cleanup method can close them after the HTTP request
completes.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: b9acdf76-0b9c-4e4a-ba46-2f0c939da26b
📒 Files selected for processing (2)
internal/cmdutil/fileupload.gointernal/cmdutil/fileupload_test.go
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #730 +/- ##
=======================================
Coverage 64.38% 64.38%
=======================================
Files 512 512
Lines 45426 45422 -4
=======================================
- Hits 29249 29247 -2
+ Misses 13604 13603 -1
+ Partials 2573 2572 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
🚀 PR Preview Install Guide🧰 CLI updatenpm i -g https://pkg.pr.new/larksuite/cli/@larksuite/cli@929ee2cc3f7bb9556fc5cc8400d7c8d88a77a75c🧩 Skill updatenpx skills add rendylong/cli#fix-file-upload-filename -y -g |
Summary
Fix generic
--fileuploads so multipart filenames are preserved for local files.BuildFormdatapreviously opened a local file, read it into memory, and passed abytes.Readerto the Lark SDK. The SDK only preserves multipart filenames when the form field is a concrete*os.File; anonymous readers fall back tounknown-file.This change passes the opened local file directly to
Formdata.AddFile, allowing the SDK to usefilepath.Base(f.Name())as the multipart filename.Fixes #729
Tests
go test ./internal/cmdutilgo test ./cmd/api ./cmd/servicemake buildSummary by CodeRabbit