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 remove_tree);
17 use Time::HiRes qw(time);
23 $path =~ s{/[^/]+$}{} || die "no dir/file in $path";
24 warn "# make_basedir $path\n";
25 -e $path ? 0 : File::Path::make_path $path;
28 # Stolen from rack/directory.rb
29 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>";
30 my $dir_page = <<PAGE;
33 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
34 <style type='text/css'>
35 table { width:100%%; }
36 .name { text-align:left; }
37 .size, .mtime { text-align:right; }
39 .mtime { width:15em; }
46 <th class='name'>Name</th>
47 <th class='size'>Size</th>
48 <th class='type'>Type</th>
49 <th class='mtime'>Last Modified</th>
58 my $reader_page = <<'PAGE';
59 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
64 <link rel="stylesheet" type="text/css" href="/BookReader/BookReader.css"/>
65 <script type="text/javascript" src="http://www.archive.org/includes/jquery-1.4.2.min.js"></script>
66 <script type="text/javascript" src="http://www.archive.org/bookreader/jquery-ui-1.8.5.custom.min.js"></script>
68 <script type="text/javascript" src="http://www.archive.org/bookreader/dragscrollable.js"></script>
69 <script type="text/javascript" src="http://www.archive.org/bookreader/jquery.colorbox-min.js"></script>
70 <script type="text/javascript" src="http://www.archive.org/bookreader/jquery.ui.ipad.js"></script>
71 <script type="text/javascript" src="http://www.archive.org/bookreader/jquery.bt.min.js"></script>
73 <script type="text/javascript" src="/BookReader/BookReader.js"></script>
75 <style type="text/css">
77 /* Hide print and embed functionality */
78 #BRtoolbar .embed, .print {
84 <script type="text/javascript">
85 $(document).ready( function() {
88 // This file shows the minimum you need to provide to BookReader to display a book
90 // Copyright(c)2008-2009 Internet Archive. Software license AGPL version 3.
92 // Create the BookReader object
93 var br = new BookReader();
97 // Return the width of a given page. Here we assume all images are 800 pixels wide
98 br.getPageWidth = function(index) {
99 if ( ! pages[index] ) return;
100 return parseInt( pages[index][1] );
103 // Return the height of a given page. Here we assume all images are 1200 pixels high
104 br.getPageHeight = function(index) {
105 if ( ! pages[index] ) return;
106 return parseInt( pages[index][2] );
109 // We load the images from archive.org -- you can modify this function to retrieve images
110 // using a different URL structure
111 br.getPageURI = function(index, reduce, rotate) {
112 if ( ! pages[index] ) return;
113 // reduce and rotate are ignored in this simple implementation, but we
114 // could e.g. look at reduce and load images from a different directory
115 // or pass the information to an image server
116 var url = pages[index][0] + '?reduce='+reduce;
117 console.debug('getPageURI', index, reduce, rotate, url);
121 // Return which side, left or right, that a given page should be displayed on
122 br.getPageSide = function(index) {
123 if (0 == (index & 0x1)) {
130 // This function returns the left and right indices for the user-visible
131 // spread that contains the given index. The return values may be
132 // null if there is no facing page or the index is invalid.
133 br.getSpreadIndices = function(pindex) {
134 var spreadIndices = [null, null];
135 if ('rl' == this.pageProgression) {
137 if (this.getPageSide(pindex) == 'R') {
138 spreadIndices[1] = pindex;
139 spreadIndices[0] = pindex + 1;
141 // Given index was LHS
142 spreadIndices[0] = pindex;
143 spreadIndices[1] = pindex - 1;
147 if (this.getPageSide(pindex) == 'L') {
148 spreadIndices[0] = pindex;
149 spreadIndices[1] = pindex + 1;
151 // Given index was RHS
152 spreadIndices[1] = pindex;
153 spreadIndices[0] = pindex - 1;
157 return spreadIndices;
160 // For a given "accessible page index" return the page number in the book.
162 // For example, index 5 might correspond to "Page 1" if there is front matter such
163 // as a title page and table of contents.
164 br.getPageNum = function(index) {
168 // Total number of leafs
169 br.numLeafs = pages.length;
171 // Book title and the URL used for the book title link
175 // Override the path used to find UI images
176 br.imagesBaseURL = '/BookReader/images/';
178 br.getEmbedCode = function(frameWidth, frameHeight, viewParams) {
179 return "Embed code not supported in bookreader demo.";
185 // read-aloud and search need backend compenents and are not supported in the demo
186 $('#BRtoolbar').find('.read').hide();
187 $('#textSrch').hide();
188 $('#btnSrch').hide();
194 <body style="background-color: ##939598;">
196 <div id="BookReader">
197 Internet Archive BookReader<br/>
201 The BookReader requires JavaScript to be enabled. Please check that your browser supports JavaScript and that it is enabled in the browser settings.
212 my($self, $file) = @_;
213 return -d $file || -f $file;
216 sub return_dir_redirect {
217 my ($self, $env) = @_;
218 my $uri = Plack::Request->new($env)->uri;
221 'Location' => $uri . '/',
222 'Content-Type' => 'text/plain',
223 'Content-Length' => 8,
229 sub convert { gm('convert',@_) }
230 sub montage { gm('montage',@_) }
234 warn "# $command ",dump(@_);
236 system 'gm', $command, @_;
238 warn sprintf("## $command %d bytes in %.2f s %s\n", -s $_[-1], $t, $_[-1]);
241 sub longest_common_prefix {
244 chop $prefix while (! /^\Q$prefix\E/i);
246 warn "# longest_common_prefix [$prefix]\n";
251 my $prefix = longest_common_prefix @_;
253 my ( $an,$bn ) = ( $a,$b );
254 $an =~ s/^\Q$prefix\E//i; $an =~ s/\D+//g;
255 $bn =~ s/^\Q$prefix\E//i; $bn =~ s/\D+//g;
256 warn "## sort [$a] $an <=> $bn [$b]\n";
261 sub convert_pdf_page {
262 my ($pdf, $page, $path) = @_;
267 warn "# pdfimages $page $pdf -> $path/\n";
268 system 'pdfimages', '-f', $page, '-l', $page, '-q', '-j', '-p', $pdf, "$path/p";
271 # glob split on spaces!
272 opendir(my $dh, $path);
273 while (readdir($dh)) {
274 my $full = "$path/$_";
275 warn "## readdir $full\n";
276 next unless -f $full; # skip . ..
281 die "can't find images for $pdf in $path" unless $#parts >= 0;
283 @parts = sort_pages @parts;
285 my $image = "$path.jpg";
287 if ( $#parts == 0 ) { # single image
288 my $part = "$path/$parts[0]";
289 convert( $part => $image );
291 my @full = map { "$path/$_" } @parts;
292 montage( @full, '-tile', '1x'.scalar(@full), '-geometry', '+1+1' => $image );
295 die "$image: $!" unless -r $image;
300 warn sprintf("## page: %d in %.2f s for %s\n", $page, $t, $image);
305 my($self, $env, $path, $fullpath) = @_;
307 my $req = Plack::Request->new($env);
309 my $dir_url = $env->{SCRIPT_NAME} . $env->{PATH_INFO};
313 if ( -f $path && $path =~ s{/([^/]+\.pdf)$}{} ) {
314 push @page_files, $1;
315 warn "# single pdf: $path / $1\n";
316 } elsif (-f $path ) {
318 if ( my $reduce = $req->param('reduce') ) {
319 $reduce = int($reduce); # BookReader javascript somethimes returns float
320 warn "# reduce $reduce $path\n";
322 my $cache_path = "cache/$dir_url.reduce.$reduce.jpg";
323 if ( $reduce <= 1 && $path =~ m/\.jpe?g$/ ) {
325 } elsif ( ! -e $cache_path ) {
326 make_basedir $cache_path;
327 convert( '-scale', ( 100 / $reduce ) .'%', $path => $cache_path );
330 return $self->SUPER::serve_path($env, $cache_path, $fullpath);
334 return $self->SUPER::serve_path($env, $path, $fullpath);
335 } elsif ( -d $path ) {
337 if ($dir_url !~ m{/$}) {
338 return $self->return_dir_redirect($env);
341 my $dh = DirHandle->new($path);
343 while (defined(my $ent = $dh->read)) {
345 push @children, $ent;
348 for my $basename (sort { $a cmp $b } @children) {
349 push @page_files, $basename if $basename =~ m/\d+\D?\.(jpg|gif|pdf)$/;
350 my $file = "$path/$basename";
351 my $url = $dir_url . $basename;
353 my $is_dir = -d $file;
357 $url = join '/', map {uri_escape($_)} split m{/}, $url;
364 my $mime_type = $is_dir ? 'directory' : ( Plack::MIME->mime_type($file) || 'text/plain' );
365 push @files, [ $url, $basename, $stat[7], $mime_type, HTTP::Date::time2str($stat[9]) ];
369 die "Unsupported format: $path";
373 @page_files = sort_pages @page_files;
374 warn "# page_files = ",dump( @page_files );
377 my $dir = Plack::Util::encode_html( $env->{PATH_INFO} );
380 if ( $req->param('bookreader') ) {
383 my $pages_path = "cache/$dir_url/bookreader.json";
384 if ( -e $pages_path ) {
385 $pages = decode_json read_file $pages_path;
387 foreach my $page ( @page_files ) {
388 my $image = Graphics::Magick->new;
389 if ( $page =~ m/\.pdf$/ ) {
390 die "$path/$page: $!" unless -r "$path/$page";
392 my $info = `pdfinfo "$path/$page"`;
393 warn "# pdfinfo $path/$page\n$info\n";
394 my $pdf_pages = $1 if ( $info =~ m/Pages:\s*(\d+)/s );
395 die "can't find number of pages for $path/$page in:\n$pdf_pages\n" unless $pdf_pages;
398 $pdf_pages = $ENV{MAX_PAGES} if $pdf_pages > $ENV{MAX_PAGES}; # FIXME
400 foreach my $nr ( 1 .. $pdf_pages ) {
401 my $cache_path = "cache/$dir_url/$page";
402 my $page_url = convert_pdf_page( "$path/$page", $nr, $cache_path . '.' . $nr );
403 warn "## ping $page_url\n";
404 my ( $w, $h, $size, $format ) = $image->ping($page_url);
405 warn "## image size $w*$h $size $format $page_url\n";
406 my $url = decode('utf-8',"/$page_url");
407 push @$pages, [ $url, $w, $h ] if $w && $h;
411 die "$path/$page: $!" unless -r "$path/$page";
412 my ( $w, $h, $size, $format ) = $image->ping("$path/$page");
413 warn "# image size $w*$h $size $format $path/$page\n";
414 my $url = decode('utf-8',"$dir_url/$page");
415 push @$pages, [ $url, $w, $h ] if $w && $h;
418 make_basedir $pages_path;
419 write_file $pages_path, => encode_json( $pages );
420 warn "# created $pages_path ", -s $pages_path, " bytes\n";
422 warn "# pages = ",dump($pages);
423 $page = sprintf $reader_page, $dir, encode_json( $pages ), $dir, '..';
427 my $files = join "\n", map {
429 sprintf $dir_file, map Plack::Util::encode_html($_), @$f;
432 $page = sprintf $dir_page, $dir, $dir, $files,
433 @page_files ? '<form><input type=submit name=bookreader value="Read"></form>' . dump( [ @page_files ] ) : '';
437 return [ 200, ['Content-Type' => 'text/html; charset=utf-8'], [ $page ] ];
446 Plack::App::BookReader - Internet Archive Book Reader with directory index
451 use Plack::App::BookReader;
452 my $app = Plack::App::BookReader->new({ root => "/path/to/htdocs" })->to_app;
456 This is a static file server PSGI application with directory index a la Apache's mod_autoindex.
464 Document root directory. Defaults to the current directory.
471 Tatsuhiko Miyagawa (based on L<Plack::App::Directory>