Add a check to see if we already hit EOF on Read
This commit is contained in:
parent
8a1b688b3c
commit
c22e314d80
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
|
bytes, err := io.Copy(dst, fileSrc)
|
||||||
// in the upReq.src io.Reader
|
|
||||||
src := io.MultiReader(bytes.NewReader(header), upReq.src)
|
|
||||||
bytes, err := io.Copy(dst, src)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
} else if bytes == 0 {
|
} else if bytes == 0 {
|
||||||
|
|
Loading…
Reference in New Issue