Skip to content

Instantly share code, notes, and snippets.

@ysasaki
Created March 9, 2010 15:33
Show Gist options
  • Save ysasaki/326702 to your computer and use it in GitHub Desktop.
Save ysasaki/326702 to your computer and use it in GitHub Desktop.
Mojolicious - send static file not in public path
package MyApp::Controller;
use strict;
use warnings;
use base 'Mojolicious::Controller';
use Path::Class;
sub sendfile {
my $self = shift;
my %args = @_;
if ( -e $args{file} and -r _ ) {
$self->res->headers->content_type($args{content_type});
$self->res->body(scalar file($args{file})->slurp);
$self->res->code(200) unless $self->res->code;
}
else {
$self->res->headers->content_type('text/plain');
$self->res->body('500 Internal ServerError');
$self->res->code(500);
}
}
package MyApp::Example;
use strict;
use warnings;
use base 'MyApp::Controller';
sub hoge {
my $self = shift;
$self->sendfile(
file => $self->app->home->rel_file('test.txt'),
content_type => 'text/plain'
);
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment