Skip to content

Commit

Permalink
Fixes #1089
Browse files Browse the repository at this point in the history
  • Loading branch information
yanick committed Oct 29, 2014
2 parents 813ca46 + 125ce64 commit b004126
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -7,6 +7,7 @@ Revision history for Dancer

[DOCUMENTATION]
- Mention environment variables in Dancer::Config. (GH#1085, Sniperovitch)
- Replace "return send_file" with "send_file". (GH#1089, Ashley Willis)

[ENHANCEMENTS]

Expand Down
25 changes: 11 additions & 14 deletions lib/Dancer.pm
Expand Up @@ -1437,7 +1437,7 @@ the path of the file must be relative to the B<public> directory unless you use
the C<system_path> option (see below).
get '/download/:file' => sub {
return send_file(params->{file});
send_file(params->{file});
}
B<WARNING> : Issuing a send_file immediately exits the current route, and perform
Expand All @@ -1457,7 +1457,7 @@ Send file supports streaming possibility using PSGI streaming. The server should
support it but normal streaming is supported on most, if not all.
get '/download/:file' => sub {
return send_file( params->{file}, streaming => 1 );
send_file( params->{file}, streaming => 1 );
}
You can control what happens using callbacks.
Expand All @@ -1466,7 +1466,7 @@ First, C<around_content> allows you to get the writer object and the chunk of
content read, and then decide what to do with each chunk:
get '/download/:file' => sub {
return send_file(
send_file(
params->{file},
streaming => 1,
callbacks => {
Expand All @@ -1483,7 +1483,7 @@ a regular file or a full string if it's a scalar ref) and decide what to do with
it:
get '/download/:file' => sub {
return send_file(
send_file(
params->{file},
streaming => 1,
callbacks => {
Expand All @@ -1503,7 +1503,7 @@ it:
Or you could use C<override> to control the entire streaming callback request:
get '/download/:file' => sub {
return send_file(
send_file(
params->{file},
streaming => 1,
callbacks => {
Expand All @@ -1521,7 +1521,7 @@ You can also set the number of bytes that will be read at a time (default being
42K bytes) using C<bytes>:
get '/download/:file' => sub {
return send_file(
send_file(
params->{file},
streaming => 1,
bytes => 524288, # 512K
Expand All @@ -1535,37 +1535,34 @@ The content-type will be set depending on the current MIME types definition
If your filename does not have an extension, or you need to force a
specific mime type, you can pass it to C<send_file> as follows:
return send_file(params->{file}, content_type => 'image/png');
send_file(params->{file}, content_type => 'image/png');
Also, you can use your aliases or file extension names on
C<content_type>, like this:
return send_file(params->{file}, content_type => 'png');
send_file(params->{file}, content_type => 'png');
For files outside your B<public> folder, you can use the C<system_path>
switch. Just bear in mind that its use needs caution as it can be
dangerous.
return send_file('/etc/passwd', system_path => 1);
send_file('/etc/passwd', system_path => 1);
If you have your data in a scalar variable, C<send_file> can be useful
as well. Pass a reference to that scalar, and C<send_file> will behave
as if there was a file with that contents:
return send_file( \$data, content_type => 'image/png' );
send_file( \$data, content_type => 'image/png' );
Note that Dancer is unable to guess the content type from the data
contents. Therefore you might need to set the C<content_type>
properly. For this kind of usage an attribute named C<filename> can be
useful. It is used as the Content-Disposition header, to hint the
browser about the filename it should use.
return send_file( \$data, content_type => 'image/png'
send_file( \$data, content_type => 'image/png'
filename => 'onion.png' );
Note that you should always use C<return send_file ...> to stop execution of
your route handler at that point.
=head2 set
Defines a setting:
Expand Down

0 comments on commit b004126

Please sign in to comment.