1
0
mirror of https://github.com/golang/go synced 2024-11-23 21:40:05 -07:00

mime/multipart: add Size to FileHeader

This change makes it possible to retrieve the size of a file part
without having to Seek to determine file-size.

Resolves #19501

Change-Id: I7b9994c4cf41c9b06a046eb7046f8952ae1f15e9
Reviewed-on: https://go-review.googlesource.com/39223
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Jamie Stackhouse 2017-04-01 01:07:59 -03:00 committed by Brad Fitzpatrick
parent 8a4cee67af
commit d96f9cbb87
2 changed files with 7 additions and 1 deletions

View File

@ -79,7 +79,7 @@ func (r *Reader) readForm(maxMemory int64) (_ *Form, err error) {
if err != nil {
return nil, err
}
_, err = io.Copy(file, io.MultiReader(&b, p))
size, err := io.Copy(file, io.MultiReader(&b, p))
if cerr := file.Close(); err == nil {
err = cerr
}
@ -88,8 +88,10 @@ func (r *Reader) readForm(maxMemory int64) (_ *Form, err error) {
return nil, err
}
fh.tmpfile = file.Name()
fh.Size = size
} else {
fh.content = b.Bytes()
fh.Size = int64(len(fh.content))
maxMemory -= n
}
form.File[name] = append(form.File[name], fh)
@ -128,6 +130,7 @@ func (f *Form) RemoveAll() error {
type FileHeader struct {
Filename string
Header textproto.MIMEHeader
Size int64
content []byte
tmpfile string

View File

@ -44,6 +44,9 @@ func testFile(t *testing.T, fh *FileHeader, efn, econtent string) File {
if fh.Filename != efn {
t.Errorf("filename = %q, want %q", fh.Filename, efn)
}
if fh.Size != int64(len(econtent)) {
t.Errorf("size = %d, want %d", fh.Size, len(econtent))
}
f, err := fh.Open()
if err != nil {
t.Fatal("opening file:", err)