rename variables dir -> path
[bookreader.git] / plack / lib / Plack / App / BookReader.pm
index a71b69b..90402e4 100644 (file)
@@ -9,6 +9,8 @@ use DirHandle;
 use URI::Escape;
 use Plack::Request;
 use Data::Dump qw(dump);
+use File::Path qw(make_path);
+use Graphics::Magick;
 
 # Stolen from rack/directory.rb
 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>";
@@ -59,13 +61,35 @@ sub return_dir_redirect {
 }
 
 sub serve_path {
-    my($self, $env, $dir, $fullpath) = @_;
-
-    if (-f $dir) {
-
-               if (
-
-        return $self->SUPER::serve_path($env, $dir, $fullpath);
+    my($self, $env, $path, $fullpath) = @_;
+
+    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";
+
+                       my $cache_path = "cache/$path.reduce.$reduce.jpg";
+                       if ( $reduce <= 1 ) {
+                               $cache_path = $path;
+                       } elsif ( ! -e $cache_path ) {
+                               my $image = Graphics::Magick->new( magick => 'jpg' );
+                               $image->Read($path);
+                               my ( $w, $h ) = $image->Get('width','height');
+                               $image->Resize(
+                                       width  => $w / $reduce,
+                                       height => $h / $reduce
+                               );
+                               $image->Write( filename => $cache_path );
+                               warn "# created $cache_path ", -s $cache_path, " bytes\n";
+                       }
+
+               return $self->SUPER::serve_path($env, $cache_path, $fullpath);
+
+               }
+
+        return $self->SUPER::serve_path($env, $path, $fullpath);
     }
 
     my $dir_url = $env->{SCRIPT_NAME} . $env->{PATH_INFO};
@@ -76,7 +100,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 '.';
@@ -87,7 +111,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;
@@ -105,7 +129,7 @@ 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 $dir  = Plack::Util::encode_html( $env->{PATH_INFO} );
     my $files = join "\n", map {
         my $f = $_;
         sprintf $dir_file, map Plack::Util::encode_html($_), @$f;
@@ -114,7 +138,7 @@ sub serve_path {
        my $meta = {
                page_urls => [ map { "$dir_url/$_" } sort { $a <=> $b } @page_files ],
        };
-    my $page  = sprintf $dir_page, $path, $path, $files, dump( $meta );
+    my $page  = sprintf $dir_page, $dir, $dir, $files, dump( $meta );
 
     return [ 200, ['Content-Type' => 'text/html; charset=utf-8'], [ $page ] ];
 }