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) {
$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());
}
}
}

View File

@ -384,9 +384,11 @@ 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 {
//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
@ -396,15 +398,12 @@ SQL;
//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;
}
}
//} 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 {

View File

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

View File

@ -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");
}
}
}

View File

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

View File

@ -17,7 +17,8 @@ class CloudStorageUploader:
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)

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)