Refactor BookReaderImages and BookReaderMeta. New BookReaderPreview to provide acces...
authorMichael Ang <mang@archive.org>
Wed, 28 Apr 2010 00:35:33 +0000 (00:35 +0000)
committerMichael Ang <mang@archive.org>
Wed, 28 Apr 2010 00:35:33 +0000 (00:35 +0000)
BookReaderIA/datanode/BookReaderImages.inc.php
BookReaderIA/datanode/BookReaderImages.php [new file with mode: 0644]
BookReaderIA/datanode/BookReaderJSON.php [new file with mode: 0644]
BookReaderIA/datanode/BookReaderMeta.inc.php
BookReaderIA/datanode/BookReaderPreview.php [new file with mode: 0644]

index b324481..5af62e1 100644 (file)
@@ -77,7 +77,10 @@ class BookReaderImages
         } else {
             $callback = null;
         }
-        
+
+        if ( !file_exists($zipPath) ) {
+            $this->BRfatal('Image stack does not exist');
+        }
         // Make sure the image stack is readable - return 403 if not
         $this->checkPrivs($zipPath);
         
@@ -218,7 +221,7 @@ class BookReaderImages
         
         $filenameForClient = $this->filenameForClient($file, $ext);
         
-        $headers = array('Content-type: '. $MIMES[$ext],
+        $headers = array('Content-type: '. $MIMES[$ext], // XXX is nginx swallowing this?
                          'Cache-Control: max-age=15552000',
                          'Content-disposition: inline; filename=' . $filenameForClient);
                           
diff --git a/BookReaderIA/datanode/BookReaderImages.php b/BookReaderIA/datanode/BookReaderImages.php
new file mode 100644 (file)
index 0000000..1af5864
--- /dev/null
@@ -0,0 +1,35 @@
+<?php
+
+/*
+Copyright(c) 2008-2010 Internet Archive. Software license AGPL version 3.
+
+This file is part of BookReader.  The full source code can be found at GitHub:
+http://github.com/openlibrary/bookreader
+
+The canonical short name of an image type is the same as in the MIME type.
+For example both .jpeg and .jpg are considered to have type "jpeg" since
+the MIME type is "image/jpeg".
+
+    BookReader is free software: you can redistribute it and/or modify
+    it under the terms of the GNU Affero General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    BookReader is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU Affero General Public License for more details.
+
+    You should have received a copy of the GNU Affero General Public License
+    along with BookReader.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+// Include BookReaderImages class definition
+require_once('BookReaderImages.inc.php');
+
+// Serve request
+$bri = new BookReaderImages();
+$bri->serveRequest($_REQUEST);
+
+?>
+
diff --git a/BookReaderIA/datanode/BookReaderJSON.php b/BookReaderIA/datanode/BookReaderJSON.php
new file mode 100644 (file)
index 0000000..076a3d1
--- /dev/null
@@ -0,0 +1,33 @@
+<?
+/*
+
+Builds metadata about a book on the Internet Archive in json(p) format so that the book
+can be accessed by other software including the Internet Archive BookReader.
+
+Michael Ang <http://github.com/mangtronix>
+
+Copyright (c) 2008-2010 Internet Archive. Software license AGPL version 3.
+
+This file is part of BookReader.
+
+    BookReader is free software: you can redistribute it and/or modify
+    it under the terms of the GNU Affero General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    BookReader is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU Affero General Public License for more details.
+
+    You should have received a copy of the GNU Affero General Public License
+    along with BookReader.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+require_once('BookReaderMeta.inc.php');
+
+$brm = new BookReaderMeta();
+$brm->processRequest($_REQUEST);
+
+
+?>
index 7b5910b..e1b4ff3 100644 (file)
@@ -167,15 +167,17 @@ class BookReaderMeta {
         // Internet Archive specific
         $response['itemId'] = $id; // XXX renamed
         $response['bookId'] = $bookId;  // XXX renamed
+        $response['itemPath'] = $itemPath;
         $response['zip'] = $imageStackFile;
         $response['server'] = $server;
         $response['imageFormat'] = $imageFormat;
         $response['archiveFormat'] = $archiveFormat;
         $response['leafNums'] = $leafNums;
+        $response['previewImage'] = $this->previewURL('preview', $response);
         
         // URL to title image
         if ('' != $titleLeaf) {
-            $response['titleImage'] = $this->imageURL($titleLeaf, $response);
+            $response['titleImage'] = $this->previewURL('title', $response);
         }
         
         if (count($coverLeafs) > 0) {
@@ -183,26 +185,14 @@ class BookReaderMeta {
             $coverImages = array();
             foreach ($coverLeafs as $key => $leafNum) {
                 array_push($coverIndices, $this->indexForLeaf($leafNum, $leafNums));
+                // $$$ TODO use preview API once it supports multiple covers
                 array_push($coverImages, $this->imageUrl($leafNum, $response));
             }
             
             $response['coverIndices'] = $coverIndices;
             $response['coverImages'] = $coverImages;
         }
-        
-        // Determine "preview" image, which may be the cover, title, or first page
-        if (array_key_exists('titleImage', $response)) {
-            // Use title image if was assert
-            $previewImage = $response['titleImage'];
-        } else if (array_key_exists('coverImages', $response)) {
-            // Try for the cover page
-            $previewImage = $response['coverImages'][0];
-        } else {
-            // Neither title nor cover asserted, use first page
-            $previewImage = $this->imageURL(0, $response);
-        }
-        $response['previewImage'] = $previewImage;
-        
+                
         return $response;
     }
     
@@ -315,6 +305,19 @@ class BookReaderMeta {
         return $url;
     }
     
+    // $$$ move inside BookReaderPreview
+    function previewURL($page, $metadata) {
+        $query = array(
+            'id' => $metadata['itemId'],
+            'bookId' => $metadata['bookId'],
+            'itemPath' => $metadata['itemPath'],
+            'server' => $metadata['server'],
+            'page' => $page,
+        );
+        
+        return 'http://' . $metadata['server'] . '/BookReader/BookReaderPreview.php?' . http_build_query($query, '', '&');
+    }
+    
     function imageFilePath($leafNum, $bookId, $format) {
         return sprintf("%s_%s/%s_%04d.%s", $bookId, $format, $bookId, intval($leafNum), $format);
     }
diff --git a/BookReaderIA/datanode/BookReaderPreview.php b/BookReaderIA/datanode/BookReaderPreview.php
new file mode 100644 (file)
index 0000000..5fb4a7f
--- /dev/null
@@ -0,0 +1,95 @@
+<?
+/*
+
+Provides access to preview images of book.
+
+Michael Ang <http://github.com/mangtronix>
+
+Copyright (c) 2010 Internet Archive. Software license AGPL version 3.
+
+This file is part of BookReader.
+
+    BookReader is free software: you can redistribute it and/or modify
+    it under the terms of the GNU Affero General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    BookReader is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU Affero General Public License for more details.
+
+    You should have received a copy of the GNU Affero General Public License
+    along with BookReader.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+require_once('BookReaderMeta.inc.php');
+require_once('BookReaderImages.inc.php');
+
+function BRfatal($message) {
+    header("HTTP/1.0 404 Not Found");
+    print $message;
+    exit();
+}
+
+$brm = new BookReaderMeta();
+$metadata = $brm->buildMetadata($_REQUEST['id'], $_REQUEST['itemPath'], $_REQUEST['bookId'], $_REQUEST['server']);
+
+$knownPages = array('title','cover','preview');
+$page = $_REQUEST['page'];
+if (! in_array($page, $knownPages) ) {
+    BRfatal("Bad or no page specified");
+}
+
+// Index of image to return
+$imageIndex = null;
+
+switch ($page) {
+    case 'title':
+        if (! array_key_exists('titleIndex', $metadata)) {
+            BRfatal("No title page asserted in book");
+        }
+        $imageIndex = $metadata['titleIndex'];
+        break;
+        
+    case 'cover':
+        if (! array_key_exists('coverIndices', $metadata)) {
+            BRfatal("No cover asserted in book");
+        }
+        $imageIndex = $metadata['coverIndices'][0]; // $$$ TODO add support for other covers
+        break;
+        
+    case 'preview':
+        // Preference is:
+        //   Title page
+        //   Cover page
+        //   Page 0
+        if (array_key_exists('titleIndex', $metadata)) {
+            $imageIndex = $metadata['titleIndex'];
+        } else if (array_key_exists('coverIndices', $metadata)) {
+            $imageIndex = $metadata['coverIndices'][0];
+        } else {
+            $imageIndex = 0;
+        }
+        break;
+        
+    default:
+        // Shouldn't be possible
+        BRfatal("Couldn't find page");
+        break;
+}
+
+$leaf = $brm->leafForIndex($imageIndex, $metadata['leafNums']);
+
+$requestEnv = array(
+    'zip' => $metadata['zip'],
+    'file' => $brm->imageFilePath($leaf, $metadata['bookId'], $metadata['imageFormat']),
+    'ext' => 'jpg',
+    'reduce' => 2, // XXX
+);
+
+// Return image data - will check privs
+$bri = new BookReaderImages();
+$bri->serveRequest($requestEnv);
+
+?>
\ No newline at end of file