| 1 | # | ||
| 2 | # bold copied from Mojolicious::Plugin::RenderFile - because there are no rpm packages | ||
| 3 | # | ||
| 4 | package Rex::Repositorio::Server::Helper::RenderFile; | ||
| 5 | use Mojo::Base 'Mojolicious::Plugin'; | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | use File::Basename; | ||
| 10 | use Encode qw( encode decode_utf8 ); | ||
| 11 | use Mojo::Util 'quote'; | ||
| 12 | |||
| 13 | our $VERSION = '1.2.1'; # VERSION | ||
| 14 | |||
| 15 | sub register { | ||
| 16 | my ( $self, $app ) = @_; | ||
| 17 | |||
| 18 | $app->helper( | ||
| 19 | 'render_file' => sub { | ||
| 20 | my $c = shift; | ||
| 21 | my %args = @_; | ||
| 22 | |||
| 23 | utf8::decode( $args{filename} ) | ||
| 24 | if $args{filename} && !utf8::is_utf8( $args{filename} ); | ||
| 25 | utf8::decode( $args{filepath} ) | ||
| 26 | if $args{filepath} && !utf8::is_utf8( $args{filepath} ); | ||
| 27 | |||
| 28 | my $filename = $args{filename}; | ||
| 29 | my $status = $args{status} || 200; | ||
| 30 | my $content_disposition = $args{content_disposition} || 'attachment'; | ||
| 31 | my $cleanup = $args{cleanup} // 0; | ||
| 32 | |||
| 33 | # Content type based on format | ||
| 34 | my $content_type; | ||
| 35 | $content_type = $c->app->types->type( $args{format} ) if $args{format}; | ||
| 36 | $content_type ||= 'application/x-download'; | ||
| 37 | |||
| 38 | # Create asset | ||
| 39 | my $asset; | ||
| 40 | if ( my $filepath = $args{filepath} ) { | ||
| 41 | unless ( -f $filepath && -r $filepath ) { | ||
| 42 | $c->app->log->error("Cannot read file [$filepath]. error [$!]"); | ||
| 43 | return; | ||
| 44 | } | ||
| 45 | |||
| 46 | $filename ||= fileparse($filepath); | ||
| 47 | $asset = Mojo::Asset::File->new( path => $filepath ); | ||
| 48 | $asset->cleanup($cleanup); | ||
| 49 | } | ||
| 50 | elsif ( $args{data} ) { | ||
| 51 | $filename ||= $c->req->url->path->parts->[-1] || 'download'; | ||
| 52 | $asset = Mojo::Asset::Memory->new(); | ||
| 53 | $asset->add_chunk( $args{data} ); | ||
| 54 | } | ||
| 55 | else { | ||
| 56 | $c->app->log->error('You must provide "data" or "filepath" option'); | ||
| 57 | return; | ||
| 58 | } | ||
| 59 | |||
| 60 | # Create response headers | ||
| 61 | $filename = quote($filename); # quote the filename, per RFC 5987 | ||
| 62 | $filename = encode( "UTF-8", $filename ); | ||
| 63 | |||
| 64 | my $headers = Mojo::Headers->new(); | ||
| 65 | $headers->add( 'Content-Type', $content_type . ';name=' . $filename ); | ||
| 66 | $headers->add( 'Content-Disposition', | ||
| 67 | $content_disposition . ';filename=' . $filename ); | ||
| 68 | |||
| 69 | # Range | ||
| 70 | # Partially based on Mojolicious::Static | ||
| 71 | if ( my $range = $c->req->headers->range ) { | ||
| 72 | my $start = 0; | ||
| 73 | my $size = $asset->size; | ||
| 74 | my $end = $size - 1 >= 0 ? $size - 1 : 0; | ||
| 75 | |||
| 76 | # Check range | ||
| 77 | if ( $range =~ m/^bytes=(\d+)-(\d+)?/ && $1 <= $end ) { | ||
| 78 | $start = $1; | ||
| 79 | $end = $2 if defined $2 && $2 <= $end; | ||
| 80 | |||
| 81 | $status = 206; | ||
| 82 | $headers->add( 'Content-Length' => $end - $start + 1 ); | ||
| 83 | $headers->add( 'Content-Range' => "bytes $start-$end/$size" ); | ||
| 84 | } | ||
| 85 | else { | ||
| 86 | # Not satisfiable | ||
| 87 | return $c->rendered(416); | ||
| 88 | } | ||
| 89 | |||
| 90 | # Set range for asset | ||
| 91 | $asset->start_range($start)->end_range($end); | ||
| 92 | } | ||
| 93 | else { | ||
| 94 | $headers->add( 'Content-Length' => $asset->size ); | ||
| 95 | } | ||
| 96 | |||
| 97 | # Set response headers | ||
| 98 | $c->res->content->headers($headers); | ||
| 99 | |||
| 100 | # Stream content directly from file | ||
| 101 | $c->res->content->asset($asset); | ||
| 102 | return $c->rendered($status); | ||
| 103 | } | ||
| 104 | ); | ||
| 105 | } | ||
| 106 | |||
| 107 | 1; | ||
| 108 | |||
| 109 | =head1 NAME | ||
| 110 | |||
| 111 | Mojolicious::Plugin::RenderFile - "render_file" helper for Mojolicious | ||
| 112 | |||
| 113 | =head1 SYNOPSIS | ||
| 114 | |||
| 115 | # Mojolicious | ||
| 116 | $self->plugin('RenderFile'); | ||
| 117 | |||
| 118 | # Mojolicious::Lite | ||
| 119 | plugin 'RenderFile'; | ||
| 120 | |||
| 121 | # In controller | ||
| 122 | $self->render_file('filepath' => '/tmp/files/file.pdf'); # file name will be "file.pdf" | ||
| 123 | |||
| 124 | # Provide any file name | ||
| 125 | $self->render_file('filepath' => '/tmp/files/file.pdf', 'filename' => 'report.pdf'); | ||
| 126 | |||
| 127 | # Render data from memory as file | ||
| 128 | $self->render_file('data' => 'some data here', 'filename' => 'report.pdf'); | ||
| 129 | |||
| 130 | # Open file in browser(do not show save dialog) | ||
| 131 | $self->render_file( | ||
| 132 | 'filepath' => '/tmp/files/file.pdf', | ||
| 133 | 'format' => 'pdf', # will change Content-Type "application/x-download" to "application/pdf" | ||
| 134 | 'content_disposition' => 'inline', # will change Content-Disposition from "attachment" to "inline" | ||
| 135 | 'cleanup' => 1, # delete file after completed | ||
| 136 | ); | ||
| 137 | |||
| 138 | =head1 DESCRIPTION | ||
| 139 | |||
| 140 | L<Mojolicious::Plugin::RenderFile> is a L<Mojolicious> plugin that adds "render_file" helper. It does not read file in memory and just streaming it to a client. | ||
| 141 | |||
| 142 | =head1 HELPERS | ||
| 143 | |||
| 144 | =head2 C<render_file> | ||
| 145 | |||
| 146 | $self->render_file(filepath => '/tmp/files/file.pdf', 'filename' => 'report.pdf' ); | ||
| 147 | |||
| 148 | With this helper you can easily provide files for download. By default "Content-Type" header is "application/x-download" and "content_disposition" option value is "attachment". | ||
| 149 | Therefore, a browser will ask where to save file. You can provide "format" option to change "Content-Type" header. | ||
| 150 | |||
| 151 | |||
| 152 | =head3 Supported Options: | ||
| 153 | |||
| 154 | =over | ||
| 155 | |||
| 156 | =item C<filepath> | ||
| 157 | |||
| 158 | Path on the filesystem to the file. You must always pass "filepath" or "data" option | ||
| 159 | |||
| 160 | =item C<data> | ||
| 161 | |||
| 162 | Binary content which will be transfered to browser. You must always pass "filepath" or "data" option | ||
| 163 | |||
| 164 | =item C<filename> (optional) | ||
| 165 | |||
| 166 | Browser will use this name for saving the file | ||
| 167 | |||
| 168 | =item C<format> (optional) | ||
| 169 | |||
| 170 | The "Content-Type" header is based on the MIME type mapping of the "format" option value. These mappings can be easily extended or changed with L<Mojolicious/"types">. | ||
| 171 | |||
| 172 | By default "Content-Type" header is "application/x-download" | ||
| 173 | |||
| 174 | =item C<content_disposition> (optional) | ||
| 175 | |||
| 176 | Tells browser how to present the file. | ||
| 177 | |||
| 178 | "attachment" (default) - is for dowloading | ||
| 179 | |||
| 180 | "inline" - is for showing file inline | ||
| 181 | |||
| 182 | =item C<cleanup> (optional) | ||
| 183 | |||
| 184 | Indicates if the file should be deleted when rendering is complete | ||
| 185 | |||
| 186 | =back | ||
| 187 | |||
| 188 | This plugin respects HTTP Range headers. | ||
| 189 | |||
| 190 | =head1 AUTHOR | ||
| 191 | |||
| 192 | Viktor Turskyi <koorchik@cpan.org> | ||
| 193 | |||
| 194 | =head1 CONTRIBUTORS | ||
| 195 | |||
| 196 | Nils Diewald (Akron) | ||
| 197 | |||
| 198 | =head1 BUGS | ||
| 199 | |||
| 200 | Please report any bugs or feature requests to Github L<https://github.com/koorchik/Mojolicious-Plugin-RenderFile> | ||
| 201 | |||
| 202 | =head1 SEE ALSO | ||
| 203 | |||
| 204 | L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>. | ||
| 205 | |||
| 206 | Copyright 2011 Viktor Turskyi | ||
| 207 | |||
| 208 | This program is free software; you can redistribute it and/or modify it | ||
| 209 | under the terms of either: the GNU General Public License as published | ||
| 210 | by the Free Software Foundation; or the Artistic License. | ||
| 211 | |||
| 212 | See http://dev.perl.org/licenses/ for more information. | ||
| 213 | |||
| 214 | =cut |