2015-02-19 21:10:01 +01:00
|
|
|
<?php
|
|
|
|
|
2015-02-20 20:01:06 +01:00
|
|
|
require_once('ProxyStorageBackend.php');
|
2015-02-25 19:02:11 +01:00
|
|
|
require_once("FileIO.php");
|
2015-02-20 20:01:06 +01:00
|
|
|
|
2015-02-19 21:10:01 +01:00
|
|
|
class Application_Service_MediaService
|
|
|
|
{
|
2015-02-20 22:36:36 +01:00
|
|
|
/** Move (or copy) a file to the stor/organize directory and send it off to the
|
|
|
|
analyzer to be processed.
|
|
|
|
* @param $callbackUrl
|
|
|
|
* @param $filePath string Path to the local file to import to the library
|
|
|
|
* @param $originalFilename string The original filename, if you want it to be preserved after import.
|
|
|
|
* @param $ownerId string The ID of the user that will own the file inside Airtime.
|
|
|
|
* @param $copyFile bool True if you want to copy the file to the "organize" directory, false if you want to move it (default)
|
|
|
|
* @return Ambigous
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public static function importFileToLibrary($callbackUrl, $filePath, $originalFilename, $ownerId, $copyFile)
|
2015-02-19 21:10:01 +01:00
|
|
|
{
|
|
|
|
$CC_CONFIG = Config::getConfig();
|
|
|
|
$apiKey = $CC_CONFIG["apiKey"][0];
|
|
|
|
|
2015-02-20 20:01:06 +01:00
|
|
|
$importedStorageDirectory = "";
|
|
|
|
if ($CC_CONFIG["current_backend"] == "file") {
|
|
|
|
$storDir = Application_Model_MusicDir::getStorDir();
|
|
|
|
$importedStorageDirectory = $storDir->getDirectory() . "/imported/" . $ownerId;
|
|
|
|
}
|
2015-02-19 21:10:01 +01:00
|
|
|
|
2015-02-20 22:36:36 +01:00
|
|
|
//Copy the temporary file over to the "organize" folder so that it's off our webserver
|
|
|
|
//and accessible by airtime_analyzer which could be running on a different machine.
|
|
|
|
$newTempFilePath = Application_Model_StoredFile::moveFileToStor($filePath, $originalFilename, $copyFile);
|
2015-02-19 21:10:01 +01:00
|
|
|
|
|
|
|
//Dispatch a message to airtime_analyzer through RabbitMQ,
|
|
|
|
//notifying it that there's a new upload to process!
|
2015-02-20 20:01:06 +01:00
|
|
|
$storageBackend = new ProxyStorageBackend($CC_CONFIG["current_backend"]);
|
2015-02-19 21:10:01 +01:00
|
|
|
Application_Model_RabbitMq::SendMessageToAnalyzer($newTempFilePath,
|
|
|
|
$importedStorageDirectory, basename($originalFilename),
|
2015-02-20 20:01:06 +01:00
|
|
|
$callbackUrl, $apiKey,
|
|
|
|
$CC_CONFIG["current_backend"],
|
|
|
|
$storageBackend->getFilePrefix());
|
2015-02-20 22:36:36 +01:00
|
|
|
|
|
|
|
return $newTempFilePath;
|
2015-02-20 20:01:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $fileId
|
|
|
|
* @param bool $inline Set the Content-Disposition header to inline to prevent a download dialog from popping up (or attachment if false)
|
|
|
|
* @throws Exception
|
|
|
|
* @throws FileNotFoundException
|
|
|
|
*/
|
|
|
|
public static function streamFileDownload($fileId, $inline=false)
|
|
|
|
{
|
|
|
|
$media = Application_Model_StoredFile::RecallById($fileId);
|
|
|
|
if ($media == null) {
|
|
|
|
throw new FileNotFoundException();
|
|
|
|
}
|
2015-03-30 17:31:07 +02:00
|
|
|
// Make sure we don't have some wrong result because of caching
|
2015-02-20 20:01:06 +01:00
|
|
|
clearstatcache();
|
|
|
|
|
2015-03-30 17:31:07 +02:00
|
|
|
$filePath = "";
|
|
|
|
|
2015-02-20 20:01:06 +01:00
|
|
|
if ($media->getPropelOrm()->isValidPhysicalFile()) {
|
|
|
|
$filename = $media->getPropelOrm()->getFilename();
|
|
|
|
//Download user left clicks a track and selects Download.
|
|
|
|
if (!$inline) {
|
|
|
|
//We are using Content-Disposition to specify
|
|
|
|
//to the browser what name the file should be saved as.
|
|
|
|
header('Content-Disposition: attachment; filename="' . $filename . '"');
|
|
|
|
} else {
|
|
|
|
//user clicks play button for track and downloads it.
|
|
|
|
header('Content-Disposition: inline; filename="' . $filename . '"');
|
|
|
|
}
|
|
|
|
|
2015-03-30 17:31:07 +02:00
|
|
|
/*
|
|
|
|
In this block of code below, we're getting the list of download URLs for a track
|
|
|
|
and then streaming the file as the response. A file can be stored in more than one location,
|
|
|
|
with the alternate locations used as a fallback, so that's why we're looping until we
|
|
|
|
are able to actually send the file.
|
|
|
|
|
|
|
|
This mechanism is used to try fetching our file from our internal S3 caching proxy server first.
|
|
|
|
If the file isn't found there (or the cache is down), then we attempt to download the file
|
|
|
|
directly from Amazon S3. We do this to save bandwidth costs!
|
|
|
|
*/
|
|
|
|
|
|
|
|
$filePaths = $media->getFilePaths();
|
|
|
|
assert(is_array($filePaths));
|
|
|
|
|
|
|
|
do {
|
|
|
|
//Read from $filePath and stream it to the browser.
|
|
|
|
$filePath = array_shift($filePaths);
|
|
|
|
try {
|
|
|
|
$size= $media->getFileSize();
|
|
|
|
$mimeType = $media->getPropelOrm()->getDbMime();
|
|
|
|
Application_Common_FileIO::smartReadFile($filePath, $size, $mimeType);
|
2015-04-01 23:29:21 +02:00
|
|
|
break; //Break out of the loop if we successfully read the file!
|
2015-03-30 17:31:07 +02:00
|
|
|
} catch (FileNotFoundException $e) {
|
|
|
|
//If we have no alternate filepaths left, then let the exception bubble up.
|
|
|
|
if (sizeof($filePaths) == 0) {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//Retry with the next alternate filepath in the list
|
|
|
|
} while (sizeof($filePaths) > 0);
|
|
|
|
|
2015-02-20 20:01:06 +01:00
|
|
|
exit;
|
2015-03-30 17:31:07 +02:00
|
|
|
|
2015-02-20 20:01:06 +01:00
|
|
|
} else {
|
2015-03-30 17:31:07 +02:00
|
|
|
throw new FileNotFoundException($filePath);
|
2015-02-20 20:01:06 +01:00
|
|
|
}
|
2015-02-19 21:10:01 +01:00
|
|
|
}
|
2015-02-20 20:01:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|