-
Notifications
You must be signed in to change notification settings - Fork 8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Serve easily dynamic files with DataFromReader
context method
#1304
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package render | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
type Reader struct { | ||
ContentType string | ||
ContentLength int64 | ||
Reader io.Reader | ||
Headers map[string]string | ||
} | ||
|
||
// Render (Reader) writes data with custom ContentType and headers. | ||
func (r Reader) Render(w http.ResponseWriter) (err error) { | ||
r.WriteContentType(w) | ||
r.Headers["Content-Length"] = strconv.FormatInt(r.ContentLength, 10) | ||
r.writeHeaders(w, r.Headers) | ||
_, err = io.Copy(w, r.Reader) | ||
return | ||
} | ||
|
||
func (r Reader) WriteContentType(w http.ResponseWriter) { | ||
writeContentType(w, []string{r.ContentType}) | ||
} | ||
|
||
func (r Reader) writeHeaders(w http.ResponseWriter, headers map[string]string) { | ||
header := w.Header() | ||
for k, v := range headers { | ||
if val := header[k]; len(val) == 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I prefer to using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jclebreton Please update and I will merge this. |
||
header[k] = []string{v} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ import ( | |
"html/template" | ||
"net/http" | ||
"net/http/httptest" | ||
"strconv" | ||
"strings" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove the extra empty line between internal package. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done ;-) |
||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
@@ -384,3 +386,24 @@ func TestRenderHTMLDebugPanics(t *testing.T) { | |
} | ||
assert.Panics(t, func() { htmlRender.Instance("", nil) }) | ||
} | ||
|
||
func TestRenderReader(t *testing.T) { | ||
w := httptest.NewRecorder() | ||
|
||
body := "#!PNG some raw data" | ||
headers := make(map[string]string) | ||
headers["Content-Disposition"] = `attachment; filename="filename.png"` | ||
|
||
err := (Reader{ | ||
ContentLength: int64(len(body)), | ||
ContentType: "image/png", | ||
Reader: strings.NewReader(body), | ||
Headers: headers, | ||
}).Render(w) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, body, w.Body.String()) | ||
assert.Equal(t, "image/png", w.HeaderMap.Get("Content-Type")) | ||
assert.Equal(t, strconv.Itoa(len(body)), w.HeaderMap.Get("Content-Length")) | ||
assert.Equal(t, headers["Content-Disposition"], w.HeaderMap.Get("Content-Disposition")) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add content link in README, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @thinkerou I'm not sure to understand the target of the asked link. This page ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should add
[Serving data from reader](#serving-data-from-reader)
after the line:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Thanks