How to get the fileinfo of a multipart.File?

3,217 views
Skip to first unread message

cso...@gmail.com

unread,
Jun 16, 2013, 5:40:29 AM6/16/13
to golan...@googlegroups.com
Given a multipart.File, how would you go about finding the size of the file, while still retaining the multipart.File itself?

os.File has Stat(), but multipart.File doesn't have anything of the sort.

Kyle Lemons

unread,
Jun 16, 2013, 5:30:47 PM6/16/13
to cso...@gmail.com, golang-nuts
You can't in general, because it isn't known; you can check the headers, though, and see if a Content-Length or something of that nature was provided.

What are you trying to do with the length?  If you want to only read up to a certain size, you can wrap it with a LimitReader.




On Sun, Jun 16, 2013 at 2:40 AM, <cso...@gmail.com> wrote:
Given a multipart.File, how would you go about finding the size of the file, while still retaining the multipart.File itself?

os.File has Stat(), but multipart.File doesn't have anything of the sort.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

cso...@gmail.com

unread,
Jun 16, 2013, 8:00:39 PM6/16/13
to golan...@googlegroups.com, cso...@gmail.com
Just trying to set limits on the size of files that can be uploaded by the user.

Using r.FormFile("file") you get a multipart.File, a multipart.FileHeader and an error. 

The FileHeader in this case doesn't seem to have any information about the size of the file sent - I'm pretty sure I could find look at the request header or whatever, but that would probably vary given there are other inputs sent over also. 

I don't want to do anything with the file for now, just test for whether it is smaller than a given size. No idiomatic way to do that?

cso...@gmail.com

unread,
Jun 16, 2013, 8:26:36 PM6/16/13
to golan...@googlegroups.com, cso...@gmail.com
Just to clarify, I do also need to know it's exact size in number of bytes also.

Jesse McNelis

unread,
Jun 16, 2013, 10:14:02 PM6/16/13
to cso...@gmail.com, golang-nuts
On Mon, Jun 17, 2013 at 10:26 AM, <cso...@gmail.com> wrote:
Just to clarify, I do also need to know it's exact size in number of bytes also.

Any kind of 'file size' information being sent from a client isn't trustworthy.
The only way to know the exact size of the file being sent is by reading it all and counting the bytes.

If you have a size limit you just create an io.LimitReader just beyond your limit and reject 
the file if it's larger than your limit.
You can optimise this rejection by checking if the 'content-length' header is beyond the limit first.

cso...@gmail.com

unread,
Jun 17, 2013, 2:17:06 AM6/17/13
to golan...@googlegroups.com, cso...@gmail.com, jes...@jessta.id.au
I'm not quite sure how to use io.LimitReader to do that to be honest.. I mean, how to know whether the file is larger than my limit via io.LimitReader..

...

Kyle Lemons

unread,
Jun 17, 2013, 2:40:45 AM6/17/13
to cso...@gmail.com, golang-nuts, Jesse McNelis
Read everything with a LimitReader then check how much you read.

---

const FileSizeLimit = 10 * Megabyte

func getFIle(r io.Reader) ([]byte, error) {
  limit := io.LimitReader(r, FileSizeLimit+1)
  data, err := ioutil.ReadAll(limit)
  if err != nil {
    return nil, err
  }
  if len(data) > FileSizeLimit {
    return nil, ErrFileTooBig
  }
  return data, nil
}


cso...@gmail.com

unread,
Jun 17, 2013, 3:11:06 AM6/17/13
to golan...@googlegroups.com, cso...@gmail.com, Jesse McNelis
Ah, I see. Though I've been doing something akin to that for a while to be honest.

What I'm struggling with is the fact that I still need to use the file for other things such as retrieving it's dimensions using the image package and sending it as a file to amazon s3 - using http://godoc.org/github.com/kr/s3#Sign.

In your example, the file is no longer usable - I think - as it's contents has been read into that byte array...

Hope I'm making sense here.... lol..

Jesse McNelis

unread,
Jun 17, 2013, 3:23:03 AM6/17/13
to cso...@gmail.com, golang-nuts
On Mon, Jun 17, 2013 at 5:11 PM, <cso...@gmail.com> wrote:
Ah, I see. Though I've been doing something akin to that for a while to be honest.

What I'm struggling with is the fact that I still need to use the file for other things such as retrieving it's dimensions using the image package and sending it as a file to amazon s3 - using http://godoc.org/github.com/kr/s3#Sign.

In your example, the file is no longer usable - I think - as it's contents has been read into that byte array...

What do you mean by 'no longer usable'? It's a byte slice, it's usable for lots of things.
If you mean, 'it's no longer an io.Reader' then http://golang.org/pkg/bytes/#Reader
You don't even have to read it in to a byte slice, you can use io.Copy to copy it to an io.Writer (an os.File) and start writing it to disk.

--
=====================
http://jessta.id.au

cso...@gmail.com

unread,
Jun 17, 2013, 3:27:24 AM6/17/13
to golan...@googlegroups.com, cso...@gmail.com, jes...@jessta.id.au
Oh lol, that was all I needed really. How to turn the byte slice back into an io.Reader. Lol..

Thank you..

cso...@gmail.com

unread,
Jun 17, 2013, 3:45:44 AM6/17/13
to golan...@googlegroups.com, cso...@gmail.com, jes...@jessta.id.au
err.. one last question... 

The bytes.Reader doesn't seem to be usable as a io.Reader, keep getting: cannot use (type bytes.Reader) as type io.Reader in function argument.

What am I doing wrong?

Jesse McNelis

unread,
Jun 17, 2013, 3:49:35 AM6/17/13
to cso...@gmail.com, golang-nuts
On Mon, Jun 17, 2013 at 5:45 PM, <cso...@gmail.com> wrote:
err.. one last question... 

The bytes.Reader doesn't seem to be usable as a io.Reader, keep getting: cannot use (type bytes.Reader) as type io.Reader in function argument.

bytes.Reader isn't an io.Reader, *bytes.Reader is.
eg.
bytesReader := bytes.NewReader(yourbyteslice)
var reader io.Reader = bytesReader

--
=====================
http://jessta.id.au

Reply all
Reply to author
Forward
0 new messages