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

Add a check to see if we already hit EOF on Read
This commit is contained in:
Andrei Marcu 2015-09-30 11:01:04 -04:00
commit 3bf7c88cc8

View File

@ -140,10 +140,19 @@ func processUpload(upReq UploadRequest) (upload Upload, err error) {
barename, extension := barePlusExt(upReq.filename)
// 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)
n, err := upReq.src.Read(header)
if n == 0 || err != nil {
if n == 0 {
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 {
@ -199,10 +208,7 @@ func processUpload(upReq UploadRequest) (upload Upload, err error) {
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 {
return
} else if bytes == 0 {