New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
mime/multipart: add 'Size int64' to FileHeader struct #19501
Comments
It seems like the first thing we'd need to do is decide whether we're going to enumerate the possible interfaces in a multipart.File. To date, the only thing we've guaranteed is that when it's on disk it is a *os.File:
It's guaranteed to be a Seeker, so you can Seek to the end and look at the resulting offset. That's what http.ServeContent does. |
Yeah, using Seek in a style similar to http.ServeContent is what I ended up doing for the actual code I wrote for what triggered the proposal. So that is how I handled the case where the underlying implementation doesn't already have a Size() method that would be quicker. I don't think we need to enumerate every possible interface of |
Seems like a Somebody want to try that? |
Approving for adding Size int64 to FileHeader. |
CL https://golang.org/cl/39223 mentions this issue. |
This change makes it possible to retrieve the size of a file part without having to Seek to determine file-size. Resolves golang#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>
When receiving a file from a form upload, it is sometimes required to be able to grab the individual file content length, rather than limiting the size of the entire request body.
However, when using
http.Request.FormFile()
you are given amultipart.File
, which is implemented as either anio.SectionReader
oros.File
depending on whether the file was larger than the defined max multipart memory size. The SectionReader is wrapped in a little structsectionReadCloser
for a no-op close.After looking at and implementing this size check. I was wishing to have a simpler method to implement checking the size, where right now to check, you can upgrade the sectionReadCloser into a "sizer". When it's a os.File, you could either Stat() and check the size or seek to the end, grab the number of bytes read, seek to the start, and then proceed.
Example:
If possible, adding Size() directly to the multipart.File interface would be great, but in some conversation on Gopher Slack, it was pointed out this would violate the Go 1 Compat. Since this is the case, I'd be open to other ideas here, but at the very least I would like to propose wrapping the os.File that is returned in a private struct similar to sectionReadCloser to add a Size() method to make
multipart.File
a single interface upgrade to get the file size.The text was updated successfully, but these errors were encountered: