added jstore path
[bookreader.git] / BookReaderIA / datanode / BookReaderImages.inc.php
index 2c6f839..ad56f20 100644 (file)
@@ -98,7 +98,7 @@ class BookReaderImages
         $imageIndex = null;
 
         // deal with subPrefix
-        if ($_REQUEST['subPrefix']) {
+        if (array_key_exists($_REQUEST, 'subPrefix') && $_REQUEST['subPrefix']) {
             $parts = explode('/', $_REQUEST['subPrefix']);
             $bookId = $parts[count($parts) - 1 ];
         } else {
@@ -120,26 +120,24 @@ class BookReaderImages
                 $imageIndex = $metadata['titleIndex'];
                 break;
             
-            /* Old 'cover' behaviour where it would show cover 0 if it exists or return 404.
-               Could be re-added as cover0, cover1, etc
-            case 'cover':
+            /* Old 'cover' behaviour where it would show cover 0 if it exists or return 404. */
+            case 'cover0':
                 if (! array_key_exists('coverIndices', $metadata)) {
                     $this->BRfatal("No cover asserted in book");
                 }
                 $imageIndex = $metadata['coverIndices'][0]; // $$$ TODO add support for other covers
                 break;
-            */
             
             case 'preview':
             case 'cover': // Show our best guess if cover is requested
                 // Preference is:
-                //   Cover page if book was published >= 1950
+                //   Cover page if book was published >= 1923
                 //   Title page
                 //   Cover page
                 //   Page 0
                          
                 if ( array_key_exists('date', $metadata) && array_key_exists('coverIndices', $metadata) ) {
-                    if ($brm->parseYear($metadata['date']) >= 1950) {
+                    if ($brm->parseYear($metadata['date']) >= 1923) {
                         $imageIndex = $metadata['coverIndices'][0];                
                         break;
                     }
@@ -224,6 +222,9 @@ class BookReaderImages
      */
      function serveRequest($requestEnv) {
      
+        // Make sure cleanup happens
+        register_shutdown_function ( array( $this, 'cleanup') );
+     
         // Process some of the request parameters
         $zipPath  = $requestEnv['zip'];
         $file     = $requestEnv['file'];
@@ -448,6 +449,7 @@ class BookReaderImages
         $tempFile = $this->getTempFilename($ext);
         array_push($this->tempFiles, $tempFile);
 
+        // error_log("bookreader running " . $cmd);
         $imageCreated = $this->createOutputImage($cmd, $tempFile, $errorMessage);
         if (! $imageCreated) {
             // $$$ automated reporting
@@ -470,8 +472,9 @@ class BookReaderImages
                 $cmd = $unzipCmd . $this->getDecompressCmd($imageInfo, $powReduce, $rotate, $scale, $region, $stdoutLink) . $compressCmd;
                 trigger_error('BookReader rerunning with new cmd: ' . $cmd, E_USER_WARNING);
                 
-                //if ($this->passthruIfSuccessful($headers, $cmd, $errorMessage)) { // $$$ move to BookReaderRequest
-                $imageCreated = $this->createOutputImage($mcd, $tempFile, $errorMessage);
+                $tempFile = $this->getTempFilename($ext);
+                array_push($this->tempFiles, $tempFile);
+                $imageCreated = $this->createOutputImage($cmd, $tempFile, $errorMessage);
                 if ($imageCreated) {
                     $recovered = true;
                 } else {
@@ -481,7 +484,7 @@ class BookReaderImages
             }
             
             if (! $recovered) {
-                $this->BRfatal('Problem processing image - command failed');
+                $this->BRfatal("Problem processing image - command failed:\n " . $cmd);
             }
         }
         
@@ -819,7 +822,7 @@ class BookReaderImages
     function createOutputImage($cmd, $tempFile, &$errorMessage) {
         $fullCmd = $cmd . " > " . $tempFile;
         system($fullCmd); // $$$ better error handling
-        return file_exists($tempFile);
+        return file_exists($tempFile) && filesize($tempFile) > 0;
     }
     
     function BRfatal($string) {
@@ -1097,9 +1100,10 @@ class BookReaderImages
             if (is_writeable($fast)) {
                 if (mkdir($fastbr)) {
                     return $fastbr;
+                } else {
+                    return $fast;
                 }
             }
-            return $fast;
         }
         
         // All else failed -- system tmp that should get cleaned on reboot
@@ -1117,6 +1121,13 @@ class BookReaderImages
         }
         $this->tempFiles = array();
     }
+
+    /*    
+    function cleanTmp() {
+        system('find /var/tmp/fast -name "BookReaderImages*" -cmin +10 -exec rm {} \;');
+        system('find /var/tmp/fast/bookreader -name "BookReaderImages*" -cmin +10 -exec rm {} \;');
+    }
+    */
     
 }