Permalink
Browse files

Write to fast tmp file instead of overly clever "stream if successful…

…" code path
  • Loading branch information...
1 parent 03eab96 commit 823909a4e2bebc3f1a08ac662582dcc749b6ae04 @mangtronix mangtronix committed Oct 13, 2011
Showing with 57 additions and 16 deletions.
  1. +56 −15 BookReaderIA/datanode/BookReaderImages.inc.php
  2. +1 −1 BookReaderIA/datanode/BookReaderImages.php
@@ -227,8 +227,8 @@ function serveRequest($requestEnv) {
// Process some of the request parameters
$zipPath = $requestEnv['zip'];
$file = $requestEnv['file'];
- if (! $ext) {
- $ext = $requestEnv['ext'];
+ if (array_key_exists('ext', $requestEnv)) {
+ $ext = $requestEnv['ext']; // Will get santized below
} else {
// Default to jpg
$ext = 'jpeg';
@@ -440,15 +440,16 @@ function serveRequest($requestEnv) {
// print $cmd;
$filenameForClient = $this->filenameForClient($file, $ext);
+
+ $errorMessage = '';
- $headers = array('Content-type: '. self::$MIMES[$ext],
- 'Cache-Control: max-age=15552000',
- 'Content-disposition: inline; filename=' . $filenameForClient);
-
+ //if (! $this->passthruIfSuccessful($headers, $cmd, $errorMessage)) { // $$$ move to BookReaderRequest
- $errorMessage = '';
-
- if (! $this->passthruIfSuccessful($headers, $cmd, $errorMessage)) { // $$$ move to BookReaderRequest
+ $tempFile = $this->getTempFilename($ext);
+ array_push($this->tempFiles, $tempFile);
+
+ $imageCreated = $this->createOutputImage($cmd, $tempFile, $errorMessage);
+ if (! $imageCreated) {
// $$$ automated reporting
trigger_error('BookReader Processing Error: ' . $cmd . ' -- ' . $errorMessage, E_USER_WARNING);
@@ -468,7 +469,10 @@ function serveRequest($requestEnv) {
$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
+
+ //if ($this->passthruIfSuccessful($headers, $cmd, $errorMessage)) { // $$$ move to BookReaderRequest
+ $imageCreated = $this->createOutputImage($mcd, $tempFile, $errorMessage);
+ if ($imageCreated) {
$recovered = true;
} else {
$this->cleanup();
@@ -481,6 +485,22 @@ function serveRequest($requestEnv) {
}
}
+ if ($imageCreated) {
+ // Send the image
+
+ $headers = array('Content-type: '. self::$MIMES[$ext],
+ 'Cache-Control: max-age=15552000',
+ 'Content-disposition: inline; filename=' . $filenameForClient,
+ 'Content-Length: ' . filesize($tempFile));
+
+ foreach($headers as $header) {
+ header($header);
+ }
+ ob_clean();
+ flush(); // attempt to send header to client
+ readfile($tempFile);
+ }
+
$this->cleanup();
}
@@ -696,6 +716,7 @@ function getDecompressCmd($srcInfo, $powReduce, $rotate, $scale, $region, $stdou
$this->BRfatal('Unknown image type: ' . $imageType);
break;
}
+
return $decompressCmd;
}
@@ -713,6 +734,7 @@ function getDecompressCmd($srcInfo, $powReduce, $rotate, $scale, $region, $stdou
// other cases, e.g. if there are warnings on stderr
function passthruIfSuccessful($headers, $cmd, &$errorMessage)
{
+
$retVal = false;
$errorMessage = '';
@@ -794,6 +816,12 @@ function passthruIfSuccessful($headers, $cmd, &$errorMessage)
return $retVal;
}
+ function createOutputImage($cmd, $tempFile, &$errorMessage) {
+ $fullCmd = $cmd . " > " . $tempFile;
+ system($fullCmd); // $$$ better error handling
+ return file_exists($tempFile);
+ }
+
function BRfatal($string) {
$this->cleanup();
throw new Exception("Image error: $string");
@@ -1057,17 +1085,30 @@ function clamp($minValue, $maxValue, $observedValue) {
}
// Get the directory for temporary files. Use the fast in-RAM tmp if available.
- /*
function getTempDir() {
- var $fast = '/var/tmp/fast';
- if (is_dir($fast)) {
- // We assume it's writeable
+ $fastbr = '/var/tmp/fast/bookreader';
+
+ if (is_writeable($fastbr)) {
+ // Our directory in fast tmp already exists
+ return $fastbr;
+ } else {
+ // Check for fast tmp and make our directory
+ $fast = '/var/tmp/fast';
+ if (is_writeable($fast)) {
+ if (mkdir($fastbr)) {
+ return $fastbr;
+ }
+ }
return $fast;
}
+ // All else failed -- system tmp that should get cleaned on reboot
return '/tmp';
}
- */
+
+ function getTempFilename($ext) {
+ return tempnam($this->getTempDir(), "BookReaderImages");
+ }
// Clean up temporary files and resources
function cleanup() {
@@ -30,7 +30,7 @@
// Serve request
$bri = new BookReaderImages();
try {
- if ($_REQUEST['page']) {
+ if (array_key_exists('page', $_REQUEST) && $_REQUEST['page']) {
// Need to lookup metadata
$bri->serveLookupRequest($_REQUEST);
} else {

0 comments on commit 823909a

Please sign in to comment.