From e0269e1e229a85421273e2e4f3c0b20829b53bf3 Mon Sep 17 00:00:00 2001 From: Duncan Sommerville Date: Tue, 3 Feb 2015 14:16:44 -0500 Subject: [PATCH 1/2] smartReadFile fix for saas show-logo --- .../application/controllers/ApiController.php | 46 +++++++++++++++---- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/airtime_mvc/application/controllers/ApiController.php b/airtime_mvc/application/controllers/ApiController.php index 089052e68..2f54a64ae 100644 --- a/airtime_mvc/application/controllers/ApiController.php +++ b/airtime_mvc/application/controllers/ApiController.php @@ -115,7 +115,7 @@ class ApiController extends Zend_Controller_Action header('Content-Disposition: inline; filename="'.$filename.'"'); } - $this->smartReadFile($media); + $this->readStoredFileObject($media); exit; } else { header ("HTTP/1.1 404 Not Found"); @@ -124,29 +124,55 @@ class ApiController extends Zend_Controller_Action $this->_helper->json->sendJson(array()); } + + /** + * Read data from StoredFile object and send with XHR response + * + * @param Application_Model_StoredFile $storedFile - StoredFile object holding file information + */ + private function readStoredFileObject($storedFile) { + $filepath = $storedFile->getFilePath(); + $size = $storedFile->getFileSize(); + $mimeType = $storedFile->getPropelOrm()->getDbMime(); + + $this->smartReadFile($filepath, $size, $mimeType); + } + /** * Reads the requested portion of a file and sends its contents to the client with the appropriate headers. * * This HTTP_RANGE compatible read file function is necessary for allowing streaming media to be skipped around in. * - * @param string $location - * @param string $mimeType + * @param string $location - the full filepath pointing to the location of the file + * @param string $mimeType - the file's mime type. Defaults to 'audio/mp3' + * @param integer $size - the file size, in bytes * @return void * * @link https://groups.google.com/d/msg/jplayer/nSM2UmnSKKA/Hu76jDZS4xcJ * @link http://php.net/manual/en/function.readfile.php#86244 */ - public function smartReadFile($media) + private function smartReadFile($location, $mimeType = 'audio/mp3', $size = null) { - $filepath = $media->getFilePath(); - $size= $media->getFileSize(); - $mimeType = $media->getPropelOrm()->getDbMime(); - - $fm = @fopen($filepath, 'rb'); + if (!$location || $location == "") { + throw new FileDoesNotExistException("Requested file does not exist!"); + } + + // If we're passing in a Stored File object, it's faster + // to use getFileSize() and pass in the result + if (!$size || $size <= 0) { + $size= filesize($location); + } + + if ($size <= 0) { + throw new Exception("Invalid file size returned for file at $location"); + } + + $time= date('r', filemtime($location)); + + $fm = @fopen($location, 'rb'); if (!$fm) { header ("HTTP/1.1 505 Internal server error"); - return; } From 6601e016a6a1707619244f8f076e977f2c404fdc Mon Sep 17 00:00:00 2001 From: Duncan Sommerville Date: Tue, 3 Feb 2015 15:19:41 -0500 Subject: [PATCH 2/2] Removed unused variable --- airtime_mvc/application/controllers/ApiController.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/airtime_mvc/application/controllers/ApiController.php b/airtime_mvc/application/controllers/ApiController.php index 2f54a64ae..b7937de0d 100644 --- a/airtime_mvc/application/controllers/ApiController.php +++ b/airtime_mvc/application/controllers/ApiController.php @@ -168,8 +168,6 @@ class ApiController extends Zend_Controller_Action throw new Exception("Invalid file size returned for file at $location"); } - $time= date('r', filemtime($location)); - $fm = @fopen($location, 'rb'); if (!$fm) { header ("HTTP/1.1 505 Internal server error");