remove common prefix before page sorting
[bookreader.git] / plack / lib / Plack / App / BookReader.pm
1 package Plack::App::BookReader;
2 use parent qw(Plack::App::File);
3 use strict;
4 use warnings;
5 use Plack::Util;
6 use HTTP::Date;
7 use Plack::MIME;
8 use DirHandle;
9 use URI::Escape;
10 use Plack::Request;
11 use Data::Dump qw(dump);
12 use File::Path qw(make_path remove_tree);
13 use Graphics::Magick;
14 use File::Slurp;
15 use JSON;
16 use autodie;
17 use Time::HiRes qw(time);
18
19 sub make_basedir {
20         my $path = shift;
21         return if -e $path;
22         $path =~ s{/[^/]+$}{} || die "no dir/file in $path";
23         File::Path::make_path $path;
24 }
25
26 # Stolen from rack/directory.rb
27 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>";
28 my $dir_page = <<PAGE;
29 <html><head>
30   <title>%s</title>
31   <meta http-equiv="content-type" content="text/html; charset=utf-8" />
32   <style type='text/css'>
33 table { width:100%%; }
34 .name { text-align:left; }
35 .size, .mtime { text-align:right; }
36 .type { width:11em; }
37 .mtime { width:15em; }
38   </style>
39 </head><body>
40 <h1>%s</h1>
41 <hr />
42 <table>
43   <tr>
44     <th class='name'>Name</th>
45     <th class='size'>Size</th>
46     <th class='type'>Type</th>
47     <th class='mtime'>Last Modified</th>
48   </tr>
49 %s
50 </table>
51 <hr />
52 <code>%s</code>
53 </body></html>
54 PAGE
55
56 my $reader_page = <<'PAGE';
57 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
58 <html>
59 <head>
60     <title>%s</title>
61     
62     <link rel="stylesheet" type="text/css" href="/BookReader/BookReader.css"/>
63     <script type="text/javascript" src="http://www.archive.org/includes/jquery-1.4.2.min.js"></script>
64     <script type="text/javascript" src="http://www.archive.org/bookreader/jquery-ui-1.8.5.custom.min.js"></script>
65
66     <script type="text/javascript" src="http://www.archive.org/bookreader/dragscrollable.js"></script>
67     <script type="text/javascript" src="http://www.archive.org/bookreader/jquery.colorbox-min.js"></script>
68     <script type="text/javascript" src="http://www.archive.org/bookreader/jquery.ui.ipad.js"></script>
69     <script type="text/javascript" src="http://www.archive.org/bookreader/jquery.bt.min.js"></script>
70
71     <script type="text/javascript" src="/BookReader/BookReader.js"></script>
72
73 <style type="text/css">
74
75 /* Hide print and embed functionality */
76 #BRtoolbar .embed, .print {
77     display: none;
78 }
79
80 </style>
81
82 <script type="text/javascript">
83 $(document).ready( function() {
84
85 // 
86 // This file shows the minimum you need to provide to BookReader to display a book
87 //
88 // Copyright(c)2008-2009 Internet Archive. Software license AGPL version 3.
89
90 // Create the BookReader object
91 var br = new BookReader();
92
93 var pages = %s;
94
95 // Return the width of a given page.  Here we assume all images are 800 pixels wide
96 br.getPageWidth = function(index) {
97         if ( ! pages[index] ) return;
98     return parseInt( pages[index][1] );
99 }
100
101 // Return the height of a given page.  Here we assume all images are 1200 pixels high
102 br.getPageHeight = function(index) {
103         if ( ! pages[index] ) return;
104     return parseInt( pages[index][2] );
105 }
106
107 // We load the images from archive.org -- you can modify this function to retrieve images
108 // using a different URL structure
109 br.getPageURI = function(index, reduce, rotate) {
110         if ( ! pages[index] ) return;
111     // reduce and rotate are ignored in this simple implementation, but we
112     // could e.g. look at reduce and load images from a different directory
113     // or pass the information to an image server
114         var url = pages[index][0] + '?reduce='+reduce;
115         console.debug('getPageURI', index, reduce, rotate, url);
116     return url;
117 }
118
119 // Return which side, left or right, that a given page should be displayed on
120 br.getPageSide = function(index) {
121     if (0 == (index & 0x1)) {
122         return 'R';
123     } else {
124         return 'L';
125     }
126 }
127
128 // This function returns the left and right indices for the user-visible
129 // spread that contains the given index.  The return values may be
130 // null if there is no facing page or the index is invalid.
131 br.getSpreadIndices = function(pindex) {   
132     var spreadIndices = [null, null]; 
133     if ('rl' == this.pageProgression) {
134         // Right to Left
135         if (this.getPageSide(pindex) == 'R') {
136             spreadIndices[1] = pindex;
137             spreadIndices[0] = pindex + 1;
138         } else {
139             // Given index was LHS
140             spreadIndices[0] = pindex;
141             spreadIndices[1] = pindex - 1;
142         }
143     } else {
144         // Left to right
145         if (this.getPageSide(pindex) == 'L') {
146             spreadIndices[0] = pindex;
147             spreadIndices[1] = pindex + 1;
148         } else {
149             // Given index was RHS
150             spreadIndices[1] = pindex;
151             spreadIndices[0] = pindex - 1;
152         }
153     }
154     
155     return spreadIndices;
156 }
157
158 // For a given "accessible page index" return the page number in the book.
159 //
160 // For example, index 5 might correspond to "Page 1" if there is front matter such
161 // as a title page and table of contents.
162 br.getPageNum = function(index) {
163     return index+1;
164 }
165
166 // Total number of leafs
167 br.numLeafs = pages.length;
168
169 // Book title and the URL used for the book title link
170 br.bookTitle= '%s';
171 br.bookUrl  = '%s';
172
173 // Override the path used to find UI images
174 br.imagesBaseURL = '/BookReader/images/';
175
176 br.getEmbedCode = function(frameWidth, frameHeight, viewParams) {
177     return "Embed code not supported in bookreader demo.";
178 }
179
180 // Let's go!
181 br.init();
182
183 // read-aloud and search need backend compenents and are not supported in the demo
184 $('#BRtoolbar').find('.read').hide();
185 $('#textSrch').hide();
186 $('#btnSrch').hide();
187
188 } );
189 </script>
190
191 </head>
192 <body style="background-color: ##939598;">
193
194 <div id="BookReader">
195     Internet Archive BookReader<br/>
196     
197     <noscript>
198     <p>
199         The BookReader requires JavaScript to be enabled. Please check that your browser supports JavaScript and that it is enabled in the browser settings.
200     </p>
201     </noscript>
202 </div>
203
204
205 </body>
206 </html>
207 PAGE
208
209 sub should_handle {
210     my($self, $file) = @_;
211     return -d $file || -f $file;
212 }
213
214 sub return_dir_redirect {
215     my ($self, $env) = @_;
216     my $uri = Plack::Request->new($env)->uri;
217     return [ 301,
218         [
219             'Location' => $uri . '/',
220             'Content-Type' => 'text/plain',
221             'Content-Length' => 8,
222         ],
223         [ 'Redirect' ],
224     ];
225 }
226
227 sub convert {
228         warn "# convert ",dump(@_);
229         my $t = time();
230         system 'gm', 'convert', @_;
231         $t = time() - $t;
232         warn sprintf("## created %d bytes in %.2f s %s\n", -s $_[-1], $t, $_[-1]);
233 }
234
235 sub longest_common_prefix {
236            my $prefix = shift;
237         for (@_) {
238                 chop $prefix while (! /^\Q$prefix\E/i);
239         }
240         warn "# longest_common_prefix [$prefix]\n";
241         return $prefix;
242 }
243
244
245 sub serve_path {
246     my($self, $env, $path, $fullpath) = @_;
247
248         my $req = Plack::Request->new($env);
249
250     my $dir_url = $env->{SCRIPT_NAME} . $env->{PATH_INFO};
251
252     if (-f $path) {
253
254                 if ( my $reduce = $req->param('reduce') ) {
255                         $reduce = int($reduce); # BookReader javascript somethimes returns float
256                         warn "# reduce $reduce $path\n";
257
258                         my $cache_path = "cache/$dir_url.reduce.$reduce.jpg";
259                         if ( $reduce <= 1 && $path =~ m/\.jpe?g$/ ) {
260                                 $cache_path = $path;
261                         } elsif ( ! -e $cache_path ) {
262                                 make_basedir $cache_path;
263                                 convert( '-scale', ( 100 / $reduce ) .'%', $path => $cache_path );
264                         }
265
266                 return $self->SUPER::serve_path($env, $cache_path, $fullpath);
267
268                 }
269
270         return $self->SUPER::serve_path($env, $path, $fullpath);
271     }
272
273     if ($dir_url !~ m{/$}) {
274         return $self->return_dir_redirect($env);
275     }
276
277     my @files = ();
278
279     my $dh = DirHandle->new($path);
280     my @children;
281     while (defined(my $ent = $dh->read)) {
282         next if $ent eq '.';
283         push @children, $ent;
284     }
285
286         my @page_files;
287
288     for my $basename (sort { $a cmp $b } @children) {
289                 push @page_files, $basename if $basename =~ m/\d+\D?\.(jpg|gif|pdf)$/;
290         my $file = "$path/$basename";
291         my $url = $dir_url . $basename;
292
293         my $is_dir = -d $file;
294         my @stat = stat _;
295
296
297         $url = join '/', map {uri_escape($_)} split m{/}, $url;
298
299         if ($is_dir) {
300             $basename .= "/";
301             $url      .= "/";
302         }
303
304         my $mime_type = $is_dir ? 'directory' : ( Plack::MIME->mime_type($file) || 'text/plain' );
305         push @files, [ $url, $basename, $stat[7], $mime_type, HTTP::Date::time2str($stat[9]) ];
306     }
307
308         if ( @page_files ) {
309                 my $prefix = longest_common_prefix @page_files;
310                 @page_files = sort {
311                                         my ( $an,$bn ) = ( $a,$b );
312                                         $an =~ s/^\Q$prefix\E//i; $an =~ s/\D+//g;
313                                         $bn =~ s/^\Q$prefix\E//i; $bn =~ s/\D+//g;
314                                         warn "## sort [$a] $an <=> $bn [$b]\n";
315                                         $an <=> $bn;
316                 } @page_files;
317                 warn "# page_files = ",dump( @page_files );
318         }
319
320     my $dir  = Plack::Util::encode_html( $env->{PATH_INFO} );
321         my $page = 'empty';
322
323         if ( $req->param('bookreader') ) {
324
325                 my $pages; # []
326                 my $pages_path = "cache/$dir_url/bookreader.json";
327                 if ( -e $pages_path ) {
328                         $pages = decode_json read_file $pages_path;
329                 } else {
330                         foreach my $page ( @page_files ) {
331                                 my $image = Graphics::Magick->new;
332                                 if ( $page =~ m/\.pdf$/ ) {
333                                         my $cache_dir = "cache/$dir_url/$page/";
334                                         make_path $cache_dir;
335                                         warn "# pdfimages $path/$page -> $cache_dir";
336                                         system 'pdfimages', '-q', '-j', '-p', "$path/$page", $cache_dir;
337
338                                         # glob split on spaces!
339                                         opendir(my $dh, $cache_dir);
340                                         while (readdir($dh)) {
341                                                 warn "## readdir = [$_]\n";
342                                                 my $page = "$cache_dir/$_";
343                                                 next unless -f $page; # skip . ..
344
345                                                 if ( $page !~ m/\.jpg$/ ) {
346                                                         convert( $page => $page . '.jpg' );
347                                                         unlink $page;
348                                                         $page .= '.jpg';
349                                                 }
350
351                                                 warn "## ping $page\n";
352                                                 die "$page: $!" unless -r $page;
353                                                 my ( $w, $h, $size, $format ) = $image->ping($page);
354                                                 warn "## image size $w*$h $size $format $page\n";
355                                                 push @$pages, [ "/$page", $w, $h ] if $w && $h;
356                                         }
357                                         closedir $dh;
358
359                                 } else {
360                                         die "$path/$page: $!" unless -r "$path/$page";
361                                         my ( $w, $h, $size, $format ) = $image->ping("$path/$page");
362                                         warn "# image size $w*$h $size $format $path/$page\n";
363                                         push @$pages, [ "$dir_url/$page", $w, $h ] if $w && $h;
364                                 }
365                         }
366                         make_basedir $pages_path;
367                         write_file $pages_path => encode_json( $pages );
368                         warn "# created $pages_path ", -s $pages_path, " bytes\n";
369                 }
370                 warn "# pages = ",dump($pages);
371                 $page = sprintf $reader_page, $dir, encode_json( $pages ), $dir, '..';
372
373         } else {
374
375                 my $files = join "\n", map {
376                         my $f = $_;
377                         sprintf $dir_file, map Plack::Util::encode_html($_), @$f;
378                 } @files;
379
380                 $page = sprintf $dir_page, $dir, $dir, $files, 
381                         @page_files ? '<form><input type=submit name=bookreader value="Read"></form>' . dump( [ @page_files ] ) : '';
382
383         }
384
385     return [ 200, ['Content-Type' => 'text/html; charset=utf-8'], [ $page ] ];
386 }
387
388 1;
389
390 __END__
391
392 =head1 NAME
393
394 Plack::App::BookReader - Internet Archive Book Reader with directory index
395
396 =head1 SYNOPSIS
397
398   # app.psgi
399   use Plack::App::BookReader;
400   my $app = Plack::App::BookReader->new({ root => "/path/to/htdocs" })->to_app;
401
402 =head1 DESCRIPTION
403
404 This is a static file server PSGI application with directory index a la Apache's mod_autoindex.
405
406 =head1 CONFIGURATION
407
408 =over 4
409
410 =item root
411
412 Document root directory. Defaults to the current directory.
413
414 =back
415
416 =head1 AUTHOR
417
418 Dobrica Pavlinusic
419 Tatsuhiko Miyagawa (based on L<Plack::App::Directory>
420
421 =head1 SEE ALSO
422
423 L<Plack::App::File>
424
425 =cut
426