github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/util/form/form.go (about) 1 package form 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "mime" 8 "mime/multipart" 9 ) 10 11 func ReadField(form *multipart.Form, name string) ([]byte, error) { 12 files, ok := form.File[name] 13 if !ok || len(files) == 0 { 14 return nil, nil 15 } 16 fh := files[0] 17 if fh.Size == 0 { 18 return nil, nil 19 } 20 f, err := fh.Open() 21 if err != nil { 22 return nil, err 23 } 24 defer func() { 25 err = f.Close() 26 }() 27 b := bytes.NewBuffer(make([]byte, 0, fh.Size)) 28 if _, err = io.Copy(b, f); err != nil { 29 return nil, err 30 } 31 return b.Bytes(), nil 32 } 33 34 func ParseBoundary(contentType string) (string, error) { 35 _, params, err := mime.ParseMediaType(contentType) 36 if err != nil { 37 return "", err 38 } 39 boundary, ok := params["boundary"] 40 if !ok { 41 return "", fmt.Errorf("malformed multipart content type header") 42 } 43 return boundary, nil 44 }