Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mojolicious::Plugin::Directory patch for different user local locale (ex. Korean Windows cp949)
--- Directory.pm.org 2012-11-12 13:38:22.000000000 +0900
+++ Directory.pm 2013-03-31 00:23:52.000000000 +0900
@@ -5,6 +5,7 @@
use Cwd ();
use Encode ();
+use Encode::Locale;
use DirHandle;
use Mojo::Base qw{ Mojolicious::Plugin };
use Mojolicious::Types;
@@ -12,6 +13,7 @@
# Stolen from Plack::App::Direcotry
my $dir_page = <<'PAGE';
<html><head>
+ % $cur_path = Encode::decode('locale', $cur_path);
<title>Index of <%= $cur_path %></title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<style type='text/css'>
@@ -98,10 +100,10 @@
: ( { url => '../', name => 'Parent Directory', size => '', type => '', mtime => '' } );
my $children = list_files($dir);
- my $cur_path = Encode::decode_utf8( Mojo::Util::url_unescape( $c->req->url->path ) );
+ my $cur_path = Mojo::Util::url_unescape( $c->req->url->path );
for my $basename ( sort { $a cmp $b } @$children ) {
my $file = "$dir/$basename";
- my $url = Mojo::Path->new($cur_path)->trailing_slash(0);
+ my $url = Mojo::Path->new($cur_path)->charset(undef)->trailing_slash(0);
push @{ $url->parts }, $basename;
my $is_dir = -d $file;
@@ -119,7 +121,7 @@
push @files, {
url => $url,
- name => $basename,
+ name => Encode::decode('locale', $basename),
size => $stat[7] || 0,
type => $mime_type,
mtime => $mtime,
@@ -140,7 +142,7 @@
my @children;
while ( defined( my $ent = $dh->read ) ) {
next if $ent eq '.' or $ent eq '..';
- push @children, Encode::decode_utf8($ent);
+ push @children, $ent;
}
return [ @children ];
}
@aero

This comment has been minimized.

Copy link
Owner Author

@aero aero commented Mar 30, 2013

*Known issue.

  1. mojo 자체 데몬을 통해 띄우면 잘 동작하나. Plack을 통해서 띄우면 Request URL을 double encoding해버리는 문제가 있는것 같으나 거기까지는 아직 추적안해봤음
  2. 파일리스팅 root 디렉토리 이름에 로컬인코딩 문자열이 있는 경우 처리도 필요하나 그건 처리안됨.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.