Skip to content

Commit

Permalink
Avoid copying the whole file into RAM to detect its content type
Browse files Browse the repository at this point in the history
request.buildHTTP copies the whole files into RAM to detect their
content types, but it increases RAM usage if there is a large file.
Since http.DetectContentType only considers at most the first 512 bytes
of data, we can avoid copying the whole data.

Signed-off-by: Junpei Kawamoto <kawamoto.junpei@gmail.com>
  • Loading branch information
jkawamoto committed Sep 5, 2021
1 parent e518b0a commit 6a7287f
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 6a7287f

Please sign in to comment.