1 package Plack::App::BookReader;
2 use parent qw(Plack::App::File);
11 use Data::Dump qw(dump);
18 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);
235 if ( my $reduce = $req->param('reduce') ) {
236 $reduce = int($reduce); # BookReader javascript somethimes returns float
237 warn "# reduce $reduce $path\n";
239 my $cache_path = "cache/$path.reduce.$reduce.jpg";
240 if ( $reduce <= 1 && $path =~ m/\.jpe?g$/ ) {
242 } elsif ( ! -e $cache_path ) {
243 my $image = Graphics::Magick->new( magick => 'jpg' );
245 my ( $w, $h ) = $image->Get('width','height');
247 width => $w / $reduce,
248 height => $h / $reduce
250 make_basedir $cache_path;
251 $image->Write( filename => $cache_path );
252 warn "# created $cache_path ", -s $cache_path, " bytes\n";
255 return $self->SUPER::serve_path($env, $cache_path, $fullpath);
259 return $self->SUPER::serve_path($env, $path, $fullpath);
262 my $dir_url = $env->{SCRIPT_NAME} . $env->{PATH_INFO};
264 if ($dir_url !~ m{/$}) {
265 return $self->return_dir_redirect($env);
268 my @files = ([ "../", "Parent Directory", '', '', '' ]);
270 my $dh = DirHandle->new($path);
272 while (defined(my $ent = $dh->read)) {
274 push @children, $ent;
279 for my $basename (sort { $a cmp $b } @children) {
280 push @page_files, $basename if $basename =~ m/\.(jpg|gif)$/;
281 my $file = "$path/$basename";
282 my $url = $dir_url . $basename;
284 my $is_dir = -d $file;
288 $url = join '/', map {uri_escape($_)} split m{/}, $url;
295 my $mime_type = $is_dir ? 'directory' : ( Plack::MIME->mime_type($file) || 'text/plain' );
296 push @files, [ $url, $basename, $stat[7], $mime_type, HTTP::Date::time2str($stat[9]) ];
299 warn "# page_files = ",dump( @page_files );
301 my $dir = Plack::Util::encode_html( $env->{PATH_INFO} );
304 if ( $req->param('bookreader') ) {
307 my $pages_path = "cache/$dir_url/bookreader.json";
308 if ( -e $pages_path ) {
309 $pages = decode_json read_file $pages_path;
314 my ( $w, $h ) = imgsize("$path/$_");
315 $w && $h ? [ "$dir_url/$_", $w, $h ] : undef
316 } sort { $a <=> $b } @page_files
318 make_basedir $pages_path;
319 write_file $pages_path => encode_json( $pages );
320 warn "# created $pages_path ", -s $pages_path, " bytes\n";
322 warn "# pages = ",dump($pages);
323 $page = sprintf $reader_page, $dir, encode_json( $pages ), $dir, $dir;
327 my $files = join "\n", map {
329 sprintf $dir_file, map Plack::Util::encode_html($_), @$f;
332 $page = sprintf $dir_page, $dir, $dir, $files,
333 @page_files ? '<form><input type=submit name=bookreader value="Read"></form>' : '';
337 return [ 200, ['Content-Type' => 'text/html; charset=utf-8'], [ $page ] ];
346 Plack::App::BookReader - Internet Archive Book Reader with directory index
351 use Plack::App::BookReader;
352 my $app = Plack::App::BookReader->new({ root => "/path/to/htdocs" })->to_app;
356 This is a static file server PSGI application with directory index a la Apache's mod_autoindex.
364 Document root directory. Defaults to the current directory.
371 Tatsuhiko Miyagawa (based on L<Plack::App::Directory>