Image permalinks, e.g. /download/itemid/page/cover.jpg
authorMichael Ang <mang@archive.org>
Fri, 23 Jul 2010 22:05:19 +0000 (22:05 +0000)
committerMichael Ang <mang@archive.org>
Fri, 23 Jul 2010 22:05:19 +0000 (22:05 +0000)
BookReaderIA/datanode/BookReaderImages.inc.php
BookReaderIA/datanode/BookReaderImages.php
BookReaderIA/inc/BookReader.inc

index b553f8f..d0e2c79 100644 (file)
@@ -24,6 +24,8 @@ the MIME type is "image/jpeg".
     along with BookReader.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+require_once("BookReaderMeta.inc.php");
+
 class BookReaderImages
 {
     public $MIMES = array('gif' => 'image/gif',
@@ -79,15 +81,17 @@ class BookReaderImages
         // Index of image to return
         $imageIndex = null;
 
-        // XXX deal with subPrefix
-        $pageInfo = $this->parsePageRequest($page);
-
-        // Parse requested page for page type, size and format options
-        if (preg_match('#^([^_]+)#', $page, $matches) === 0) {
-            // Unrecognized page specifier
-            $this->BRfatal('Unrecognized page specifier');
+        // deal with subPrefix
+        if ($_REQUEST['subPrefix']) {
+            $parts = split('/', $_REQUEST['subPrefix']);
+            $bookId = $parts[count($parts) - 1 ];
+        } else {
+            $bookId = $_REQUEST['id'];
         }
-        $basePage = $matches[1];
+        
+        $pageInfo = $this->parsePageRequest($page, $bookId);
+
+        $basePage = $pageInfo['type'];
         
         switch ($basePage) {
             case 'title':
@@ -130,9 +134,26 @@ class BookReaderImages
                 $imageIndex = 0;
                 break;
                 
+            case 'n':
+                // Accessible index page
+                $imageIndex = intval($pageInfo['value']);
+                break;
+                
+            case 'page':
+                // Named page
+                $index = array_search($pageInfo['value'], $metadata['pageNums']);
+                if ($index === FALSE) {
+                    // Not found
+                    $this->BRfatal("Page not found");
+                    break;
+                }
+                
+                $imageIndex = $index;
+                break;
+                
             default:
                 // Shouldn't be possible
-                $this->BRfatal("Couldn't find page");
+                $this->BRfatal("Unrecognized page type requested");
                 break;
                 
         }
index f3b8da7..fd06508 100644 (file)
@@ -30,7 +30,13 @@ require_once('BookReaderImages.inc.php');
 // Serve request
 $bri = new BookReaderImages();
 try {
-    $bri->serveRequest($_REQUEST);
+    if ($_REQUEST['page']) {
+        // Need to lookup metadata
+        $bri->serveLookupRequest($_REQUEST);
+    } else {
+        // Request should be fully qualified - no lookup needed
+        $bri->serveRequest($_REQUEST);
+    }
 } catch (Exception $e) {
     header("HTTP/1.0 404 Not Found");
     header("Content-type: text/plain");
index 8d8934f..0007586 100644 (file)
@@ -325,7 +325,7 @@ class BookReader
     
     $parts = array();
     
-    // Pull off query
+    // Pull off query, e.g. ?foo=bar
     if (preg_match('#(.*?)(\?.*)#', $path, $matches) === 1) {
         $parts['query'] = $matches[2];
         $path = $matches[1];
@@ -342,9 +342,9 @@ class BookReader
     // Look for operators
     // The sub-prefix can be arbitrary, so we match up until the first operator
     $operators = '(' . join('|', self::$downloadOperators) . ')';
-    $pattern = '#/(?P<subPrefix>.*?)/(?P<operator>' . $operators . ')/(?P<operand>.*)#';
+    $pattern = '#(?P<subPrefix>.*?)/(?P<operator>' . $operators . ')/(?P<operand>.*)#';
     if (preg_match($pattern, $path, $matches) === 1) {
-        $parts['subPrefix'] = $matches['subPrefix'];
+        $parts['subPrefix'] = substr($matches['subPrefix'], 1); // remove leading '/'
         $parts['operator'] = $matches['operator'];
         $parts['operand'] = $matches['operand'];
     } else {