diff --git a/airtime_mvc/application/controllers/LibraryController.php b/airtime_mvc/application/controllers/LibraryController.php index bcb44e04d..96cbf6e89 100644 --- a/airtime_mvc/application/controllers/LibraryController.php +++ b/airtime_mvc/application/controllers/LibraryController.php @@ -351,7 +351,6 @@ class LibraryController extends Zend_Controller_Action foreach ($files as $id) { $file = Application_Model_StoredFile::RecallById($id); - if (isset($file)) { try { $res = $file->delete(); @@ -359,8 +358,9 @@ class LibraryController extends Zend_Controller_Action $message = $noPermissionMsg; } catch (Exception $e) { //could throw a scheduled in future exception. - $message = _("Could not delete some scheduled files."); + $message = _("Could not delete file(s)."); Logging::debug($e->getMessage()); + Logging::info($e->getMessage()); } } } diff --git a/airtime_mvc/application/models/StoredFile.php b/airtime_mvc/application/models/StoredFile.php index 3d3197621..74bb51d2d 100644 --- a/airtime_mvc/application/models/StoredFile.php +++ b/airtime_mvc/application/models/StoredFile.php @@ -384,27 +384,26 @@ SQL; $file_id = $this->_file->getDbId(); Logging::info("User ".$user->getLogin()." is deleting file: ".$this->_file->getDbTrackTitle()." - file id: ".$file_id); - $music_dir = Application_Model_MusicDir::getDirByPK($this->_file->getDbDirectory()); - if (!is_null($music_dir) && $music_dir->getType() == "stor" && file_exists($filepath)) { - try { - $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; - } - } + //try { + //Delete the physical file from either the local stor directory + //or from the cloud + $this->_file->deletePhysicalFile(); + + $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()); + + //delete the file record from cc_files (and cloud_file, if applicable) + $this->_file->delete(); + //} catch (Exception $e) { + //Logging::error($e->getMessage()); + //return; + //} } /* @@ -611,7 +610,8 @@ SQL; //Attempt to get the cloud file object and return it. If no cloud //file object is found then we are dealing with a regular stored //object so return that - $cloudFile = $storedFile->getCloudFiles()->getFirst(); + $cloudFile = CloudFileQuery::create()->findOneByCcFileId($p_id); + if (is_null($cloudFile)) { return self::createWithFile($storedFile, $con); } else { diff --git a/airtime_mvc/application/models/airtime/CcFiles.php b/airtime_mvc/application/models/airtime/CcFiles.php index ea761f473..2790de707 100644 --- a/airtime_mvc/application/models/airtime/CcFiles.php +++ b/airtime_mvc/application/models/airtime/CcFiles.php @@ -101,7 +101,12 @@ class CcFiles extends BaseCcFiles { public function deletePhysicalFile() { - unlink($this->getAbsoluteFilePath()); + $filepath = $this->getAbsoluteFilePath(); + if (file_exists($filepath)) { + unlink($filepath); + } else { + throw new Exception("Could not locate file ".$filepath); + } } } // CcFiles diff --git a/airtime_mvc/application/models/airtime/CloudFile.php b/airtime_mvc/application/models/airtime/CloudFile.php index 253b374d0..eab64c1ce 100644 --- a/airtime_mvc/application/models/airtime/CloudFile.php +++ b/airtime_mvc/application/models/airtime/CloudFile.php @@ -53,18 +53,27 @@ class CloudFile extends BaseCloudFile public function deletePhysicalFile() { - //TODO: execute a python script that deletes the file from the cloud + $CC_CONFIG = Config::getConfig(); + //$pathToScript = isset($_SERVER['AIRTIME_BASE']) ? $_SERVER['AIRTIME_BASE']."cloud_storage_deleter.py" : "/home/denise/airtime/cloud_storage_deleter.py"; - //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');*/ + $provider = escapeshellarg($CC_CONFIG["cloud_storage"]["provider"]); + $bucket = escapeshellarg($CC_CONFIG["cloud_storage"]["bucket"]); + $apiKey = escapeshellarg($CC_CONFIG["cloud_storage"]["api_key"]); + $apiSecret = escapeshellarg($CC_CONFIG["cloud_storage"]["api_key_secret"]); + $objName = $this->getResourceId(); + //we will pass the cloud storage bucket and api key info to the script + //instead of the script reading from a config file beacuse on saas we + //might store this info in the apache vhost + $command = '/usr/lib/airtime/pypo/bin/cloud_storage_deleter.py "' + .$provider.'" "' + .$bucket.'" "' + .$apiKey.'" "' + .$apiSecret.'" "' + .$this->getResourceId().'" 2>&1; echo $?'; + $output = shell_exec($command); + if ($output != "0") { + Logging::info($output); + throw new Exception("Could not delete file from cloud storage"); + } } } diff --git a/airtime_mvc/application/modules/rest/controllers/MediaController.php b/airtime_mvc/application/modules/rest/controllers/MediaController.php index 7c57b58d7..0408ff04e 100644 --- a/airtime_mvc/application/modules/rest/controllers/MediaController.php +++ b/airtime_mvc/application/modules/rest/controllers/MediaController.php @@ -288,7 +288,7 @@ class Rest_MediaController extends Zend_Rest_Controller $file = CcFilesQuery::create()->findPk($id); if ($file) { $con = Propel::getConnection(); - $storedFile = new Application_Model_StoredFile($file, $con); + $storedFile = Application_Model_StoredFile::RecallById($id, $con); $storedFile->delete(); //TODO: This checks your session permissions... Make it work without a session? $this->getResponse() diff --git a/python_apps/airtime_analyzer/airtime_analyzer/cloud_storage_uploader.py b/python_apps/airtime_analyzer/airtime_analyzer/cloud_storage_uploader.py index 3113d2937..d6b8bd9b6 100644 --- a/python_apps/airtime_analyzer/airtime_analyzer/cloud_storage_uploader.py +++ b/python_apps/airtime_analyzer/airtime_analyzer/cloud_storage_uploader.py @@ -16,8 +16,9 @@ class CloudStorageUploader: file_base_name = os.path.basename(audio_file_path) file_name, extension = os.path.splitext(file_base_name) object_name = "%s_%s%s" % (file_name, str(uuid.uuid4()), extension) - - driver = self.get_cloud_driver() + + cls = get_driver(getattr(Provider, self._provider)) + driver = cls(self._api_key, self._api_key_secret) try: container = driver.get_container(self._bucket) @@ -48,7 +49,8 @@ class CloudStorageUploader: return metadata def delete_obj(self, obj_name): - driver = self.get_cloud_driver() + cls = get_driver(getattr(Provider, self._provider)) + driver = cls(self._api_key, self._api_key_secret) try: cloud_obj = driver.get_object(container_name=self._bucket, @@ -59,6 +61,3 @@ class CloudStorageUploader: except ObjectDoesNotExistError: raise Exception("Could not find object on %s" % self._provider) - def get_cloud_driver(self): - cls = get_driver(getattr(Provider, self._provider)) - return cls(self._api_key, self._api_key_secret) diff --git a/python_apps/pypo/cloud_storage_deleter.py b/python_apps/pypo/cloud_storage_deleter.py new file mode 100755 index 000000000..6b6aef818 --- /dev/null +++ b/python_apps/pypo/cloud_storage_deleter.py @@ -0,0 +1,24 @@ +#!/usr/bin/python + +import sys + +from libcloud.storage.providers import get_driver +from libcloud.storage.types import Provider, ContainerDoesNotExistError, ObjectDoesNotExistError + +provider = str(sys.argv[0]) +bucket = str(sys.argv[1]) +api_key = str(sys.argv[2]) +api_key_secret = str(sys.argv[3]) +obj_name = str(sys.argv[4]) + +cls = get_driver(getattr(Provider, provider)) +driver = cls(api_key, api_key_secret) + +try: + cloud_obj = driver.get_object(container_name=bucket, + object_name=obj_name) + filesize = getattr(cloud_obj, 'size') + driver.delete_object(obj=cloud_obj) +except ObjectDoesNotExistError: + raise Exception("Could not find object on %s" % provider) +