From 8c2754972eff244e7ce02eecc396261cdc4077b4 Mon Sep 17 00:00:00 2001 From: drigato Date: Tue, 29 Jul 2014 15:07:51 -0400 Subject: [PATCH] CC-5896: Store cloud files in separate table, inherited from cc_files Refactored storedfile->delete() Added a deletePhysicalFile function to CcFile and CloudFile Cleaned up schedule events that get passed to Pypo --- airtime_mvc/application/models/Schedule.php | 17 +++--- airtime_mvc/application/models/StoredFile.php | 54 +++++++------------ .../application/models/airtime/CcFiles.php | 5 ++ .../application/models/airtime/CloudFile.php | 17 ++++++ python_apps/pypo/pypofile.py | 14 +---- 5 files changed, 53 insertions(+), 54 deletions(-) diff --git a/airtime_mvc/application/models/Schedule.php b/airtime_mvc/application/models/Schedule.php index e941f98bc..802125b5f 100644 --- a/airtime_mvc/application/models/Schedule.php +++ b/airtime_mvc/application/models/Schedule.php @@ -724,7 +724,7 @@ SQL; } } - private static function createFileScheduleEvent(&$data, $item, $media_id, $uri, $filesize, $object_name, $isInCloud) + private static function createFileScheduleEvent(&$data, $item, $media_id, $uri, $filesize, $object_name=null) { $start = self::AirtimeTimeToPypoTime($item["start"]); $end = self::AirtimeTimeToPypoTime($item["end"]); @@ -759,10 +759,11 @@ SQL; 'show_name' => $item["show_name"], 'replay_gain' => $replay_gain, 'independent_event' => $independent_event, - 'filesize' => $filesize, - 'object_name' => $object_name, - 'is_in_cloud' => $isInCloud + 'filesize' => $filesize ); + if (!is_null($object_name)) { + $schedule_item["object_name"] = $object_name; + } if ($schedule_item['cue_in'] > $schedule_item['cue_out']) { $schedule_item['cue_in'] = $schedule_item['cue_out']; @@ -895,10 +896,12 @@ SQL; $media_id = $item['file_id']; $storedFile = Application_Model_StoredFile::RecallById($media_id); $uri = $storedFile->getFilePath(); - $object_name = $storedFile->getResourceId(); + $object_name = null; + if ($storedFile->getPropelOrm() instanceof CloudFile) { + $object_name = $storedFile->getResourceId(); + } $filesize = $storedFile->getFileSize(); - $isInCloud = $storedFile->isInCloud(); - self::createFileScheduleEvent($data, $item, $media_id, $uri, $filesize, $object_name, $isInCloud); + self::createFileScheduleEvent($data, $item, $media_id, $uri, $filesize, $object_name); } elseif (!is_null($item['stream_id'])) { //row is type "webstream" diff --git a/airtime_mvc/application/models/StoredFile.php b/airtime_mvc/application/models/StoredFile.php index 85a9ef22f..3d3197621 100644 --- a/airtime_mvc/application/models/StoredFile.php +++ b/airtime_mvc/application/models/StoredFile.php @@ -387,44 +387,24 @@ SQL; $music_dir = Application_Model_MusicDir::getDirByPK($this->_file->getDbDirectory()); if (!is_null($music_dir) && $music_dir->getType() == "stor" && file_exists($filepath)) { try { - $this->doFileDeletionCleanup($this->getFileSize()); - unlink($filepath); + $filesize = $this->getFileSize(); + + //Update the user's disk usage + Application_Model_Preference::updateDiskUsage(-1 * $filesize); + + //Explicitly update any playlist's and block's length that contain + //the file getting deleted + self::updateBlockAndPlaylistLength($this->_file->getDbId()); + + $this->_file->deletePhysicalFile(); + + //delete the file record from cc_files (and cloud_file, if applicable) + $this->_file->delete(); } catch (Exception $e) { Logging::error($e->getMessage()); return; } - } /*elseif ($isInCloud) { - //Dispatch a message to airtime_analyzer through RabbitMQ, - //notifying it that we need to delete a file from the cloud - $CC_CONFIG = Config::getConfig(); - $apiKey = $CC_CONFIG["apiKey"][0]; - - //If the file was successfully deleted from the cloud the analyzer - //will make a request to the Media API to do the deletion cleanup. - $callbackUrl = 'http://'.$_SERVER['HTTP_HOST'].'/rest/media/'.$file_id.'/delete-success'; - - Application_Model_RabbitMq::SendDeleteMessageToAnalyzer( - $callbackUrl, $this->_file->getDbResourceId(), $apiKey, 'delete'); - }*/ - } - - /* - * This function handles all the actions required when a file is deleted - */ - public function doFileDeletionCleanup($filesize) - { - if ($filesize <= 0) { - throw new Exception ("Could not delete file with ".$filesize." filesize"); } - //Update the user's disk usage - Application_Model_Preference::updateDiskUsage(-1 * $filesize); - - //Explicitly update any playlist's and block's length that contains - //the file getting deleted - self::updateBlockAndPlaylistLength($this->_file->getDbId()); - - //delete the file record from cc_files - $this->_file->delete(); } /* @@ -574,12 +554,16 @@ SQL; public function getResourceId() { - return $this->_file->getDbResourceId(); + return $this->_file->getResourceId(); } public function getFileSize() { - return $this->_file->getFileSize(); + $filesize = $this->_file->getFileSize(); + if ($filesize <= 0) { + throw new Exception ("Could not determine filesize for file id: ".$this->_file->getDbId().". Filesize: ".$filesize); + } + return $filesize; } public static function Insert($md, $con) diff --git a/airtime_mvc/application/models/airtime/CcFiles.php b/airtime_mvc/application/models/airtime/CcFiles.php index f9456f262..ea761f473 100644 --- a/airtime_mvc/application/models/airtime/CcFiles.php +++ b/airtime_mvc/application/models/airtime/CcFiles.php @@ -99,4 +99,9 @@ class CcFiles extends BaseCcFiles { return is_file($this->getAbsoluteFilePath()); } + public function deletePhysicalFile() + { + unlink($this->getAbsoluteFilePath()); + } + } // CcFiles diff --git a/airtime_mvc/application/models/airtime/CloudFile.php b/airtime_mvc/application/models/airtime/CloudFile.php index c1780d11c..253b374d0 100644 --- a/airtime_mvc/application/models/airtime/CloudFile.php +++ b/airtime_mvc/application/models/airtime/CloudFile.php @@ -50,4 +50,21 @@ class CloudFile extends BaseCloudFile return false; } } + + public function deletePhysicalFile() + { + //TODO: execute a python script that deletes the file from the cloud + + //Dispatch a message to airtime_analyzer through RabbitMQ, + //notifying it that we need to delete a file from the cloud + /*$CC_CONFIG = Config::getConfig(); + $apiKey = $CC_CONFIG["apiKey"][0]; + + //If the file was successfully deleted from the cloud the analyzer + //will make a request to the Media API to do the deletion cleanup. + $callbackUrl = 'http://'.$_SERVER['HTTP_HOST'].'/rest/media/'.$file_id.'/delete-success'; + + Application_Model_RabbitMq::SendDeleteMessageToAnalyzer( + $callbackUrl, $this->_file->getDbResourceId(), $apiKey, 'delete');*/ + } } diff --git a/python_apps/pypo/pypofile.py b/python_apps/pypo/pypofile.py index 4292adf62..d6863883c 100644 --- a/python_apps/pypo/pypofile.py +++ b/python_apps/pypo/pypofile.py @@ -37,21 +37,11 @@ class PypoFile(Thread): """ src = media_item['uri'] dst = media_item['dst'] - is_in_cloud = media_item['is_in_cloud'] - - try: - if is_in_cloud: - src_size = media_item['filesize'] - else: - src_size = os.path.getsize(src) - except Exception, e: - self.logger.error("Could not get size of source file: %s", src) - return + src_size = media_item['filesize'] dst_exists = True try: dst_size = os.path.getsize(dst) - self.logger.debug(dst_size) except Exception, e: dst_exists = False @@ -73,7 +63,7 @@ class PypoFile(Thread): """ copy will overwrite dst if it already exists """ - if is_in_cloud: + if 'object_name' in media_item: csd = CloudStorageDownloader() csd.download_obj(dst, media_item['object_name']) else: