Created
May 15, 2012 05:38
-
-
Save koorchik/2699369 to your computer and use it in GitHub Desktop.
render_file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $app->helper('render_file' => sub { | |
| my $c = shift; | |
| my %args = @_; | |
| my $filepath = $args{filepath}; | |
| unless ( -f $filepath && -r $filepath ) { | |
| $c->app->log->error("Cannot read file [$filepath]. error [$!]"); | |
| return; | |
| } | |
| my $filename = $args{filename} || fileparse($filepath); | |
| my $status = $args{status} || 200; | |
| my $headers = Mojo::Headers->new(); | |
| $headers->add( 'Content-Type', 'application/x-download;name=' . $filename ); | |
| $headers->add( 'Content-Disposition', 'attachment;filename=' . $filename ); | |
| $c->res->content->headers($headers); | |
| # Stream content directly from file | |
| $c->res->content->asset( Mojo::Asset::File->new( path => $filepath ) ); | |
| return $c->rendered($status); | |
| } ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment