diff --git a/airtime_mvc/application/amazon/Amazon_S3.php b/airtime_mvc/application/amazon/Amazon_S3.php new file mode 100644 index 000000000..dcf7011ca --- /dev/null +++ b/airtime_mvc/application/amazon/Amazon_S3.php @@ -0,0 +1,71 @@ +setBucket($CC_CONFIG['cloud_storage']['bucket']); + $this->setAccessKey($CC_CONFIG['cloud_storage']['api_key']); + $this->setSecretKey($CC_CONFIG['cloud_storage']['api_key_secret']); + $this->setZendServiceAmazonS3(); + } + + public function getZendServiceAmazonS3() + { + return $this->zendServiceAmazonS3; + } + + private function setZendServiceAmazonS3() + { + $this->zendServiceAmazonS3 = new Zend_Service_Amazon_S3( + $this->getAccessKey(), + $this->getSecretKey()); + } + + public function getBucket() + { + return $this->bucket; + } + + private function setBucket($bucket) + { + $this->bucket = $bucket; + } + + public function getAccessKey() + { + return $this->accessKey; + } + + private function setAccessKey($accessKey) + { + $this->accessKey = $accessKey; + } + + public function getSecretKey() + { + return $this->secretKey; + } + + private function setSecretKey($secretKey) + { + $this->secretKey = $secretKey; + } +} \ No newline at end of file diff --git a/airtime_mvc/application/models/StoredFile.php b/airtime_mvc/application/models/StoredFile.php index c3b72b3a2..3bf35899a 100644 --- a/airtime_mvc/application/models/StoredFile.php +++ b/airtime_mvc/application/models/StoredFile.php @@ -382,26 +382,21 @@ SQL; $file_id = $this->_file->getDbId(); Logging::info("User ".$user->getLogin()." is deleting file: ".$this->_file->getDbTrackTitle()." - file id: ".$file_id); - //try { - //Delete the physical file from either the local stor directory - //or from the cloud - // TODO: don't have deletePhysicalFile return the filesize. - // Instead, fetch that value before deleting the file. - $filesize = $this->_file->deletePhysicalFile(); + $filesize = $this->_file->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; - //} + //Delete the physical file from either the local stor directory + //or from the cloud + $this->_file->deletePhysicalFile(); + + //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(); } /* diff --git a/airtime_mvc/application/models/airtime/CcFiles.php b/airtime_mvc/application/models/airtime/CcFiles.php index e1d770bad..9d40aef66 100644 --- a/airtime_mvc/application/models/airtime/CcFiles.php +++ b/airtime_mvc/application/models/airtime/CcFiles.php @@ -102,8 +102,6 @@ class CcFiles extends BaseCcFiles { /** * * Deletes the file from the stor directory - * - * Returns the filesize of the deleted file */ public function deletePhysicalFile() { @@ -113,8 +111,6 @@ class CcFiles extends BaseCcFiles { } else { throw new Exception("Could not locate file ".$filepath); } - - return $this->getFileSize(); } } // CcFiles diff --git a/airtime_mvc/application/models/airtime/CloudFile.php b/airtime_mvc/application/models/airtime/CloudFile.php index b815d4ed3..414933e11 100644 --- a/airtime_mvc/application/models/airtime/CloudFile.php +++ b/airtime_mvc/application/models/airtime/CloudFile.php @@ -1,6 +1,6 @@ get_s3_signed_url( - $CC_CONFIG['cloud_storage']['api_key'], - $CC_CONFIG['cloud_storage']['api_key_secret'], - $CC_CONFIG['cloud_storage']['bucket']); + return $this->get_s3_signed_url(); } - private function get_s3_signed_url($s3_key, $s3_key_secret, $bucket) + private function get_s3_signed_url() { //should be longer than track length $expires = 120; $resource_id = $this->getResourceId(); + $amazon_s3 = new Amazon_S3(); + $s3_bucket = $amazon_s3->getBucket(); + $s3_secret_key = $amazon_s3->getSecretKey(); + $s3_access_key = $amazon_s3->getAccessKey(); + $zend_s3 = $amazon_s3->getZendServiceAmazonS3(); + $expires = time()+$expires; - $string_to_sign = utf8_encode("GET\n\n\n$expires\n/$bucket/$resource_id"); + $string_to_sign = utf8_encode("GET\n\n\n$expires\n/$s3_bucket/$resource_id"); // We need to urlencode the entire signature in case the hashed signature // has spaces. (NOTE: utf8_encode() does not work here because it turns // spaces into non-breaking spaces) - $signature = urlencode(base64_encode((hash_hmac("sha1", $string_to_sign, $s3_key_secret, true)))); + $signature = urlencode(base64_encode((hash_hmac("sha1", $string_to_sign, $s3_secret_key, true)))); - $authentication_params = "AWSAccessKeyId=$s3_key&Expires=$expires&Signature=$signature"; + $authentication_params = "AWSAccessKeyId=$s3_access_key&Expires=$expires&Signature=$signature"; - $s3 = new Zend_Service_Amazon_S3($s3_key, $s3_key_secret); - $endpoint = $s3->getEndpoint(); + $endpoint = $zend_s3->getEndpoint(); $scheme = $endpoint->getScheme(); $host = $endpoint->getHost(); - $url = "$scheme://$host/$bucket/".utf8_encode($resource_id)."?$authentication_params"; + $url = "$scheme://$host/$s3_bucket/".utf8_encode($resource_id)."?$authentication_params"; return $url; } public function getFileSize() { - return strlen(file_get_contents($this->getAbsoluteFilePath())); + $amazon_s3 = new Amazon_S3(); + + $zend_s3 = $amazon_s3->getZendServiceAmazonS3(); + $bucket = $amazon_s3->getBucket(); + $resource_id = $this->getResourceId(); + + $amz_resource = utf8_encode("$bucket/$resource_id"); + $amz_resource_info = $zend_s3->getInfo($amz_resource); + return $amz_resource_info["size"]; } public function getFilename() @@ -80,31 +90,21 @@ class CloudFile extends BaseCloudFile /** * * Deletes the file from Amazon S3 - * - * If the file was successfully deleted the filesize of that file is returned */ public function deletePhysicalFile() { - $CC_CONFIG = Config::getConfig(); - - $s3 = new Zend_Service_Amazon_S3( - $CC_CONFIG['cloud_storage']['api_key'], - $CC_CONFIG['cloud_storage']['api_key_secret']); - - $bucket = $CC_CONFIG['cloud_storage']['bucket']; + $amazon_s3 = new Amazon_S3(); + $zend_s3 = $amazon_s3->getZendServiceAmazonS3(); + $bucket = $amazon_s3->getBucket(); $resource_id = $this->getResourceId(); $amz_resource = utf8_encode("$bucket/$resource_id"); - if ($s3->isObjectAvailable($amz_resource)) { - $obj_info = $s3->getInfo($amz_resource); - $filesize = $obj_info["size"]; - + if ($zend_s3->isObjectAvailable($amz_resource)) { // removeObject() returns true even if the object was not deleted (bug?) // so that is not a good way to do error handling. isObjectAvailable() // does however return the correct value; We have to assume that if the // object is available the removeObject() function will work. - $s3->removeObject($amz_resource); - return $filesize; + $zend_s3->removeObject($amz_resource); } else { throw new Exception("ERROR: Could not locate object on Amazon S3"); } diff --git a/airtime_mvc/build/airtime.conf b/airtime_mvc/build/airtime.conf index 5ba766ad3..10963f552 100644 --- a/airtime_mvc/build/airtime.conf +++ b/airtime_mvc/build/airtime.conf @@ -31,7 +31,8 @@ monit_password = airtime connection_retries = 3 time_between_retries = 60 -[amazon] +[cloud_storage] +provider = S3 bucket = -access_key = -secret_key = \ No newline at end of file +api_key = +api_key_secret = \ No newline at end of file diff --git a/airtime_mvc/public/index.php b/airtime_mvc/public/index.php index 4340a3075..a20586e82 100644 --- a/airtime_mvc/public/index.php +++ b/airtime_mvc/public/index.php @@ -57,6 +57,9 @@ if (file_exists('/usr/share/php/libzend-framework-php')) { set_include_path('/usr/share/php/libzend-framework-php' . PATH_SEPARATOR . get_include_path()); } +//amazon directory +set_include_path(APPLICATION_PATH . '/amazon' . PATH_SEPARATOR . get_include_path()); + /** Zend_Application */ require_once 'Zend/Application.php'; $application = new Zend_Application(