mirror of
https://github.com/labstack/echo.git
synced 2025-04-23 12:18:53 +02:00
ReadSeeker instead of Reader to future usage
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
70bf482381
commit
55e3dba946
23
context.go
23
context.go
@ -112,9 +112,9 @@ type (
|
|||||||
// File sends a response with the content of the file.
|
// File sends a response with the content of the file.
|
||||||
File(string) error
|
File(string) error
|
||||||
|
|
||||||
// Attachment sends a response from `io.Reader` as attachment, prompting client
|
// Attachment sends a response from `io.ReaderSeeker` as attachment, prompting
|
||||||
// to save the file.
|
// client to save the file.
|
||||||
Attachment(io.Reader, string) error
|
Attachment(io.ReadSeeker, string) error
|
||||||
|
|
||||||
// NoContent sends a response with no body and a status code.
|
// NoContent sends a response with no body and a status code.
|
||||||
NoContent(int) error
|
NoContent(int) error
|
||||||
@ -137,7 +137,7 @@ type (
|
|||||||
// ServeContent sends static content from `io.Reader` and handles caching
|
// ServeContent sends static content from `io.Reader` and handles caching
|
||||||
// via `If-Modified-Since` request header. It automatically sets `Content-Type`
|
// via `If-Modified-Since` request header. It automatically sets `Content-Type`
|
||||||
// and `Last-Modified` response headers.
|
// and `Last-Modified` response headers.
|
||||||
ServeContent(io.Reader, string, time.Time) error
|
ServeContent(io.ReadSeeker, string, time.Time) error
|
||||||
|
|
||||||
// Object returns the `context` instance.
|
// Object returns the `context` instance.
|
||||||
Object() *context
|
Object() *context
|
||||||
@ -384,8 +384,8 @@ func (c *context) File(file string) error {
|
|||||||
return c.ServeContent(f, fi.Name(), fi.ModTime())
|
return c.ServeContent(f, fi.Name(), fi.ModTime())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) Attachment(r io.Reader, name string) (err error) {
|
func (c *context) Attachment(r io.ReadSeeker, name string) (err error) {
|
||||||
c.response.Header().Set(ContentType, detectContentType(name))
|
c.response.Header().Set(ContentType, ContentTypeByExtension(name))
|
||||||
c.response.Header().Set(ContentDisposition, "attachment; filename="+name)
|
c.response.Header().Set(ContentDisposition, "attachment; filename="+name)
|
||||||
c.response.WriteHeader(http.StatusOK)
|
c.response.WriteHeader(http.StatusOK)
|
||||||
_, err = io.Copy(c.response, r)
|
_, err = io.Copy(c.response, r)
|
||||||
@ -422,7 +422,7 @@ func (c *context) Object() *context {
|
|||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) ServeContent(r io.Reader, name string, modtime time.Time) error {
|
func (c *context) ServeContent(content io.ReadSeeker, name string, modtime time.Time) error {
|
||||||
rq := c.Request()
|
rq := c.Request()
|
||||||
rs := c.Response()
|
rs := c.Response()
|
||||||
|
|
||||||
@ -432,14 +432,17 @@ func (c *context) ServeContent(r io.Reader, name string, modtime time.Time) erro
|
|||||||
return c.NoContent(http.StatusNotModified)
|
return c.NoContent(http.StatusNotModified)
|
||||||
}
|
}
|
||||||
|
|
||||||
rs.Header().Set(ContentType, detectContentType(name))
|
rs.Header().Set(ContentType, ContentTypeByExtension(name))
|
||||||
rs.Header().Set(LastModified, modtime.UTC().Format(http.TimeFormat))
|
rs.Header().Set(LastModified, modtime.UTC().Format(http.TimeFormat))
|
||||||
rs.WriteHeader(http.StatusOK)
|
rs.WriteHeader(http.StatusOK)
|
||||||
_, err := io.Copy(rs, r)
|
_, err := io.Copy(rs, content)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func detectContentType(name string) (t string) {
|
// ContentTypeByExtension returns the MIME type associated with the file based on
|
||||||
|
// its extension. It returns `application/octet-stream` incase MIME type is not
|
||||||
|
// found.
|
||||||
|
func ContentTypeByExtension(name string) (t string) {
|
||||||
if t = mime.TypeByExtension(filepath.Ext(name)); t == "" {
|
if t = mime.TypeByExtension(filepath.Ext(name)); t == "" {
|
||||||
t = OctetStream
|
t = OctetStream
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user