From f39f9329cc39dcac3fbefec8db804824ad5febc9 Mon Sep 17 00:00:00 2001 From: Martin Konecny Date: Fri, 19 Apr 2013 20:10:51 -0400 Subject: [PATCH] CC-4992: Decouple pypo from Airtime install -first commit --- .../application/controllers/ApiController.php | 144 ++++++++++-------- airtime_mvc/application/models/Schedule.php | 1 + airtime_mvc/application/models/Scheduler.php | 2 +- airtime_mvc/application/models/StoredFile.php | 56 ++++--- 4 files changed, 116 insertions(+), 87 deletions(-) diff --git a/airtime_mvc/application/controllers/ApiController.php b/airtime_mvc/application/controllers/ApiController.php index eaf24e223..72c296622 100644 --- a/airtime_mvc/application/controllers/ApiController.php +++ b/airtime_mvc/application/controllers/ApiController.php @@ -511,81 +511,88 @@ class ApiController extends Zend_Controller_Action { $return_hash = array(); Application_Model_Preference::SetImportTimestamp(); - //Logging::info("--->Mode: $mode || file: {$md['MDATA_KEY_FILEPATH']} "); - //Logging::info( $md ); - // create also modifies the file if it exists - if ($mode == "create") { - $filepath = $md['MDATA_KEY_FILEPATH']; - $filepath = Application_Common_OsPath::normpath($filepath); - $file = Application_Model_StoredFile::RecallByFilepath($filepath); - if (is_null($file)) { - $file = Application_Model_StoredFile::Insert($md); - } else { - // If the file already exists we will update and make sure that - // it's marked as 'exists'. - $file->setFileExistsFlag(true); - $file->setFileHiddenFlag(false); - $file->setMetadata($md); - } - if ($md['is_record'] != 0) { - $this->uploadRecordedActionParam($md['MDATA_KEY_TRACKNUMBER'], $file->getId()); - } - - } elseif ($mode == "modify") { - $filepath = $md['MDATA_KEY_FILEPATH']; - $file = Application_Model_StoredFile::RecallByFilepath($filepath); + $con = Propel::getConnection(CcFilesPeer::DATABASE_NAME); + $con->beginTransaction(); + try { + // create also modifies the file if it exists + if ($mode == "create") { + $filepath = $md['MDATA_KEY_FILEPATH']; + $filepath = Application_Common_OsPath::normpath($filepath); + $file = Application_Model_StoredFile::RecallByFilepath($filepath, $con); + if (is_null($file)) { + $file = Application_Model_StoredFile::Insert($md, $con); + } else { + // If the file already exists we will update and make sure that + // it's marked as 'exists'. + $file->setFileExistsFlag(true); + $file->setFileHiddenFlag(false); + $file->setMetadata($md); + } + if ($md['is_record'] != 0) { + $this->uploadRecordedActionParam($md['MDATA_KEY_TRACKNUMBER'], $file->getId()); + } + + } elseif ($mode == "modify") { + $filepath = $md['MDATA_KEY_FILEPATH']; + $file = Application_Model_StoredFile::RecallByFilepath($filepath, $con); - //File is not in database anymore. - if (is_null($file)) { - $return_hash['error'] = _("File does not exist in Airtime."); + //File is not in database anymore. + if (is_null($file)) { + $return_hash['error'] = _("File does not exist in Airtime."); - return $return_hash; - } - //Updating a metadata change. - else { - $file->setMetadata($md); - } - } elseif ($mode == "moved") { - $file = Application_Model_StoredFile::RecallByFilepath( - $md['MDATA_KEY_ORIGINAL_PATH']); + return $return_hash; + } + //Updating a metadata change. + else { + $file->setMetadata($md); + } + } elseif ($mode == "moved") { + $file = Application_Model_StoredFile::RecallByFilepath( + $md['MDATA_KEY_ORIGINAL_PATH'], $con); - if (is_null($file)) { - $return_hash['error'] = _('File does not exist in Airtime'); - } else { + if (is_null($file)) { + $return_hash['error'] = _('File does not exist in Airtime'); + } else { + $filepath = $md['MDATA_KEY_FILEPATH']; + //$filepath = str_replace("\\", "", $filepath); + $file->setFilePath($filepath); + } + } elseif ($mode == "delete") { + $filepath = $md['MDATA_KEY_FILEPATH']; + $filepath = str_replace("\\", "", $filepath); + $file = Application_Model_StoredFile::RecallByFilepath($filepath, $con); + + if (is_null($file)) { + $return_hash['error'] = _("File doesn't exist in Airtime."); + Logging::warn("Attempt to delete file that doesn't exist. + Path: '$filepath'"); + + return $return_hash; + } else { + $file->deleteByMediaMonitor(); + } + } elseif ($mode == "delete_dir") { $filepath = $md['MDATA_KEY_FILEPATH']; //$filepath = str_replace("\\", "", $filepath); - $file->setFilePath($filepath); - } - } elseif ($mode == "delete") { - $filepath = $md['MDATA_KEY_FILEPATH']; - $filepath = str_replace("\\", "", $filepath); - $file = Application_Model_StoredFile::RecallByFilepath($filepath); + $files = Application_Model_StoredFile::RecallByPartialFilepath($filepath, $con); - if (is_null($file)) { - $return_hash['error'] = _("File doesn't exist in Airtime."); - Logging::warn("Attempt to delete file that doesn't exist. - Path: '$filepath'"); + foreach ($files as $file) { + $file->deleteByMediaMonitor(); + } + $return_hash['success'] = 1; return $return_hash; - } else { - $file->deleteByMediaMonitor(); } - } elseif ($mode == "delete_dir") { - $filepath = $md['MDATA_KEY_FILEPATH']; - //$filepath = str_replace("\\", "", $filepath); - $files = Application_Model_StoredFile::RecallByPartialFilepath($filepath); - foreach ($files as $file) { - $file->deleteByMediaMonitor(); - } - $return_hash['success'] = 1; - - return $return_hash; + $return_hash['fileid'] = is_null($file) ? '-1' : $file->getId(); + $con->commit(); + } catch (Exception $e) { + Logging::warn("rolling back"); + Logging::warn($e->getMessage()); + $con->rollback(); + $return_hash['error'] = $e->getMessage(); } - - $return_hash['fileid'] = is_null($file) ? '-1' : $file->getId(); - return $return_hash; } @@ -604,6 +611,10 @@ class ApiController extends Zend_Controller_Action // least 1 digit if ( !preg_match('/^md\d+$/', $k) ) { continue; } $info_json = json_decode($raw_json, $assoc = true); + + Logging::info($info_json); + //$info_json['MDATA_KEY_TRACKNUMBER'] = '4294967295'; + // Log invalid requests if ( !array_key_exists('mode', $info_json) ) { Logging::info("Received bad request(key=$k), no 'mode' parameter. Bad request is:"); @@ -627,7 +638,12 @@ class ApiController extends Zend_Controller_Action // Removing 'mode' key from $info_json might not be necessary... $mode = $info_json['mode']; unset( $info_json['mode'] ); - $response = $this->dispatchMetadata($info_json, $mode); + try { + $response = $this->dispatchMetadata($info_json, $mode); + } catch (Exception $e) { + Logging::warn($e->getMessage()); + Logging::warn(gettype($e)); + } // We tack on the 'key' back to every request in case the would like to associate // his requests with particular responses $response['key'] = $k; diff --git a/airtime_mvc/application/models/Schedule.php b/airtime_mvc/application/models/Schedule.php index 1aac2566e..145aebfc9 100644 --- a/airtime_mvc/application/models/Schedule.php +++ b/airtime_mvc/application/models/Schedule.php @@ -838,6 +838,7 @@ SQL; { $utcTimeZone = new DateTimeZone("UTC"); $items = self::getItems($range_start, $range_end); + foreach ($items as $item) { $showEndDateTime = new DateTime($item["show_end"], $utcTimeZone); diff --git a/airtime_mvc/application/models/Scheduler.php b/airtime_mvc/application/models/Scheduler.php index ee8263c2c..eadbe9789 100644 --- a/airtime_mvc/application/models/Scheduler.php +++ b/airtime_mvc/application/models/Scheduler.php @@ -136,7 +136,7 @@ class Application_Model_Scheduler if ($type === "audioclip") { $file = CcFilesQuery::create()->findPK($id, $this->con); - $storedFile = new Application_Model_StoredFile($file->getDbId()); + $storedFile = new Application_Model_StoredFile($file, $this->con); if (is_null($file) || !$file->visible()) { throw new Exception(_("A selected File does not exist!")); diff --git a/airtime_mvc/application/models/StoredFile.php b/airtime_mvc/application/models/StoredFile.php index 6f5ed142d..5a309bfc4 100644 --- a/airtime_mvc/application/models/StoredFile.php +++ b/airtime_mvc/application/models/StoredFile.php @@ -20,6 +20,11 @@ class Application_Model_StoredFile */ private $_file; + /** + * @holds PDO object reference + */ + private $_con; + /** * array of db metadata -> propel */ @@ -53,6 +58,11 @@ class Application_Model_StoredFile "cueout" => "DbCueOut", ); + function __construct($file, $con) { + $this->_file = $file; + $this->_con = $con; + } + public function getId() { return $this->_file->getDbId(); @@ -86,9 +96,8 @@ class Application_Model_StoredFile $this->_file->save(); } - public static function createWithFile($f) { - $storedFile = new Application_Model_StoredFile(); - $storedFile->_file = $f; + public static function createWithFile($f, $con) { + $storedFile = new Application_Model_StoredFile($f, $con); return $storedFile; } @@ -218,7 +227,7 @@ class Application_Model_StoredFile } $this->_file->setDbMtime(new DateTime("now", new DateTimeZone("UTC"))); - $this->_file->save(); + $this->_file->save($this->_con); } /** @@ -491,7 +500,7 @@ SQL; $this->_file->setDbDirectory($musicDir->getId()); $this->_file->setDbFilepath($path_info[1]); - $this->_file->save(); + $this->_file->save($this->_con); } /** @@ -544,7 +553,7 @@ SQL; return $baseUrl."api/get-media/file/".$this->getId().".".$this->getFileExtension(); } - public static function Insert($md) + public static function Insert($md, $con) { // save some work by checking if filepath is given right away if ( !isset($md['MDATA_KEY_FILEPATH']) ) { @@ -556,8 +565,7 @@ SQL; $file->setDbUtime($now); $file->setDbMtime($now); - $storedFile = new Application_Model_StoredFile(); - $storedFile->_file = $file; + $storedFile = new Application_Model_StoredFile($file, $con); // removed "//" in the path. Always use '/' for path separator // TODO : it might be better to just call OsPath::normpath on the file @@ -575,19 +583,24 @@ SQL; } public static function Recall($p_id=null, $p_gunid=null, $p_md5sum=null, - $p_filepath=null) { - if( isset($p_id ) ) { - $f = CcFilesQuery::create()->findPK(intval($p_id)); - return is_null($f) ? null : self::createWithFile($f); + $p_filepath=null, $con=null) { + + //TODO + if (is_null($con)) { + $con = Propel::getConnection(CcFilesPeer::DATABASE_NAME); + } + + if (isset($p_id)) { + $f = CcFilesQuery::create()->findPK(intval($p_id), $con); + return is_null($f) ? null : self::createWithFile($f, $con); } elseif ( isset($p_gunid) ) { throw new Exception("You should never use gunid ($gunid) anymore"); } elseif ( isset($p_md5sum) ) { throw new Exception("Searching by md5($p_md5sum) is disabled"); } elseif ( isset($p_filepath) ) { - return is_null($f) ? null : self::createWithFile( - Application_Model_StoredFile::RecallByFilepath($p_filepath)); + return is_null($f) ? null : Application_Model_StoredFile::RecallByFilepath($p_filepath, $con); } else { - throw new Exception("No arguments passsed to Recall"); + throw new Exception("No arguments passed to Recall"); } } @@ -603,7 +616,7 @@ SQL; * @param string $p_filepath path of file stored in Airtime. * @return Application_Model_StoredFile|NULL */ - public static function RecallByFilepath($p_filepath) + public static function RecallByFilepath($p_filepath, $con) { $path_info = Application_Model_MusicDir::splitFilePath($p_filepath); @@ -615,11 +628,11 @@ SQL; $file = CcFilesQuery::create() ->filterByDbDirectory($music_dir->getId()) ->filterByDbFilepath($path_info[1]) - ->findOne(); - return is_null($file) ? null : self::createWithFile($file); + ->findOne($con); + return is_null($file) ? null : self::createWithFile($file, $con); } - public static function RecallByPartialFilepath($partial_path) + public static function RecallByPartialFilepath($partial_path, $con) { $path_info = Application_Model_MusicDir::splitFilePath($partial_path); @@ -631,11 +644,10 @@ SQL; $files = CcFilesQuery::create() ->filterByDbDirectory($music_dir->getId()) ->filterByDbFilepath("$path_info[1]%") - ->find(); + ->find($con); $res = array(); foreach ($files as $file) { - $storedFile = new Application_Model_StoredFile(); - $storedFile->_file = $file; + $storedFile = new Application_Model_StoredFile($file, $con); $res[] = $storedFile; }