Skip to content

Commit

Permalink
Inline feature (labstack#637)
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscocpg authored and vishr committed Aug 22, 2016
1 parent 6fd725c commit 8a85626
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
15 changes: 14 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package echo
import (
"encoding/json"
"encoding/xml"
"fmt"
"io"
"mime"
"mime/multipart"
Expand Down Expand Up @@ -140,6 +141,10 @@ type (
// client to save the file.
Attachment(io.ReadSeeker, string) error

// Inline sends a response from `io.ReaderSeeker` as inline, opening
// the file in the browser.
Inline(io.ReadSeeker, string) error

// NoContent sends a response with no body and a status code.
NoContent(int) error

Expand Down Expand Up @@ -417,8 +422,16 @@ func (c *echoContext) File(file string) error {
}

func (c *echoContext) Attachment(r io.ReadSeeker, name string) (err error) {
return c.contentDisposition(r, name, "attachment")
}

func (c *echoContext) Inline(r io.ReadSeeker, name string) (err error) {
return c.contentDisposition(r, name, "inline")
}

func (c *echoContext) contentDisposition(r io.ReadSeeker, name, dispositionType string) (err error) {
c.response.Header().Set(HeaderContentType, ContentTypeByExtension(name))
c.response.Header().Set(HeaderContentDisposition, "attachment; filename="+name)
c.response.Header().Set(HeaderContentDisposition, fmt.Sprintf("%s; filename=%s", dispositionType, name))
c.response.WriteHeader(http.StatusOK)
_, err = io.Copy(c.response, r)
return
Expand Down
13 changes: 13 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ func TestContext(t *testing.T) {
}
}

// Inline
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec).(*echoContext)
file, err = os.Open("_fixture/images/walle.png")
if assert.NoError(t, err) {
err = c.Inline(file, "walle.png")
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Status())
assert.Equal(t, "inline; filename=walle.png", rec.Header().Get(HeaderContentDisposition))
assert.Equal(t, 219885, rec.Body.Len())
}
}

// NoContent
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec).(*echoContext)
Expand Down

0 comments on commit 8a85626

Please sign in to comment.