SAAS-922 - expose mime-based file extensions from /rest/media
This commit is contained in:
parent
0d27011e65
commit
92c9fd30b5
5 changed files with 51 additions and 68 deletions
|
@ -24,4 +24,31 @@ class FileDataHelper {
|
||||||
$data["bpm"] = intval($data["bpm"]);
|
$data["bpm"] = intval($data["bpm"]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a suitable extension for the given file
|
||||||
|
*
|
||||||
|
* @param string $mime
|
||||||
|
*
|
||||||
|
* @return string file extension with(!) a dot
|
||||||
|
*
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static function getFileExtensionFromMime($mime)
|
||||||
|
{
|
||||||
|
if ($mime == "audio/ogg" || $mime == "application/ogg" || $mime == "audio/vorbis") {
|
||||||
|
return ".ogg";
|
||||||
|
} elseif ($mime == "audio/mp3" || $mime == "audio/mpeg" || $mime == "audio/mpeg3") {
|
||||||
|
return ".mp3";
|
||||||
|
} elseif ($mime == "audio/x-flac") {
|
||||||
|
return ".flac";
|
||||||
|
} elseif ($mime == "audio/mp4") {
|
||||||
|
return ".mp4";
|
||||||
|
} elseif ($mime == "audio/wav" || $mime == "audio/x-wav") {
|
||||||
|
return ".wav";
|
||||||
|
} else {
|
||||||
|
throw new Exception("Unknown $mime");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -45,8 +45,6 @@ class Application_Common_HTTPHelper
|
||||||
|
|
||||||
class ZendActionHttpException extends Exception {
|
class ZendActionHttpException extends Exception {
|
||||||
|
|
||||||
private $_action;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Zend_Controller_Action $action
|
* @param Zend_Controller_Action $action
|
||||||
* @param int $statusCode
|
* @param int $statusCode
|
||||||
|
@ -58,8 +56,7 @@ class ZendActionHttpException extends Exception {
|
||||||
*/
|
*/
|
||||||
public function __construct(Zend_Controller_Action $action, $statusCode, $message,
|
public function __construct(Zend_Controller_Action $action, $statusCode, $message,
|
||||||
$code = 0, Exception $previous = null) {
|
$code = 0, Exception $previous = null) {
|
||||||
$this->_action = $action;
|
Logging::error("Error in action " . $action->getRequest()->getActionName()
|
||||||
Logging::info("Error in action " . $action->getRequest()->getActionName()
|
|
||||||
. " with status code $statusCode: $message");
|
. " with status code $statusCode: $message");
|
||||||
$action->getResponse()
|
$action->getResponse()
|
||||||
->setHttpResponseCode($statusCode)
|
->setHttpResponseCode($statusCode)
|
||||||
|
@ -67,8 +64,4 @@ class ZendActionHttpException extends Exception {
|
||||||
parent::__construct($message, $code, $previous);
|
parent::__construct($message, $code, $previous);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getAction() {
|
|
||||||
return $this->_action;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -163,9 +163,9 @@ class Application_Model_Preference
|
||||||
$sql .= " AND subjid = :id";
|
$sql .= " AND subjid = :id";
|
||||||
$paramMap[':id'] = $userId;
|
$paramMap[':id'] = $userId;
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = Application_Common_Database::prepareAndExecute($sql, $paramMap, Application_Common_Database::COLUMN);
|
$result = Application_Common_Database::prepareAndExecute($sql, $paramMap, Application_Common_Database::COLUMN);
|
||||||
|
|
||||||
//return an empty string if the result doesn't exist.
|
//return an empty string if the result doesn't exist.
|
||||||
if ($result == 0) {
|
if ($result == 0) {
|
||||||
$res = "";
|
$res = "";
|
||||||
|
|
|
@ -17,6 +17,7 @@ class Application_Model_StoredFile
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @holds propel database object
|
* @holds propel database object
|
||||||
|
* @var CcFiles
|
||||||
*/
|
*/
|
||||||
private $_file;
|
private $_file;
|
||||||
|
|
||||||
|
@ -467,48 +468,6 @@ SQL;
|
||||||
$this->_file->save();
|
$this->_file->save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function getRealFileExtension() {
|
|
||||||
$path = $this->_file->getDbFilepath();
|
|
||||||
$path_elements = explode('.', $path);
|
|
||||||
if (count($path_elements) < 2) {
|
|
||||||
return "";
|
|
||||||
} else {
|
|
||||||
return $path_elements[count($path_elements) - 1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return suitable extension.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
* file extension without a dot
|
|
||||||
*/
|
|
||||||
public function getFileExtension()
|
|
||||||
{
|
|
||||||
$possible_ext = $this->getRealFileExtension();
|
|
||||||
if ($possible_ext !== "") {
|
|
||||||
return $possible_ext;
|
|
||||||
}
|
|
||||||
|
|
||||||
// We fallback to guessing the extension from the mimetype if we
|
|
||||||
// cannot extract it from the file name
|
|
||||||
|
|
||||||
$mime = $this->_file->getDbMime();
|
|
||||||
|
|
||||||
if ($mime == "audio/ogg" || $mime == "application/ogg" || $mime == "audio/vorbis") {
|
|
||||||
return "ogg";
|
|
||||||
} elseif ($mime == "audio/mp3" || $mime == "audio/mpeg") {
|
|
||||||
return "mp3";
|
|
||||||
} elseif ($mime == "audio/x-flac") {
|
|
||||||
return "flac";
|
|
||||||
} elseif ($mime == "audio/mp4") {
|
|
||||||
return "mp4";
|
|
||||||
} else {
|
|
||||||
throw new Exception("Unknown $mime");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the absolute filepath
|
* Get the absolute filepath
|
||||||
*
|
*
|
||||||
|
@ -568,7 +527,7 @@ SQL;
|
||||||
*/
|
*/
|
||||||
public function getRelativeFileUrl($baseUrl)
|
public function getRelativeFileUrl($baseUrl)
|
||||||
{
|
{
|
||||||
return $baseUrl."api/get-media/file/".$this->getId().".".$this->getFileExtension();
|
return $baseUrl."api/get-media/file/".$this->getId().FileDataHelper::getFileExtensionFromMime($this->_file->getDbMime());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getResourceId()
|
public function getResourceId()
|
||||||
|
|
|
@ -205,11 +205,6 @@ class CcFiles extends BaseCcFiles {
|
||||||
$cloudFile->save();
|
$cloudFile->save();
|
||||||
|
|
||||||
Application_Model_Preference::updateDiskUsage($fileSizeBytes);
|
Application_Model_Preference::updateDiskUsage($fileSizeBytes);
|
||||||
|
|
||||||
$now = new DateTime("now", new DateTimeZone("UTC"));
|
|
||||||
$file->setDbMtime($now);
|
|
||||||
$file->save();
|
|
||||||
|
|
||||||
} else if ($file) {
|
} else if ($file) {
|
||||||
|
|
||||||
// Since we check for this value when deleting files, set it first
|
// Since we check for this value when deleting files, set it first
|
||||||
|
@ -238,14 +233,13 @@ class CcFiles extends BaseCcFiles {
|
||||||
$file->setDbFilepath($filePathRelativeToStor);
|
$file->setDbFilepath($filePathRelativeToStor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$now = new DateTime("now", new DateTimeZone("UTC"));
|
|
||||||
$file->setDbMtime($now);
|
|
||||||
$file->save();
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw new FileNotFoundException();
|
throw new FileNotFoundException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$now = new DateTime("now", new DateTimeZone("UTC"));
|
||||||
|
$file->setDbMtime($now);
|
||||||
|
$file->save();
|
||||||
}
|
}
|
||||||
catch (FileNotFoundException $e)
|
catch (FileNotFoundException $e)
|
||||||
{
|
{
|
||||||
|
@ -356,17 +350,27 @@ class CcFiles extends BaseCcFiles {
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Strips out the private fields we do not want to send back in API responses
|
* Strips out the private fields we do not want to send back in API responses
|
||||||
* @param $file string a CcFiles object
|
*
|
||||||
|
* @param CcFiles $file a CcFiles object
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
//TODO: rename this function?
|
//TODO: rename this function?
|
||||||
public static function sanitizeResponse($file)
|
public static function sanitizeResponse($file) {
|
||||||
{
|
|
||||||
$response = $file->toArray(BasePeer::TYPE_FIELDNAME);
|
$response = $file->toArray(BasePeer::TYPE_FIELDNAME);
|
||||||
|
|
||||||
foreach (self::$privateFields as $key) {
|
foreach (self::$privateFields as $key) {
|
||||||
unset($response[$key]);
|
unset($response[$key]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$mime = $file->getDbMime();
|
||||||
|
if (!empty($mime)) {
|
||||||
|
// Get an extension based on the file's mime type and change the path to use this extension
|
||||||
|
$path = pathinfo($file->getDbFilepath());
|
||||||
|
$ext = FileDataHelper::getFileExtensionFromMime($mime);
|
||||||
|
$response["filepath"] = ($path["dirname"] . '/' . $path["filename"] . $ext);
|
||||||
|
}
|
||||||
|
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue