From 6a7287f2f319e788ba166f9bba8b5f0567179388 Mon Sep 17 00:00:00 2001 From: Junpei Kawamoto Date: Sat, 4 Sep 2021 01:05:16 -0600 Subject: [PATCH] Avoid copying the whole file into RAM to detect its content type 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 --- client/request.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/client/request.go b/client/request.go index 2ffeb8d1..0329ccff 100644 --- a/client/request.go +++ b/client/request.go @@ -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)