1 package Plack::App::BookReader;
2 use parent qw(Plack::App::File);
11 use Data::Dump qw(dump);
12 use File::Path qw(make_path);
15 # Stolen from rack/directory.rb
16 my $dir_file = "<tr><td class='name'><a href='%s'>%s</a></td><td class='size'>%s</td><td class='type'>%s</td><td class='mtime'>%s</td></tr>";
17 my $dir_page = <<PAGE;
20 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
21 <style type='text/css'>
22 table { width:100%%; }
23 .name { text-align:left; }
24 .size, .mtime { text-align:right; }
26 .mtime { width:15em; }
33 <th class='name'>Name</th>
34 <th class='size'>Size</th>
35 <th class='type'>Type</th>
36 <th class='mtime'>Last Modified</th>
46 my($self, $file) = @_;
47 return -d $file || -f $file;
50 sub return_dir_redirect {
51 my ($self, $env) = @_;
52 my $uri = Plack::Request->new($env)->uri;
55 'Location' => $uri . '/',
56 'Content-Type' => 'text/plain',
57 'Content-Length' => 8,
64 my($self, $env, $path, $fullpath) = @_;
68 my $req = Plack::Request->new($env);
69 if ( my $reduce = $req->param('reduce') ) {
70 $reduce = int($reduce); # BookReader javascript somethimes returns float
71 warn "# -scale 1/$reduce $path\n";
73 my $cache_path = "cache/$path.reduce.$reduce.jpg";
76 } elsif ( ! -e $cache_path ) {
77 my $image = Graphics::Magick->new( magick => 'jpg' );
79 my ( $w, $h ) = $image->Get('width','height');
81 width => $w / $reduce,
82 height => $h / $reduce
84 $image->Write( filename => $cache_path );
85 warn "# created $cache_path ", -s $cache_path, " bytes\n";
88 return $self->SUPER::serve_path($env, $cache_path, $fullpath);
92 return $self->SUPER::serve_path($env, $path, $fullpath);
95 my $dir_url = $env->{SCRIPT_NAME} . $env->{PATH_INFO};
97 if ($dir_url !~ m{/$}) {
98 return $self->return_dir_redirect($env);
101 my @files = ([ "../", "Parent Directory", '', '', '' ]);
103 my $dh = DirHandle->new($path);
105 while (defined(my $ent = $dh->read)) {
107 push @children, $ent;
112 for my $basename (sort { $a cmp $b } @children) {
113 push @page_files, $basename if $basename =~ m/\.jpg$/;
114 my $file = "$path/$basename";
115 my $url = $dir_url . $basename;
117 my $is_dir = -d $file;
121 $url = join '/', map {uri_escape($_)} split m{/}, $url;
128 my $mime_type = $is_dir ? 'directory' : ( Plack::MIME->mime_type($file) || 'text/plain' );
129 push @files, [ $url, $basename, $stat[7], $mime_type, HTTP::Date::time2str($stat[9]) ];
132 my $dir = Plack::Util::encode_html( $env->{PATH_INFO} );
133 my $files = join "\n", map {
135 sprintf $dir_file, map Plack::Util::encode_html($_), @$f;
139 page_urls => [ map { "$dir_url/$_" } sort { $a <=> $b } @page_files ],
141 my $page = sprintf $dir_page, $dir, $dir, $files, dump( $meta );
143 return [ 200, ['Content-Type' => 'text/html; charset=utf-8'], [ $page ] ];
152 Plack::App::BookReader - Internet Archive Book Reader with directory index
157 use Plack::App::BookReader;
158 my $app = Plack::App::BookReader->new({ root => "/path/to/htdocs" })->to_app;
162 This is a static file server PSGI application with directory index a la Apache's mod_autoindex.
170 Document root directory. Defaults to the current directory.
177 Tatsuhiko Miyagawa (based on L<Plack::App::Directory>