Skip to content

Commit

Permalink
Merge pull request #214 from jkawamoto/213
Browse files Browse the repository at this point in the history
Avoid copying the whole file into RAM to detect its content type
  • Loading branch information
youyuanwu committed Sep 5, 2021
2 parents e518b0a + 6a7287f commit 715c788
Showing 1 changed file with 4 additions and 7 deletions.
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

0 comments on commit 715c788

Please sign in to comment.