Skip to content
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

Avoid copying the whole file into RAM to detect its content type #214

Merged
merged 1 commit into from Sep 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 4 additions & 7 deletions client/request.go
Expand Up @@ -152,18 +152,15 @@ func (r *request) buildHTTP(mediaType, basePath string, producers map[string]run
}()
for fn, f := range r.fileFields {
for _, fi := range f {
buf := bytes.NewBuffer([]byte{})

// Need to read the data so that we can detect the content type
_, err := io.Copy(buf, fi)
buf := make([]byte, 512)
size, err := fi.Read(buf)
if err != nil {
_ = pw.CloseWithError(err)
log.Println(err)
}
fileBytes := buf.Bytes()
fileContentType := http.DetectContentType(fileBytes)

newFi := runtime.NamedReader(fi.Name(), buf)
fileContentType := http.DetectContentType(buf)
newFi := runtime.NamedReader(fi.Name(), io.MultiReader(bytes.NewReader(buf[:size]), fi))

// Create the MIME headers for the new part
h := make(textproto.MIMEHeader)
Expand Down