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::Seconds 'ONE_YEAR';
23 $path =~ s{/[^/]+$}{} || die "no dir/file in $path";
24 File::Path::make_path $path;
27 # Stolen from rack/directory.rb
28 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>";
29 my $dir_page = <<PAGE;
32 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
33 <style type='text/css'>
34 table { width:100%%; }
35 .name { text-align:left; }
36 .size, .mtime { text-align:right; }
38 .mtime { width:15em; }
45 <th class='name'>Name</th>
46 <th class='size'>Size</th>
47 <th class='type'>Type</th>
48 <th class='mtime'>Last Modified</th>
57 my $reader_page = <<'PAGE';
58 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
63 <link rel="stylesheet" type="text/css" href="/BookReader/BookReader.css"/>
64 <script type="text/javascript" src="http://www.archive.org/includes/jquery-1.4.2.min.js"></script>
65 <script type="text/javascript" src="http://www.archive.org/bookreader/jquery-ui-1.8.5.custom.min.js"></script>
67 <script type="text/javascript" src="http://www.archive.org/bookreader/dragscrollable.js"></script>
68 <script type="text/javascript" src="http://www.archive.org/bookreader/jquery.colorbox-min.js"></script>
69 <script type="text/javascript" src="http://www.archive.org/bookreader/jquery.ui.ipad.js"></script>
70 <script type="text/javascript" src="http://www.archive.org/bookreader/jquery.bt.min.js"></script>
72 <script type="text/javascript" src="/BookReader/BookReader.js"></script>
74 <style type="text/css">
76 /* Hide print and embed functionality */
77 #BRtoolbar .embed, .print {
83 <script type="text/javascript">
84 $(document).ready( function() {
87 // This file shows the minimum you need to provide to BookReader to display a book
89 // Copyright(c)2008-2009 Internet Archive. Software license AGPL version 3.
91 // Create the BookReader object
92 var br = new BookReader();
96 // Return the width of a given page. Here we assume all images are 800 pixels wide
97 br.getPageWidth = function(index) {
98 if ( ! pages[index] ) return;
99 return parseInt( pages[index][1] );
102 // Return the height of a given page. Here we assume all images are 1200 pixels high
103 br.getPageHeight = function(index) {
104 if ( ! pages[index] ) return;
105 return parseInt( pages[index][2] );
108 // We load the images from archive.org -- you can modify this function to retrieve images
109 // using a different URL structure
110 br.getPageURI = function(index, reduce, rotate) {
111 if ( ! pages[index] ) return;
112 // reduce and rotate are ignored in this simple implementation, but we
113 // could e.g. look at reduce and load images from a different directory
114 // or pass the information to an image server
115 var url = pages[index][0] + '?reduce='+reduce;
116 console.debug('getPageURI', index, reduce, rotate, url);
120 // Return which side, left or right, that a given page should be displayed on
121 br.getPageSide = function(index) {
122 if (0 == (index & 0x1)) {
129 // This function returns the left and right indices for the user-visible
130 // spread that contains the given index. The return values may be
131 // null if there is no facing page or the index is invalid.
132 br.getSpreadIndices = function(pindex) {
133 var spreadIndices = [null, null];
134 if ('rl' == this.pageProgression) {
136 if (this.getPageSide(pindex) == 'R') {
137 spreadIndices[1] = pindex;
138 spreadIndices[0] = pindex + 1;
140 // Given index was LHS
141 spreadIndices[0] = pindex;
142 spreadIndices[1] = pindex - 1;
146 if (this.getPageSide(pindex) == 'L') {
147 spreadIndices[0] = pindex;
148 spreadIndices[1] = pindex + 1;
150 // Given index was RHS
151 spreadIndices[1] = pindex;
152 spreadIndices[0] = pindex - 1;
156 return spreadIndices;
159 // For a given "accessible page index" return the page number in the book.
161 // For example, index 5 might correspond to "Page 1" if there is front matter such
162 // as a title page and table of contents.
163 br.getPageNum = function(index) {
167 // Total number of leafs
168 br.numLeafs = pages.length;
170 // Book title and the URL used for the book title link
174 // Override the path used to find UI images
175 br.imagesBaseURL = '/BookReader/images/';
177 br.getEmbedCode = function(frameWidth, frameHeight, viewParams) {
178 return "Embed code not supported in bookreader demo.";
184 // read-aloud and search need backend compenents and are not supported in the demo
185 $('#BRtoolbar').find('.read').hide();
186 $('#textSrch').hide();
187 $('#btnSrch').hide();
193 <body style="background-color: ##939598;">
195 <div id="BookReader">
196 Internet Archive BookReader<br/>
200 The BookReader requires JavaScript to be enabled. Please check that your browser supports JavaScript and that it is enabled in the browser settings.
211 my($self, $file) = @_;
212 return -d $file || -f $file;
215 sub return_dir_redirect {
216 my ($self, $env) = @_;
217 my $uri = Plack::Request->new($env)->uri;
220 'Location' => $uri . '/',
221 'Content-Type' => 'text/plain',
222 'Content-Length' => 8,
229 my($self, $env, $path, $fullpath) = @_;
231 my $req = Plack::Request->new($env);
233 my $dir_url = $env->{SCRIPT_NAME} . $env->{PATH_INFO};
237 if ( my $reduce = $req->param('reduce') ) {
238 $reduce = int($reduce); # BookReader javascript somethimes returns float
239 warn "# reduce $reduce $path\n";
241 my $cache_path = "cache/$dir_url.reduce.$reduce.jpg";
242 if ( $reduce <= 1 && $path =~ m/\.jpe?g$/ ) {
244 } elsif ( ! -e $cache_path ) {
245 my $image = Graphics::Magick->new;
246 warn "## Read $path ", -s $path, " bytes\n";
248 my ( $w, $h ) = $image->Get('width','height');
250 width => $w / $reduce,
251 height => $h / $reduce
253 make_basedir $cache_path;
254 $image->Write( filename => $cache_path );
255 warn "# created $cache_path ", -s $cache_path, " bytes\n";
258 return $self->SUPER::serve_path($env, $cache_path, $fullpath);
262 return $self->SUPER::serve_path($env, $path, $fullpath);
265 if ($dir_url !~ m{/$}) {
266 return $self->return_dir_redirect($env);
271 my $dh = DirHandle->new($path);
273 while (defined(my $ent = $dh->read)) {
275 push @children, $ent;
280 for my $basename (sort { $a cmp $b } @children) {
281 push @page_files, $basename if $basename =~ m/\d+\.(jpg|gif|pdf)$/;
282 my $file = "$path/$basename";
283 my $url = $dir_url . $basename;
285 my $is_dir = -d $file;
289 $url = join '/', map {uri_escape($_)} split m{/}, $url;
296 my $mime_type = $is_dir ? 'directory' : ( Plack::MIME->mime_type($file) || 'text/plain' );
297 push @files, [ $url, $basename, $stat[7], $mime_type, HTTP::Date::time2str($stat[9]) ];
300 warn "# page_files = ",dump( @page_files );
302 my $dir = Plack::Util::encode_html( $env->{PATH_INFO} );
305 if ( $req->param('bookreader') ) {
308 my $pages_path = "cache/$dir_url/bookreader.json";
309 if ( 0 && -e $pages_path ) {
310 $pages = decode_json read_file $pages_path;
312 foreach my $page ( sort { $a <=> $b } @page_files ) {
313 my $image = Graphics::Magick->new;
314 if ( $page =~ m/\.pdf$/ ) {
315 my $cache_dir = "cache/$dir_url/$page/";
316 make_path $cache_dir;
317 warn "# pdfimages $path/$page -> $cache_dir";
318 system 'pdfimages', '-q', '-j', '-p', "$path/$page", $cache_dir;
320 # glob split on spaces!
321 opendir(my $dh, $cache_dir);
322 while (readdir($dh)) {
323 warn "## readdir = [$_]\n";
324 my $page = "$cache_dir/$_";
325 next unless -f $page; # skip . ..
327 if ( $page !~ m/\.jpg$/ ) {
328 warn "# convert to jpg";
329 system 'gm', 'convert', $page, $page . '.jpg';
334 warn "## ping $page\n";
335 die "$page: $!" unless -r $page;
336 my ( $w, $h, $size, $format ) = $image->ping($page);
337 warn "## image size $w*$h $size $format $page\n";
338 push @$pages, [ "/$page", $w, $h ] if $w && $h;
343 die "$path/$page: $!" unless -r "$path/$page";
344 my ( $w, $h, $size, $format ) = $image->ping("$path/$page");
345 warn "# image size $w*$h $size $format $path/$page\n";
346 push @$pages, [ "$dir_url/$page", $w, $h ] if $w && $h;
349 make_basedir $pages_path;
350 write_file $pages_path => encode_json( $pages );
351 warn "# created $pages_path ", -s $pages_path, " bytes\n";
353 warn "# pages = ",dump($pages);
354 $page = sprintf $reader_page, $dir, encode_json( $pages ), $dir, '..';
358 my $files = join "\n", map {
360 sprintf $dir_file, map Plack::Util::encode_html($_), @$f;
363 $page = sprintf $dir_page, $dir, $dir, $files,
364 @page_files ? '<form><input type=submit name=bookreader value="Read"></form>' . dump( [ @page_files ] ) : '';
368 return [ 200, ['Content-Type' => 'text/html; charset=utf-8'], [ $page ] ];
377 Plack::App::BookReader - Internet Archive Book Reader with directory index
382 use Plack::App::BookReader;
383 my $app = Plack::App::BookReader->new({ root => "/path/to/htdocs" })->to_app;
387 This is a static file server PSGI application with directory index a la Apache's mod_autoindex.
395 Document root directory. Defaults to the current directory.
402 Tatsuhiko Miyagawa (based on L<Plack::App::Directory>