CC-5888: Handle file deletion if the file is stored in the cloud

Execute a python script that deletes a file from the cloud
This commit is contained in:
drigato 2014-07-31 23:11:49 -04:00
parent 8c2754972e
commit dd37ffbdd7
7 changed files with 81 additions and 44 deletions

View File

@ -351,7 +351,6 @@ class LibraryController extends Zend_Controller_Action
foreach ($files as $id) { foreach ($files as $id) {
$file = Application_Model_StoredFile::RecallById($id); $file = Application_Model_StoredFile::RecallById($id);
if (isset($file)) { if (isset($file)) {
try { try {
$res = $file->delete(); $res = $file->delete();
@ -359,8 +358,9 @@ class LibraryController extends Zend_Controller_Action
$message = $noPermissionMsg; $message = $noPermissionMsg;
} catch (Exception $e) { } catch (Exception $e) {
//could throw a scheduled in future exception. //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::debug($e->getMessage());
Logging::info($e->getMessage());
} }
} }
} }

View File

@ -384,27 +384,26 @@ SQL;
$file_id = $this->_file->getDbId(); $file_id = $this->_file->getDbId();
Logging::info("User ".$user->getLogin()." is deleting file: ".$this->_file->getDbTrackTitle()." - file id: ".$file_id); Logging::info("User ".$user->getLogin()." is deleting file: ".$this->_file->getDbTrackTitle()." - file id: ".$file_id);
$music_dir = Application_Model_MusicDir::getDirByPK($this->_file->getDbDirectory()); //try {
if (!is_null($music_dir) && $music_dir->getType() == "stor" && file_exists($filepath)) { //Delete the physical file from either the local stor directory
try { //or from the cloud
$filesize = $this->getFileSize(); $this->_file->deletePhysicalFile();
//Update the user's disk usage $filesize = $this->getFileSize();
Application_Model_Preference::updateDiskUsage(-1 * $filesize);
//Update the user's disk usage
//Explicitly update any playlist's and block's length that contain Application_Model_Preference::updateDiskUsage(-1 * $filesize);
//the file getting deleted
self::updateBlockAndPlaylistLength($this->_file->getDbId()); //Explicitly update any playlist's and block's length that contain
//the file getting deleted
$this->_file->deletePhysicalFile(); self::updateBlockAndPlaylistLength($this->_file->getDbId());
//delete the file record from cc_files (and cloud_file, if applicable) //delete the file record from cc_files (and cloud_file, if applicable)
$this->_file->delete(); $this->_file->delete();
} catch (Exception $e) { //} catch (Exception $e) {
Logging::error($e->getMessage()); //Logging::error($e->getMessage());
return; //return;
} //}
}
} }
/* /*
@ -611,7 +610,8 @@ SQL;
//Attempt to get the cloud file object and return it. If no cloud //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 //file object is found then we are dealing with a regular stored
//object so return that //object so return that
$cloudFile = $storedFile->getCloudFiles()->getFirst(); $cloudFile = CloudFileQuery::create()->findOneByCcFileId($p_id);
if (is_null($cloudFile)) { if (is_null($cloudFile)) {
return self::createWithFile($storedFile, $con); return self::createWithFile($storedFile, $con);
} else { } else {

View File

@ -101,7 +101,12 @@ class CcFiles extends BaseCcFiles {
public function deletePhysicalFile() 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 } // CcFiles

View File

@ -53,18 +53,27 @@ class CloudFile extends BaseCloudFile
public function deletePhysicalFile() 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, $provider = escapeshellarg($CC_CONFIG["cloud_storage"]["provider"]);
//notifying it that we need to delete a file from the cloud $bucket = escapeshellarg($CC_CONFIG["cloud_storage"]["bucket"]);
/*$CC_CONFIG = Config::getConfig(); $apiKey = escapeshellarg($CC_CONFIG["cloud_storage"]["api_key"]);
$apiKey = $CC_CONFIG["apiKey"][0]; $apiSecret = escapeshellarg($CC_CONFIG["cloud_storage"]["api_key_secret"]);
$objName = $this->getResourceId();
//If the file was successfully deleted from the cloud the analyzer //we will pass the cloud storage bucket and api key info to the script
//will make a request to the Media API to do the deletion cleanup. //instead of the script reading from a config file beacuse on saas we
$callbackUrl = 'http://'.$_SERVER['HTTP_HOST'].'/rest/media/'.$file_id.'/delete-success'; //might store this info in the apache vhost
$command = '/usr/lib/airtime/pypo/bin/cloud_storage_deleter.py "'
Application_Model_RabbitMq::SendDeleteMessageToAnalyzer( .$provider.'" "'
$callbackUrl, $this->_file->getDbResourceId(), $apiKey, 'delete');*/ .$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");
}
} }
} }

View File

@ -288,7 +288,7 @@ class Rest_MediaController extends Zend_Rest_Controller
$file = CcFilesQuery::create()->findPk($id); $file = CcFilesQuery::create()->findPk($id);
if ($file) { if ($file) {
$con = Propel::getConnection(); $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? $storedFile->delete(); //TODO: This checks your session permissions... Make it work without a session?
$this->getResponse() $this->getResponse()

View File

@ -16,8 +16,9 @@ class CloudStorageUploader:
file_base_name = os.path.basename(audio_file_path) file_base_name = os.path.basename(audio_file_path)
file_name, extension = os.path.splitext(file_base_name) file_name, extension = os.path.splitext(file_base_name)
object_name = "%s_%s%s" % (file_name, str(uuid.uuid4()), extension) 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: try:
container = driver.get_container(self._bucket) container = driver.get_container(self._bucket)
@ -48,7 +49,8 @@ class CloudStorageUploader:
return metadata return metadata
def delete_obj(self, obj_name): 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: try:
cloud_obj = driver.get_object(container_name=self._bucket, cloud_obj = driver.get_object(container_name=self._bucket,
@ -59,6 +61,3 @@ class CloudStorageUploader:
except ObjectDoesNotExistError: except ObjectDoesNotExistError:
raise Exception("Could not find object on %s" % self._provider) 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)

View File

@ -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)