mirror of
https://github.com/valyala/fasthttp.git
synced 2026-06-14 15:56:44 +03:00
Added SendFile to Response
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user