Skip to content

Commit

Permalink
Merge pull request #4 from bicycle1885/pull-req-multibyte
Browse files Browse the repository at this point in the history
enable to handle multibyte filenames
  • Loading branch information
koorchik committed Mar 22, 2013
2 parents 31517af + 86bdb2e commit 8b8c9e2
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/Mojolicious/Plugin/RenderFile.pm
Expand Up @@ -4,6 +4,7 @@ use Mojo::Base 'Mojolicious::Plugin';
use strict;
use warnings;
use File::Basename;
use Encode qw( encode decode_utf8 );
use Mojo::Util 'quote';

our $VERSION = '0.05';
Expand All @@ -15,7 +16,7 @@ sub register {
my $c = shift;
my %args = @_;

my $filename = $args{filename};
my $filename = decode_utf8($args{filename});
my $status = $args{status} || 200;
my $content_disposition = $args{content_disposition} || 'attachment';

Expand All @@ -26,7 +27,7 @@ sub register {

# Create asset
my $asset;
if ( my $filepath = $args{filepath} ) {
if ( my $filepath = decode_utf8($args{filepath}) ) {
unless ( -f $filepath && -r $filepath ) {
$c->app->log->error("Cannot read file [$filepath]. error [$!]");
return;
Expand All @@ -45,6 +46,7 @@ sub register {

# Create response headers
$filename = quote($filename); # quote the filename, per RFC 5987
$filename = encode("UTF-8", $filename);

my $headers = Mojo::Headers->new();
$headers->add( 'Content-Type', $content_type . ';name=' . $filename );
Expand Down
69 changes: 69 additions & 0 deletions t/multibyte_filename.t
@@ -0,0 +1,69 @@
use Mojo::Base -strict;

use Test::More;
use Encode;

use Mojolicious::Lite;
use Test::Mojo;

use lib '../lib';
use utf8;

use File::Basename qw/dirname/;
use File::Spec::Functions qw/rel2abs/;

my $FILE = rel2abs( dirname(__FILE__) . '/' . '漢字.txt' );

plugin 'RenderFile';

get "/default" => sub {
my $self = shift;
$self->render_file(
filepath => $FILE
);
};

get "/filename" => sub {
my $self = shift;
$self->render_file(
filepath => $FILE,
filename => '別名.txt',
);
};

get "/encoded" => sub {
my $self = shift;
$self->render_file(
filepath => encode_utf8 $FILE,
);
};

get "/encoded_filename" => sub {
my $self = shift;
$self->render_file(
filepath => encode_utf8 $FILE,
filename => encode_utf8 '別名.txt',
);
};


my $t = Test::Mojo->new;

$t->get_ok("/default")
->status_is(200)
->content_is( encode_utf8 '漢字(かんじ)は、古代中国に発祥を持つ文字。' )
->header_is( 'Content-Disposition' => encode_utf8 'attachment;filename="漢字.txt"' );

$t->get_ok("/filename")
->status_is(200)
->header_is( 'Content-Disposition' => encode_utf8 'attachment;filename="別名.txt"' );

$t->get_ok("/encoded")
->status_is(200)
->header_is( 'Content-Disposition' => encode_utf8 'attachment;filename="漢字.txt"' );

$t->get_ok("/encoded_filename")
->status_is(200)
->header_is( 'Content-Disposition' => encode_utf8 'attachment;filename="別名.txt"' );

done_testing();
1 change: 1 addition & 0 deletions t/漢字.txt
@@ -0,0 +1 @@
漢字(かんじ)は、古代中国に発祥を持つ文字。

0 comments on commit 8b8c9e2

Please sign in to comment.