Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: simplify parserRequestBodyFile logic
Browse files Browse the repository at this point in the history
efectn committed Dec 2, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 9a2ceb7 commit 0949f42
Showing 1 changed file with 5 additions and 20 deletions.
25 changes: 5 additions & 20 deletions client/hooks.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package client

import (
"errors"
"fmt"
"io"
"math/rand"
@@ -241,8 +240,8 @@ func parserRequestBodyFile(req *Request) error {
return fmt.Errorf("write formdata error: %w", err)
}

// add file
b := make([]byte, 512)
// add files
fileBuf := make([]byte, 1<<20) // Allocate 1MB buffer
for i, v := range req.files {
if v.name == "" && v.path == "" {
return ErrFileNoName
@@ -273,24 +272,10 @@ func parserRequestBodyFile(req *Request) error {
return fmt.Errorf("create file error: %w", err)
}

for {
n, err := v.reader.Read(b)
if err != nil && !errors.Is(err, io.EOF) {
return fmt.Errorf("read file error: %w", err)
}

if errors.Is(err, io.EOF) {
break
}

_, err = w.Write(b[:n])
if err != nil {
return fmt.Errorf("write file error: %w", err)
}
}
// Copy the file from reader to multipart writer
io.CopyBuffer(w, v.reader, fileBuf)

err = v.reader.Close()
if err != nil {
if err := v.reader.Close(); err != nil {
return fmt.Errorf("close file error: %w", err)
}
}

0 comments on commit 0949f42

Please sign in to comment.