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]);
236 my($self, $env, $path, $fullpath) = @_;
238 my $req = Plack::Request->new($env);
240 my $dir_url = $env->{SCRIPT_NAME} . $env->{PATH_INFO};
244 if ( my $reduce = $req->param('reduce') ) {
245 $reduce = int($reduce); # BookReader javascript somethimes returns float
246 warn "# reduce $reduce $path\n";
248 my $cache_path = "cache/$dir_url.reduce.$reduce.jpg";
249 if ( $reduce <= 1 && $path =~ m/\.jpe?g$/ ) {
251 } elsif ( ! -e $cache_path ) {
252 make_basedir $cache_path;
253 convert( '-scale', ( 100 / $reduce ) .'%', $path => $cache_path );
256 return $self->SUPER::serve_path($env, $cache_path, $fullpath);
260 return $self->SUPER::serve_path($env, $path, $fullpath);
263 if ($dir_url !~ m{/$}) {
264 return $self->return_dir_redirect($env);
269 my $dh = DirHandle->new($path);
271 while (defined(my $ent = $dh->read)) {
273 push @children, $ent;
278 for my $basename (sort { $a cmp $b } @children) {
279 push @page_files, $basename if $basename =~ m/\d+\.(jpg|gif|pdf)$/;
280 my $file = "$path/$basename";
281 my $url = $dir_url . $basename;
283 my $is_dir = -d $file;
287 $url = join '/', map {uri_escape($_)} split m{/}, $url;
294 my $mime_type = $is_dir ? 'directory' : ( Plack::MIME->mime_type($file) || 'text/plain' );
295 push @files, [ $url, $basename, $stat[7], $mime_type, HTTP::Date::time2str($stat[9]) ];
298 warn "# page_files = ",dump( @page_files );
300 my $dir = Plack::Util::encode_html( $env->{PATH_INFO} );
303 if ( $req->param('bookreader') ) {
306 my $pages_path = "cache/$dir_url/bookreader.json";
307 if ( -e $pages_path ) {
308 $pages = decode_json read_file $pages_path;
310 foreach my $page ( sort { $a <=> $b } @page_files ) {
311 my $image = Graphics::Magick->new;
312 if ( $page =~ m/\.pdf$/ ) {
313 my $cache_dir = "cache/$dir_url/$page/";
314 make_path $cache_dir;
315 warn "# pdfimages $path/$page -> $cache_dir";
316 system 'pdfimages', '-q', '-j', '-p', "$path/$page", $cache_dir;
318 # glob split on spaces!
319 opendir(my $dh, $cache_dir);
320 while (readdir($dh)) {
321 warn "## readdir = [$_]\n";
322 my $page = "$cache_dir/$_";
323 next unless -f $page; # skip . ..
325 if ( $page !~ m/\.jpg$/ ) {
326 convert( $page => $page . '.jpg' );
331 warn "## ping $page\n";
332 die "$page: $!" unless -r $page;
333 my ( $w, $h, $size, $format ) = $image->ping($page);
334 warn "## image size $w*$h $size $format $page\n";
335 push @$pages, [ "/$page", $w, $h ] if $w && $h;
340 die "$path/$page: $!" unless -r "$path/$page";
341 my ( $w, $h, $size, $format ) = $image->ping("$path/$page");
342 warn "# image size $w*$h $size $format $path/$page\n";
343 push @$pages, [ "$dir_url/$page", $w, $h ] if $w && $h;
346 make_basedir $pages_path;
347 write_file $pages_path => encode_json( $pages );
348 warn "# created $pages_path ", -s $pages_path, " bytes\n";
350 warn "# pages = ",dump($pages);
351 $page = sprintf $reader_page, $dir, encode_json( $pages ), $dir, '..';
355 my $files = join "\n", map {
357 sprintf $dir_file, map Plack::Util::encode_html($_), @$f;
360 $page = sprintf $dir_page, $dir, $dir, $files,
361 @page_files ? '<form><input type=submit name=bookreader value="Read"></form>' . dump( [ @page_files ] ) : '';
365 return [ 200, ['Content-Type' => 'text/html; charset=utf-8'], [ $page ] ];
374 Plack::App::BookReader - Internet Archive Book Reader with directory index
379 use Plack::App::BookReader;
380 my $app = Plack::App::BookReader->new({ root => "/path/to/htdocs" })->to_app;
384 This is a static file server PSGI application with directory index a la Apache's mod_autoindex.
392 Document root directory. Defaults to the current directory.
399 Tatsuhiko Miyagawa (based on L<Plack::App::Directory>