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);
22 $path =~ s{/[^/]+$}{} || die "no dir/file in $path";
23 File::Path::make_path $path;
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;
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; }
37 .mtime { width:15em; }
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>
56 my $reader_page = <<'PAGE';
57 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
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>
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>
71 <script type="text/javascript" src="/BookReader/BookReader.js"></script>
73 <style type="text/css">
75 /* Hide print and embed functionality */
76 #BRtoolbar .embed, .print {
82 <script type="text/javascript">
83 $(document).ready( function() {
86 // This file shows the minimum you need to provide to BookReader to display a book
88 // Copyright(c)2008-2009 Internet Archive. Software license AGPL version 3.
90 // Create the BookReader object
91 var br = new BookReader();
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] );
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] );
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);
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)) {
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) {
135 if (this.getPageSide(pindex) == 'R') {
136 spreadIndices[1] = pindex;
137 spreadIndices[0] = pindex + 1;
139 // Given index was LHS
140 spreadIndices[0] = pindex;
141 spreadIndices[1] = pindex - 1;
145 if (this.getPageSide(pindex) == 'L') {
146 spreadIndices[0] = pindex;
147 spreadIndices[1] = pindex + 1;
149 // Given index was RHS
150 spreadIndices[1] = pindex;
151 spreadIndices[0] = pindex - 1;
155 return spreadIndices;
158 // For a given "accessible page index" return the page number in the book.
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) {
166 // Total number of leafs
167 br.numLeafs = pages.length;
169 // Book title and the URL used for the book title link
173 // Override the path used to find UI images
174 br.imagesBaseURL = '/BookReader/images/';
176 br.getEmbedCode = function(frameWidth, frameHeight, viewParams) {
177 return "Embed code not supported in bookreader demo.";
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();
192 <body style="background-color: ##939598;">
194 <div id="BookReader">
195 Internet Archive BookReader<br/>
199 The BookReader requires JavaScript to be enabled. Please check that your browser supports JavaScript and that it is enabled in the browser settings.
210 my($self, $file) = @_;
211 return -d $file || -f $file;
214 sub return_dir_redirect {
215 my ($self, $env) = @_;
216 my $uri = Plack::Request->new($env)->uri;
219 'Location' => $uri . '/',
220 'Content-Type' => 'text/plain',
221 'Content-Length' => 8,
228 warn "# convert ",dump(@_);
230 system 'gm', 'convert', @_;
232 warn sprintf("## created %d bytes in %.2f s %s\n", -s $_[-1], $t, $_[-1]);
235 sub longest_common_prefix {
238 chop $prefix while (! /^\Q$prefix\E/i);
240 warn "# longest_common_prefix [$prefix]\n";
246 my($self, $env, $path, $fullpath) = @_;
248 my $req = Plack::Request->new($env);
250 my $dir_url = $env->{SCRIPT_NAME} . $env->{PATH_INFO};
254 if ( my $reduce = $req->param('reduce') ) {
255 $reduce = int($reduce); # BookReader javascript somethimes returns float
256 warn "# reduce $reduce $path\n";
258 my $cache_path = "cache/$dir_url.reduce.$reduce.jpg";
259 if ( $reduce <= 1 && $path =~ m/\.jpe?g$/ ) {
261 } elsif ( ! -e $cache_path ) {
262 make_basedir $cache_path;
263 convert( '-scale', ( 100 / $reduce ) .'%', $path => $cache_path );
266 return $self->SUPER::serve_path($env, $cache_path, $fullpath);
270 return $self->SUPER::serve_path($env, $path, $fullpath);
273 if ($dir_url !~ m{/$}) {
274 return $self->return_dir_redirect($env);
279 my $dh = DirHandle->new($path);
281 while (defined(my $ent = $dh->read)) {
283 push @children, $ent;
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;
293 my $is_dir = -d $file;
297 $url = join '/', map {uri_escape($_)} split m{/}, $url;
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]) ];
309 my $prefix = longest_common_prefix @page_files;
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";
317 warn "# page_files = ",dump( @page_files );
320 my $dir = Plack::Util::encode_html( $env->{PATH_INFO} );
323 if ( $req->param('bookreader') ) {
326 my $pages_path = "cache/$dir_url/bookreader.json";
327 if ( -e $pages_path ) {
328 $pages = decode_json read_file $pages_path;
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;
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 . ..
345 if ( $page !~ m/\.jpg$/ ) {
346 convert( $page => $page . '.jpg' );
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;
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;
366 make_basedir $pages_path;
367 write_file $pages_path => encode_json( $pages );
368 warn "# created $pages_path ", -s $pages_path, " bytes\n";
370 warn "# pages = ",dump($pages);
371 $page = sprintf $reader_page, $dir, encode_json( $pages ), $dir, '..';
375 my $files = join "\n", map {
377 sprintf $dir_file, map Plack::Util::encode_html($_), @$f;
380 $page = sprintf $dir_page, $dir, $dir, $files,
381 @page_files ? '<form><input type=submit name=bookreader value="Read"></form>' . dump( [ @page_files ] ) : '';
385 return [ 200, ['Content-Type' => 'text/html; charset=utf-8'], [ $page ] ];
394 Plack::App::BookReader - Internet Archive Book Reader with directory index
399 use Plack::App::BookReader;
400 my $app = Plack::App::BookReader->new({ root => "/path/to/htdocs" })->to_app;
404 This is a static file server PSGI application with directory index a la Apache's mod_autoindex.
412 Document root directory. Defaults to the current directory.
419 Tatsuhiko Miyagawa (based on L<Plack::App::Directory>