07045c57bde7c666060c148051bc1fd44393c95a
[bookreader.git] / plack / lib / Plack / App / BookReader.pm
1 package Plack::App::BookReader;
2 use parent qw(Plack::App::File);
3 use strict;
4 use warnings;
5 use Plack::Util;
6 use HTTP::Date;
7 use Plack::MIME;
8 use DirHandle;
9 use URI::Escape;
10 use Plack::Request;
11 use Data::Dump qw(dump);
12 use File::Path;
13 use Graphics::Magick;
14 use File::Slurp;
15 use Image::Size;
16 use JSON;
17 use Time::Piece ();
18 use Time::Seconds 'ONE_YEAR';
19
20 sub make_basedir {
21         my $path = shift;
22         return if -e $path;
23         $path =~ s{/[^/]+$}{} || die "no dir/file in $path";
24         File::Path::make_path $path;
25 }
26
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;
30 <html><head>
31   <title>%s</title>
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; }
37 .type { width:11em; }
38 .mtime { width:15em; }
39   </style>
40 </head><body>
41 <h1>%s</h1>
42 <hr />
43 <table>
44   <tr>
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>
49   </tr>
50 %s
51 </table>
52 <hr />
53 <code>%s</code>
54 </body></html>
55 PAGE
56
57 my $reader_page = <<'PAGE';
58 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
59 <html>
60 <head>
61     <title>%s</title>
62     
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>
66
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>
71
72     <script type="text/javascript" src="/BookReader/BookReader.js"></script>
73
74 <style type="text/css">
75
76 /* Hide print and embed functionality */
77 #BRtoolbar .embed, .print {
78     display: none;
79 }
80
81 </style>
82
83 <script type="text/javascript">
84 $(document).ready( function() {
85
86 // 
87 // This file shows the minimum you need to provide to BookReader to display a book
88 //
89 // Copyright(c)2008-2009 Internet Archive. Software license AGPL version 3.
90
91 // Create the BookReader object
92 var br = new BookReader();
93
94 var pages = %s;
95
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] );
100 }
101
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] );
106 }
107
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);
117     return url;
118 }
119
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)) {
123         return 'R';
124     } else {
125         return 'L';
126     }
127 }
128
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) {
135         // Right to Left
136         if (this.getPageSide(pindex) == 'R') {
137             spreadIndices[1] = pindex;
138             spreadIndices[0] = pindex + 1;
139         } else {
140             // Given index was LHS
141             spreadIndices[0] = pindex;
142             spreadIndices[1] = pindex - 1;
143         }
144     } else {
145         // Left to right
146         if (this.getPageSide(pindex) == 'L') {
147             spreadIndices[0] = pindex;
148             spreadIndices[1] = pindex + 1;
149         } else {
150             // Given index was RHS
151             spreadIndices[1] = pindex;
152             spreadIndices[0] = pindex - 1;
153         }
154     }
155     
156     return spreadIndices;
157 }
158
159 // For a given "accessible page index" return the page number in the book.
160 //
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) {
164     return index+1;
165 }
166
167 // Total number of leafs
168 br.numLeafs = pages.length;
169
170 // Book title and the URL used for the book title link
171 br.bookTitle= '%s';
172 br.bookUrl  = '%s';
173
174 // Override the path used to find UI images
175 br.imagesBaseURL = '/BookReader/images/';
176
177 br.getEmbedCode = function(frameWidth, frameHeight, viewParams) {
178     return "Embed code not supported in bookreader demo.";
179 }
180
181 // Let's go!
182 br.init();
183
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();
188
189 } );
190 </script>
191
192 </head>
193 <body style="background-color: ##939598;">
194
195 <div id="BookReader">
196     Internet Archive BookReader<br/>
197     
198     <noscript>
199     <p>
200         The BookReader requires JavaScript to be enabled. Please check that your browser supports JavaScript and that it is enabled in the browser settings.
201     </p>
202     </noscript>
203 </div>
204
205
206 </body>
207 </html>
208 PAGE
209
210 sub should_handle {
211     my($self, $file) = @_;
212     return -d $file || -f $file;
213 }
214
215 sub return_dir_redirect {
216     my ($self, $env) = @_;
217     my $uri = Plack::Request->new($env)->uri;
218     return [ 301,
219         [
220             'Location' => $uri . '/',
221             'Content-Type' => 'text/plain',
222             'Content-Length' => 8,
223         ],
224         [ 'Redirect' ],
225     ];
226 }
227
228 sub serve_path {
229     my($self, $env, $path, $fullpath) = @_;
230
231         my $req = Plack::Request->new($env);
232
233     if (-f $path) {
234
235                 if ( my $reduce = $req->param('reduce') ) {
236                         $reduce = int($reduce); # BookReader javascript somethimes returns float
237                         warn "# reduce $reduce $path\n";
238
239                         my $cache_path = "cache/$path.reduce.$reduce.jpg";
240                         if ( $reduce <= 1 && $path =~ m/\.jpe?g$/ ) {
241                                 $cache_path = $path;
242                         } elsif ( ! -e $cache_path ) {
243                                 my $image = Graphics::Magick->new( magick => 'jpg' );
244                                 $image->Read($path);
245                                 my ( $w, $h ) = $image->Get('width','height');
246                                 $image->Resize(
247                                         width  => $w / $reduce,
248                                         height => $h / $reduce
249                                 );
250                                 make_basedir $cache_path;
251                                 $image->Write( filename => $cache_path );
252                                 warn "# created $cache_path ", -s $cache_path, " bytes\n";
253                         }
254
255                 return $self->SUPER::serve_path($env, $cache_path, $fullpath);
256
257                 }
258
259         return $self->SUPER::serve_path($env, $path, $fullpath);
260     }
261
262     my $dir_url = $env->{SCRIPT_NAME} . $env->{PATH_INFO};
263
264     if ($dir_url !~ m{/$}) {
265         return $self->return_dir_redirect($env);
266     }
267
268     my @files = ([ "../", "Parent Directory", '', '', '' ]);
269
270     my $dh = DirHandle->new($path);
271     my @children;
272     while (defined(my $ent = $dh->read)) {
273         next if $ent eq '.';
274         push @children, $ent;
275     }
276
277         my @page_files;
278
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;
283
284         my $is_dir = -d $file;
285         my @stat = stat _;
286
287
288         $url = join '/', map {uri_escape($_)} split m{/}, $url;
289
290         if ($is_dir) {
291             $basename .= "/";
292             $url      .= "/";
293         }
294
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]) ];
297     }
298
299         warn "# page_files = ",dump( @page_files );
300
301     my $dir  = Plack::Util::encode_html( $env->{PATH_INFO} );
302         my $page = 'empty';
303
304         if ( $req->param('bookreader') ) {
305
306                 my $pages;
307                 my $pages_path = "cache/$dir_url/bookreader.json";
308                 if ( -e $pages_path ) {
309                         $pages = decode_json read_file $pages_path;
310                 } else {
311                         $pages = [
312                                 grep { defined $_ }
313                                 map {
314                                         my ( $w, $h ) = imgsize("$path/$_"); 
315                                         $w && $h ? [ "$dir_url/$_", $w, $h ] : undef
316                                 } sort { $a <=> $b } @page_files
317                         ];
318                         make_basedir $pages_path;
319                         write_file $pages_path => encode_json( $pages );
320                         warn "# created $pages_path ", -s $pages_path, " bytes\n";
321                 }
322                 warn "# pages = ",dump($pages);
323                 $page = sprintf $reader_page, $dir, encode_json( $pages ), $dir, $dir;
324
325         } else {
326
327                 my $files = join "\n", map {
328                         my $f = $_;
329                         sprintf $dir_file, map Plack::Util::encode_html($_), @$f;
330                 } @files;
331
332                 $page = sprintf $dir_page, $dir, $dir, $files, 
333                         @page_files ? '<form><input type=submit name=bookreader value="Read"></form>' : '';
334
335         }
336
337     return [ 200, ['Content-Type' => 'text/html; charset=utf-8'], [ $page ] ];
338 }
339
340 1;
341
342 __END__
343
344 =head1 NAME
345
346 Plack::App::BookReader - Internet Archive Book Reader with directory index
347
348 =head1 SYNOPSIS
349
350   # app.psgi
351   use Plack::App::BookReader;
352   my $app = Plack::App::BookReader->new({ root => "/path/to/htdocs" })->to_app;
353
354 =head1 DESCRIPTION
355
356 This is a static file server PSGI application with directory index a la Apache's mod_autoindex.
357
358 =head1 CONFIGURATION
359
360 =over 4
361
362 =item root
363
364 Document root directory. Defaults to the current directory.
365
366 =back
367
368 =head1 AUTHOR
369
370 Dobrica Pavlinusic
371 Tatsuhiko Miyagawa (based on L<Plack::App::Directory>
372
373 =head1 SEE ALSO
374
375 L<Plack::App::File>
376
377 =cut
378