Refactor BookReaderImages and BookReaderMeta. New BookReaderPreview to provide acces...
[bookreader.git] / BookReaderIA / datanode / BookReaderPreview.php
1 <?
2 /*
3
4 Provides access to preview images of book.
5
6 Michael Ang <http://github.com/mangtronix>
7
8 Copyright (c) 2010 Internet Archive. Software license AGPL version 3.
9
10 This file is part of BookReader.
11
12     BookReader is free software: you can redistribute it and/or modify
13     it under the terms of the GNU Affero General Public License as published by
14     the Free Software Foundation, either version 3 of the License, or
15     (at your option) any later version.
16
17     BookReader is distributed in the hope that it will be useful,
18     but WITHOUT ANY WARRANTY; without even the implied warranty of
19     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20     GNU Affero General Public License for more details.
21
22     You should have received a copy of the GNU Affero General Public License
23     along with BookReader.  If not, see <http://www.gnu.org/licenses/>.
24 */
25
26 require_once('BookReaderMeta.inc.php');
27 require_once('BookReaderImages.inc.php');
28
29 function BRfatal($message) {
30     header("HTTP/1.0 404 Not Found");
31     print $message;
32     exit();
33 }
34
35 $brm = new BookReaderMeta();
36 $metadata = $brm->buildMetadata($_REQUEST['id'], $_REQUEST['itemPath'], $_REQUEST['bookId'], $_REQUEST['server']);
37
38 $knownPages = array('title','cover','preview');
39 $page = $_REQUEST['page'];
40 if (! in_array($page, $knownPages) ) {
41     BRfatal("Bad or no page specified");
42 }
43
44 // Index of image to return
45 $imageIndex = null;
46
47 switch ($page) {
48     case 'title':
49         if (! array_key_exists('titleIndex', $metadata)) {
50             BRfatal("No title page asserted in book");
51         }
52         $imageIndex = $metadata['titleIndex'];
53         break;
54         
55     case 'cover':
56         if (! array_key_exists('coverIndices', $metadata)) {
57             BRfatal("No cover asserted in book");
58         }
59         $imageIndex = $metadata['coverIndices'][0]; // $$$ TODO add support for other covers
60         break;
61         
62     case 'preview':
63         // Preference is:
64         //   Title page
65         //   Cover page
66         //   Page 0
67         if (array_key_exists('titleIndex', $metadata)) {
68             $imageIndex = $metadata['titleIndex'];
69         } else if (array_key_exists('coverIndices', $metadata)) {
70             $imageIndex = $metadata['coverIndices'][0];
71         } else {
72             $imageIndex = 0;
73         }
74         break;
75         
76     default:
77         // Shouldn't be possible
78         BRfatal("Couldn't find page");
79         break;
80 }
81
82 $leaf = $brm->leafForIndex($imageIndex, $metadata['leafNums']);
83
84 $requestEnv = array(
85     'zip' => $metadata['zip'],
86     'file' => $brm->imageFilePath($leaf, $metadata['bookId'], $metadata['imageFormat']),
87     'ext' => 'jpg',
88     'reduce' => 2, // XXX
89 );
90
91 // Return image data - will check privs
92 $bri = new BookReaderImages();
93 $bri->serveRequest($requestEnv);
94
95 ?>