Merge pull request #90 from sourcefabric/saas-readfile

smartReadFile fix for saas show-logo
This commit is contained in:
Albert Santoni 2015-02-03 15:20:07 -05:00
commit 47f1113106
1 changed files with 34 additions and 10 deletions

View File

@ -115,7 +115,7 @@ class ApiController extends Zend_Controller_Action
header('Content-Disposition: inline; filename="'.$filename.'"'); header('Content-Disposition: inline; filename="'.$filename.'"');
} }
$this->smartReadFile($media); $this->readStoredFileObject($media);
exit; exit;
} else { } else {
header ("HTTP/1.1 404 Not Found"); header ("HTTP/1.1 404 Not Found");
@ -124,29 +124,53 @@ class ApiController extends Zend_Controller_Action
$this->_helper->json->sendJson(array()); $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. * 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. * This HTTP_RANGE compatible read file function is necessary for allowing streaming media to be skipped around in.
* *
* @param string $location * @param string $location - the full filepath pointing to the location of the file
* @param string $mimeType * @param string $mimeType - the file's mime type. Defaults to 'audio/mp3'
* @param integer $size - the file size, in bytes
* @return void * @return void
* *
* @link https://groups.google.com/d/msg/jplayer/nSM2UmnSKKA/Hu76jDZS4xcJ * @link https://groups.google.com/d/msg/jplayer/nSM2UmnSKKA/Hu76jDZS4xcJ
* @link http://php.net/manual/en/function.readfile.php#86244 * @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(); if (!$location || $location == "") {
$size= $media->getFileSize(); throw new FileDoesNotExistException("Requested file does not exist!");
$mimeType = $media->getPropelOrm()->getDbMime(); }
$fm = @fopen($filepath, 'rb'); // 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");
}
$fm = @fopen($location, 'rb');
if (!$fm) { if (!$fm) {
header ("HTTP/1.1 505 Internal server error"); header ("HTTP/1.1 505 Internal server error");
return; return;
} }