Added SendFile to Response

This commit is contained in:
Aliaksandr Valialkin
2015-12-09 13:47:11 +02:00
parent bc50b11813
commit 6ad39dad61
2 changed files with 26 additions and 16 deletions
+25
View File
@@ -7,6 +7,7 @@ import (
"fmt"
"io"
"mime/multipart"
"os"
"sync"
)
@@ -98,6 +99,30 @@ func (req *Request) SetConnectionClose() {
req.Header.SetConnectionClose()
}
// SendFile registers file on the given path to be used as response body
// when Write is called.
//
// Note that SendFile doesn't set Content-Type, so set it yourself
// with Header.SetContentType.
func (resp *Response) SendFile(path string) error {
f, err := os.Open(path)
if err != nil {
return err
}
statInfo, err := f.Stat()
if err != nil {
f.Close()
return err
}
size64 := statInfo.Size()
size := int(size64)
if int64(size) != size64 {
size = -1
}
resp.SetBodyStream(f, size)
return nil
}
// SetBodyStream sets response body stream and, optionally body size.
//
// If bodySize is >= 0, then the bodyStream must provide exactly bodySize bytes
+1 -16
View File
@@ -659,22 +659,7 @@ func (ctx *RequestCtx) ResetBody() {
// so set it yourself with SetContentType() before returning
// from RequestHandler.
func (ctx *RequestCtx) SendFile(path string) error {
f, err := os.Open(path)
if err != nil {
return err
}
statInfo, err := f.Stat()
if err != nil {
f.Close()
return err
}
size64 := statInfo.Size()
size := int(size64)
if int64(size) != size64 {
size = -1
}
ctx.SetBodyStream(f, size)
return nil
return ctx.Response.SendFile(path)
}
// Write writes p into response body.