1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

Context#StaticContent signature changed

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-03-27 21:03:35 -07:00
parent a2d757eddc
commit 28ec39daaa
3 changed files with 23 additions and 25 deletions

View File

@ -136,7 +136,7 @@ type (
// ServeContent sends static content from `io.Reader` and handles caching
// via `If-Modified-Since` request header. It automatically sets `Content-Type`
// and `Last-Modified` response headers.
ServeContent(http.File) error
ServeContent(io.Reader, string, time.Time) error
// Object returns the `context` instance.
Object() *context
@ -380,8 +380,9 @@ func (c *context) File(file string) error {
if err != nil {
return ErrNotFound
}
fi, _ = f.Stat()
}
return c.ServeContent(f)
return c.ServeContent(f, fi.Name(), fi.ModTime())
}
func (c *context) Attachment(r io.Reader, name string) (err error) {
@ -422,24 +423,20 @@ func (c *context) Object() *context {
return c
}
func (c *context) ServeContent(f http.File) error {
func (c *context) ServeContent(r io.Reader, name string, modtime time.Time) error {
rq := c.Request()
rs := c.Response()
fi, err := f.Stat()
if err != nil {
return err
}
if t, err := time.Parse(http.TimeFormat, rq.Header().Get(IfModifiedSince)); err == nil && fi.ModTime().Before(t.Add(1*time.Second)) {
if t, err := time.Parse(http.TimeFormat, rq.Header().Get(IfModifiedSince)); err == nil && modtime.Before(t.Add(1*time.Second)) {
rs.Header().Del(ContentType)
rs.Header().Del(ContentLength)
return c.NoContent(http.StatusNotModified)
}
rs.Header().Set(ContentType, detectContentType(fi.Name()))
rs.Header().Set(LastModified, fi.ModTime().UTC().Format(http.TimeFormat))
rs.Header().Set(ContentType, detectContentType(name))
rs.Header().Set(LastModified, modtime.UTC().Format(http.TimeFormat))
rs.WriteHeader(http.StatusOK)
_, err = io.Copy(rs, f)
_, err := io.Copy(rs, r)
return err
}

View File

@ -245,23 +245,23 @@ func TestContextServeContent(t *testing.T) {
rc := test.NewResponseRecorder()
c := NewContext(rq, rc, e)
// Not cached
fs := http.Dir("_fixture/images")
f, err := fs.Open("walle.png")
if assert.NoError(t, err) {
if assert.NoError(t, c.ServeContent(f)) {
assert.Equal(t, http.StatusOK, rc.Status())
}
}
fi, err := f.Stat()
if assert.NoError(t, err) {
// Not cached
if assert.NoError(t, c.ServeContent(f, fi.Name(), fi.ModTime())) {
assert.Equal(t, http.StatusOK, rc.Status())
}
// Cached
rc = test.NewResponseRecorder()
c = NewContext(rq, rc, e)
fi, err := f.Stat()
if assert.NoError(t, err) {
rq.Header().Set(IfModifiedSince, fi.ModTime().UTC().Format(http.TimeFormat))
if assert.NoError(t, c.ServeContent(f)) {
assert.Equal(t, http.StatusNotModified, rc.Status())
// Cached
rc = test.NewResponseRecorder()
c = NewContext(rq, rc, e)
rq.Header().Set(IfModifiedSince, fi.ModTime().UTC().Format(http.TimeFormat))
if assert.NoError(t, c.ServeContent(f, fi.Name(), fi.ModTime())) {
assert.Equal(t, http.StatusNotModified, rc.Status())
}
}
}
}

View File

@ -97,8 +97,9 @@ func StaticFromConfig(config StaticConfig) echo.MiddlewareFunc {
}
return next.Handle(c)
}
fi, _ = f.Stat() // Index file stat
}
return c.ServeContent(f)
return c.ServeContent(f, fi.Name(), fi.ModTime())
})
}
}