X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=plack%2Flib%2FPlack%2FApp%2FBookReader.pm;h=f3f26f32c77c8c76ea0efdafb1c075757b2066bf;hb=58cceebf12bef2963f1da327d4e8344359add18e;hp=0c07304c00853e788dc849d99e558483b79513a4;hpb=1652f5a0c2d3f14967c6fe2096a821bdc7ab17e9;p=bookreader.git diff --git a/plack/lib/Plack/App/BookReader.pm b/plack/lib/Plack/App/BookReader.pm index 0c07304..f3f26f3 100644 --- a/plack/lib/Plack/App/BookReader.pm +++ b/plack/lib/Plack/App/BookReader.pm @@ -9,8 +9,20 @@ use DirHandle; use URI::Escape; use Plack::Request; use Data::Dump qw(dump); -use File::Path qw(make_path); +use File::Path; use Graphics::Magick; +use File::Slurp; +use Image::Size; +use JSON; +use Time::Piece (); +use Time::Seconds 'ONE_YEAR'; + +sub make_basedir { + my $path = shift; + return if -e $path; + $path =~ s{/[^/]+$}{} || die "no dir/file in $path"; + File::Path::make_path $path; +} # Stolen from rack/directory.rb my $dir_file = "%s%s%s%s"; @@ -42,6 +54,159 @@ table { width:100%%; } PAGE +my $reader_page = <<'PAGE'; + + + + %s + + + + + + + + + + + + + + + + + + + +
+ Internet Archive BookReader
+ + +
+ + + + +PAGE + sub should_handle { my($self, $file) = @_; return -d $file || -f $file; @@ -63,12 +228,13 @@ sub return_dir_redirect { sub serve_path { my($self, $env, $path, $fullpath) = @_; + my $req = Plack::Request->new($env); + if (-f $path) { - my $req = Plack::Request->new($env); if ( my $reduce = $req->param('reduce') ) { $reduce = int($reduce); # BookReader javascript somethimes returns float - warn "# -scale 1/$reduce $path\n"; + warn "# reduce $reduce $path\n"; my $cache_path = "cache/$path.reduce.$reduce.jpg"; if ( $reduce <= 1 ) { @@ -81,6 +247,7 @@ sub serve_path { width => $w / $reduce, height => $h / $reduce ); + make_basedir $cache_path; $image->Write( filename => $cache_path ); warn "# created $cache_path ", -s $cache_path, " bytes\n"; } @@ -100,7 +267,7 @@ sub serve_path { my @files = ([ "../", "Parent Directory", '', '', '' ]); - my $dh = DirHandle->new($dir); + my $dh = DirHandle->new($path); my @children; while (defined(my $ent = $dh->read)) { next if $ent eq '.'; @@ -111,7 +278,7 @@ sub serve_path { for my $basename (sort { $a cmp $b } @children) { push @page_files, $basename if $basename =~ m/\.jpg$/; - my $file = "$dir/$basename"; + my $file = "$path/$basename"; my $url = $dir_url . $basename; my $is_dir = -d $file; @@ -129,16 +296,40 @@ sub serve_path { push @files, [ $url, $basename, $stat[7], $mime_type, HTTP::Date::time2str($stat[9]) ]; } - my $path = Plack::Util::encode_html( $env->{PATH_INFO} ); - my $files = join "\n", map { - my $f = $_; - sprintf $dir_file, map Plack::Util::encode_html($_), @$f; - } @files; + my $dir = Plack::Util::encode_html( $env->{PATH_INFO} ); + my $page = 'empty'; + + if ( $req->param('bookreader') ) { + + my $pages; + my $pages_path = "cache/$dir_url/bookreader.json"; + if ( 0 && -e $pages_path ) { + $pages = decode_json read_file $pages_path; + } else { + $pages = [ + grep { defined $_ } + map { + my ( $w, $h ) = imgsize("$path/$_"); + $w && $h ? [ "$dir_url/$_", $w, $h ] : undef + } sort { $a <=> $b } @page_files + ]; + make_basedir $pages_path; + write_file $pages_path => encode_json( $pages ); + warn "# created $pages_path ", -s $pages_path, " bytes\n"; + } + warn "# pages = ",dump($pages); + $page = sprintf $reader_page, $dir, encode_json( $pages ), $dir; # FIXME: title + + } else { + + my $files = join "\n", map { + my $f = $_; + sprintf $dir_file, map Plack::Util::encode_html($_), @$f; + } @files; + + $page = sprintf $dir_page, $dir, $dir, $files; - my $meta = { - page_urls => [ map { "$dir_url/$_" } sort { $a <=> $b } @page_files ], - }; - my $page = sprintf $dir_page, $path, $path, $files, dump( $meta ); + } return [ 200, ['Content-Type' => 'text/html; charset=utf-8'], [ $page ] ]; }