-
-
Notifications
You must be signed in to change notification settings - Fork 2k
🐛 fix: Always close form file #3786
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
9750e2a
ed46424
69cae1f
0f71741
8e61167
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -269,43 +269,51 @@ func parserRequestBodyFile(req *Request) error { | |
|
|
||
| defer fileBufPool.Put(fileBuf) | ||
|
|
||
| for i, v := range req.files { | ||
| if v.name == "" && v.path == "" { | ||
| for i, f := range req.files { | ||
| if f.name == "" && f.path == "" { | ||
| return ErrFileNoName | ||
| } | ||
|
|
||
| // Set the file name if not provided. | ||
| if v.name == "" && v.path != "" { | ||
| v.path = filepath.Clean(v.path) | ||
| v.name = filepath.Base(v.path) | ||
| if f.name == "" && f.path != "" { | ||
| f.path = filepath.Clean(f.path) | ||
| f.name = filepath.Base(f.path) | ||
| } | ||
|
|
||
| // Set the field name if not provided. | ||
| if v.fieldName == "" { | ||
| v.fieldName = "file" + strconv.Itoa(i+1) | ||
| if f.fieldName == "" { | ||
| f.fieldName = "file" + strconv.Itoa(i+1) | ||
| } | ||
|
|
||
| // If reader is not set, open the file. | ||
| if v.reader == nil { | ||
| v.reader, err = os.Open(v.path) | ||
| if err != nil { | ||
| return fmt.Errorf("open file error: %w", err) | ||
| } | ||
| if err := addFormFile(mw, f, fileBuf); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // Create form file and copy the content. | ||
| w, err := mw.CreateFormFile(v.fieldName, v.name) | ||
| func addFormFile(mw *multipart.Writer, f *File, fileBuf *[]byte) error { | ||
| // If reader is not set, open the file. | ||
| if f.reader == nil { | ||
| var err error | ||
| f.reader, err = os.Open(f.path) | ||
| if err != nil { | ||
| return fmt.Errorf("create file error: %w", err) | ||
| return fmt.Errorf("open file error: %w", err) | ||
| } | ||
| } | ||
|
|
||
| if _, err := io.CopyBuffer(w, v.reader, *fileBuf); err != nil { | ||
| return fmt.Errorf("failed to copy file data: %w", err) | ||
| } | ||
| // Ensure the file reader is always closed after copying. | ||
| defer f.reader.Close() //nolint:errcheck // not needed | ||
|
|
||
| if err := v.reader.Close(); err != nil { | ||
| return fmt.Errorf("close file error: %w", err) | ||
| } | ||
| // Create form file and copy the content. | ||
| w, err := mw.CreateFormFile(f.fieldName, f.name) | ||
| if err != nil { | ||
| return fmt.Errorf("create file error: %w", err) | ||
| } | ||
|
|
||
| if _, err := io.CopyBuffer(w, f.reader, *fileBuf); err != nil { | ||
| return fmt.Errorf("failed to copy file data: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
|
Comment on lines
+296
to
319
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While using I suggest modifying the function to handle the error from func addFormFile(mw *multipart.Writer, f *File, fileBuf *[]byte) (err error) {
// If reader is not set, open the file.
if f.reader == nil {
f.reader, err = os.Open(f.path)
if err != nil {
return fmt.Errorf("open file error: %w", err)
}
}
// Ensure the file reader is always closed after copying.
defer func() {
if closeErr := f.reader.Close(); closeErr != nil && err == nil {
err = fmt.Errorf("close file error: %w", closeErr)
}
}()
// Create form file and copy the content.
var w io.Writer
w, err = mw.CreateFormFile(f.fieldName, f.name)
if err != nil {
return fmt.Errorf("create file error: %w", err)
}
if _, err = io.CopyBuffer(w, f.reader, *fileBuf); err != nil {
return fmt.Errorf("failed to copy file data: %w", err)
}
return nil
} |
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.