Merge branch 'saas-dev' into saas-stream-settings

This commit is contained in:
Duncan Sommerville 2015-07-08 12:47:24 -04:00
commit 6b9d9e8063
17 changed files with 186 additions and 141 deletions

View file

@ -27,17 +27,18 @@ class Application_Model_Preference
private static function setValue($key, $value, $isUserValue = false)
{
$cache = new Cache();
try {
$con = Propel::getConnection(CcPrefPeer::DATABASE_NAME);
$con->beginTransaction();
$con = Propel::getConnection(CcPrefPeer::DATABASE_NAME);
$con->beginTransaction();
try {
/* Comment this out while we reevaluate it in favor of a unique constraint
static::_lock($con); */
$userId = self::getUserId();
if ($isUserValue && is_null($userId))
if ($isUserValue && is_null($userId)) {
throw new Exception("User id can't be null for a user preference {$key}.");
}
//Check if key already exists
$sql = "SELECT COUNT(*) FROM cc_pref"
." WHERE keystr = :key";
@ -113,6 +114,28 @@ class Application_Model_Preference
$cache->store($key, $value, $isUserValue, $userId);
}
/**
* Given a PDO connection, lock the cc_pref table for the current transaction
*
* Creates a table level lock, which defaults to ACCESS EXCLUSIVE mode;
* see http://www.postgresql.org/docs/9.1/static/explicit-locking.html
*
* @param PDO $con
*/
private static function _lock($con) {
// If we're not in a transaction, a lock is pointless
if (!$con->inTransaction()) {
return;
}
// Don't specify NOWAIT here; we should block on obtaining this lock
// in case we're handling simultaneous requests.
// Locks only last until the end of the transaction, so we shouldn't have to
// worry about this causing any noticeable difference in request processing speed
$sql = "LOCK TABLE cc_pref";
$st = $con->prepare($sql);
$st->execute();
}
private static function getValue($key, $isUserValue = false)
{
$cache = new Cache();
@ -140,9 +163,9 @@ class Application_Model_Preference
$sql .= " AND subjid = :id";
$paramMap[':id'] = $userId;
}
$result = Application_Common_Database::prepareAndExecute($sql, $paramMap, Application_Common_Database::COLUMN);
//return an empty string if the result doesn't exist.
if ($result == 0) {
$res = "";

View file

@ -17,6 +17,7 @@ class Application_Model_StoredFile
{
/**
* @holds propel database object
* @var CcFiles
*/
private $_file;
@ -467,48 +468,6 @@ SQL;
$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
*
@ -568,7 +527,7 @@ SQL;
*/
public function getRelativeFileUrl($baseUrl)
{
return $baseUrl."api/get-media/file/".$this->getId().".".$this->getFileExtension();
return $baseUrl."api/get-media/file/".$this->getId();
}
public function getResourceId()

View file

@ -205,11 +205,6 @@ class CcFiles extends BaseCcFiles {
$cloudFile->save();
Application_Model_Preference::updateDiskUsage($fileSizeBytes);
$now = new DateTime("now", new DateTimeZone("UTC"));
$file->setDbMtime($now);
$file->save();
} else if ($file) {
// Since we check for this value when deleting files, set it first
@ -238,14 +233,13 @@ class CcFiles extends BaseCcFiles {
$file->setDbFilepath($filePathRelativeToStor);
}
}
$now = new DateTime("now", new DateTimeZone("UTC"));
$file->setDbMtime($now);
$file->save();
} else {
throw new FileNotFoundException();
}
$now = new DateTime("now", new DateTimeZone("UTC"));
$file->setDbMtime($now);
$file->save();
}
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
* @param $file string a CcFiles object
*
* @param CcFiles $file a CcFiles object
*
* @return array
*/
//TODO: rename this function?
public static function sanitizeResponse($file)
{
public static function sanitizeResponse($file) {
$response = $file->toArray(BasePeer::TYPE_FIELDNAME);
foreach (self::$privateFields as $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;
}