Browse Source

Merge pull request #16 from Uncurlhalo/fix-512-byte-files

Add a check to see if we already hit EOF on Read
pull/23/head
Andrei Marcu 9 years ago
parent
commit
3bf7c88cc8
  1. 16
      upload.go

16
upload.go

@ -140,10 +140,19 @@ func processUpload(upReq UploadRequest) (upload Upload, err error) {
barename, extension := barePlusExt(upReq.filename) barename, extension := barePlusExt(upReq.filename)
// Pull the first 512 bytes off for use in MIME detection if needed // Pull the first 512 bytes off for use in MIME detection if needed
// If the file uploaded fit in 512 or less bytes, then the Read will have returned EOF
// In this situation we don't want to create a MultiReader as this will cause no EOF to be emitted
// by the resulting io.Reader. Instead we just create a new reader from the bytes we already read,
// and only create a multiReader if we have yet to get EOF from the first read.
var fileSrc io.Reader
header := make([]byte, 512) header := make([]byte, 512)
n, err := upReq.src.Read(header) n, err := upReq.src.Read(header)
if n == 0 || err != nil {
if n == 0 {
return return
} else if err == io.EOF {
fileSrc = bytes.NewReader(header[:n])
} else {
fileSrc = io.MultiReader(bytes.NewReader(header), upReq.src)
} }
if upReq.randomBarename || len(barename) == 0 { if upReq.randomBarename || len(barename) == 0 {
@ -199,10 +208,7 @@ func processUpload(upReq UploadRequest) (upload Upload, err error) {
metadataWrite(upload.Filename, &upload) metadataWrite(upload.Filename, &upload)
// Make a multi-reader from the header and the remainder of bytes
// in the upReq.src io.Reader
src := io.MultiReader(bytes.NewReader(header), upReq.src)
bytes, err := io.Copy(dst, src)
bytes, err := io.Copy(dst, fileSrc)
if err != nil { if err != nil {
return return
} else if bytes == 0 { } else if bytes == 0 {

Loading…
Cancel
Save