CC-6046, CC-6045, CC-6047 - New SoundCloud implementation

This commit is contained in:
Duncan Sommerville 2015-06-03 16:57:17 -04:00
parent 51a3f19f43
commit b0b6e037ac
62 changed files with 4009 additions and 2491 deletions

View file

@ -717,11 +717,6 @@ class ApiController extends Zend_Controller_Action
// fields
$file->setMetadataValue('MDATA_KEY_CREATOR', "Airtime Show Recorder");
$file->setMetadataValue('MDATA_KEY_TRACKNUMBER', $show_instance_id);
if (!$showCanceled && Application_Model_Preference::GetAutoUploadRecordedShowToSoundcloud()) {
$id = $file->getId();
Application_Model_Soundcloud::uploadSoundcloud($id);
}
}
public function mediaMonitorSetupAction()

View file

@ -71,6 +71,7 @@ class ErrorController extends Zend_Controller_Action {
* 404 error - route or controller
*/
public function error404Action() {
Logging::info("404!");
$this->_helper->viewRenderer('error-404');
$this->getResponse()->setHttpResponseCode(404);
$this->view->message = _('Page not found.');

View file

@ -265,8 +265,9 @@ class LibraryController extends Zend_Controller_Action
}
}
//SOUNDCLOUD MENU OPTIONS
if ($type === "audioclip" && Application_Model_Preference::GetUploadToSoundcloudOption()) {
// SOUNDCLOUD MENU OPTION
$soundcloudService = new SoundcloudService();
if ($type === "audioclip" && $soundcloudService->hasAccessToken()) {
//create a menu separator
$menu["sep1"] = "-----------";
@ -274,20 +275,16 @@ class LibraryController extends Zend_Controller_Action
//create a sub menu for Soundcloud actions.
$menu["soundcloud"] = array("name" => _("Soundcloud"), "icon" => "soundcloud", "items" => array());
$scid = $file->getSoundCloudId();
if ($scid > 0) {
$url = $file->getSoundCloudLinkToFile();
$menu["soundcloud"]["items"]["view"] = array("name" => _("View on Soundcloud"), "icon" => "soundcloud", "url" => $url);
}
if (!is_null($scid)) {
$serviceId = $soundcloudService->getServiceId($id);
if (!is_null($file) && $serviceId != 0) {
$menu["soundcloud"]["items"]["view"] = array("name" => _("View on Soundcloud"), "icon" => "soundcloud", "url" => $baseUrl."soundcloud/view-on-sound-cloud/id/{$id}");
$text = _("Re-upload to SoundCloud");
} else {
$text = _("Upload to SoundCloud");
}
$menu["soundcloud"]["items"]["upload"] = array("name" => $text, "icon" => "soundcloud", "url" => $baseUrl."library/upload-file-soundcloud/id/{$id}");
// TODO: reimplement how this works
$menu["soundcloud"]["items"]["upload"] = array("name" => $text, "icon" => "soundcloud", "url" => $baseUrl."soundcloud/upload/id/{$id}");
}
if (empty($menu)) {
@ -525,33 +522,4 @@ class LibraryController extends Zend_Controller_Action
Logging::info($e->getMessage());
}
}
public function uploadFileSoundcloudAction()
{
$id = $this->_getParam('id');
Application_Model_Soundcloud::uploadSoundcloud($id);
// we should die with ui info
$this->_helper->json->sendJson(null);
}
public function getUploadToSoundcloudStatusAction()
{
$id = $this->_getParam('id');
$type = $this->_getParam('type');
if ($type == "show") {
$show_instance = new Application_Model_ShowInstance($id);
$this->view->sc_id = $show_instance->getSoundCloudFileId();
$file = $show_instance->getRecordedFile();
$this->view->error_code = $file->getSoundCloudErrorCode();
$this->view->error_msg = $file->getSoundCloudErrorMsg();
} elseif ($type == "file") {
$file = Application_Model_StoredFile::RecallById($id);
$this->view->sc_id = $file->getSoundCloudId();
$this->view->error_code = $file->getSoundCloudErrorCode();
$this->view->error_msg = $file->getSoundCloudErrorMsg();
} else {
Logging::warn("Trying to upload unknown type: $type with id: $id");
}
}
}

View file

@ -62,14 +62,9 @@ class PreferenceController extends Zend_Controller_Action
Application_Model_Preference::setTuneinPartnerKey($values["tunein_partner_key"]);
Application_Model_Preference::setTuneinPartnerId($values["tunein_partner_id"]);
/*Application_Model_Preference::SetUploadToSoundcloudOption($values["UploadToSoundcloudOption"]);
Application_Model_Preference::SetSoundCloudDownloadbleOption($values["SoundCloudDownloadbleOption"]);
Application_Model_Preference::SetSoundCloudUser($values["SoundCloudUser"]);
Application_Model_Preference::SetSoundCloudPassword($values["SoundCloudPassword"]);
Application_Model_Preference::SetSoundCloudTags($values["SoundCloudTags"]);
Application_Model_Preference::SetSoundCloudGenre($values["SoundCloudGenre"]);
Application_Model_Preference::SetSoundCloudTrackType($values["SoundCloudTrackType"]);
Application_Model_Preference::SetSoundCloudLicense($values["SoundCloudLicense"]);*/
// SoundCloud Preferences
Application_Model_Preference::setDefaultSoundCloudLicenseType($values["SoundCloudLicense"]);
Application_Model_Preference::setDefaultSoundCloudSharingType($values["SoundCloudSharing"]);
$this->view->statusMsg = "<div class='success'>". _("Preferences updated.")."</div>";
$this->view->form = $form;

View file

@ -253,25 +253,6 @@ class ScheduleController extends Zend_Controller_Action
$this->view->show_id = $showId;
}
public function uploadToSoundCloudAction()
{
$show_instance = $this->_getParam('id');
try {
$show_inst = new Application_Model_ShowInstance($show_instance);
} catch (Exception $e) {
$this->view->show_error = true;
return false;
}
$file = $show_inst->getRecordedFile();
$id = $file->getId();
Application_Model_Soundcloud::uploadSoundcloud($id);
// we should die with ui info
$this->_helper->json->sendJson(null);
}
public function makeContextMenuAction()
{
$instanceId = $this->_getParam('instanceId');

View file

@ -0,0 +1,78 @@
<?php
require_once "ThirdPartyController.php";
require_once "ise/php-soundcloud/src/Soundcloud/Service.php";
class SoundcloudController extends ThirdPartyController {
/**
* @var SoundcloudService
*/
private $_soundcloudService;
/**
* Set up SoundCloud access variables.
*/
public function init() {
parent::init();
$this->_soundcloudService = new SoundcloudService();
}
/**
* Send user to SoundCloud to authorize before being redirected
*/
public function authorizeAction() {
$auth_url = $this->_soundcloudService->getAuthorizeUrl();
header('Location: ' . $auth_url);
}
/**
* Called when user successfully completes SoundCloud authorization.
* Store the returned request token for future requests.
*/
public function redirectAction() {
$code = $_GET['code'];
$this->_soundcloudService->requestNewAccessToken($code);
header('Location: ' . $this->_baseUrl . 'Preference'); // Redirect back to the Preference page
}
/**
* Fetch the permalink to a file on SoundCloud and redirect to it.
*/
public function viewOnSoundCloudAction() {
$request = $this->getRequest();
$id = $request->getParam('id');
try {
$soundcloudLink = $this->_soundcloudService->getLinkToFile($id);
header('Location: ' . $soundcloudLink);
} catch (Soundcloud\Exception\InvalidHttpResponseCodeException $e) {
// If we end up here it means the track was removed from SoundCloud
// or the foreign id in our database is incorrect, so we should just
// get rid of the database record
Logging::warn("Error retrieving track data from SoundCloud: " . $e->getMessage());
$this->_soundcloudService->removeTrackReference($id);
// Redirect to a 404 so the user knows something went wrong
header('Location: ' . $this->_baseUrl . 'error/error-404'); // Redirect back to the Preference page
}
}
/**
* Upload the file with the given id to SoundCloud.
*
* @throws Zend_Controller_Response_Exception thrown if upload fails for any reason
*/
public function uploadAction() {
$request = $this->getRequest();
$id = $request->getParam('id');
$this->_soundcloudService->upload($id);
}
/**
* Clear the previously saved request token from the preferences.
*/
public function deauthorizeAction() {
Application_Model_Preference::setSoundCloudRequestToken("");
header('Location: ' . $this->_baseUrl . 'Preference'); // Redirect back to the Preference page
}
}

View file

@ -0,0 +1,55 @@
<?php
/**
* Class ThirdPartyController abstract superclass for third-party service authorization
*/
abstract class ThirdPartyController extends Zend_Controller_Action {
/**
* @var string base url and port for redirection
*/
protected $_baseUrl;
/**
* Disable controller rendering and initialize
*/
public function init() {
$CC_CONFIG = Config::getConfig();
$this->_baseUrl = 'http://' . $CC_CONFIG['baseUrl'] . ":" . $CC_CONFIG['basePort'] . "/";
$this->view->layout()->disableLayout(); // Don't inject the standard Now Playing header.
$this->_helper->viewRenderer->setNoRender(true); // Don't use (phtml) templates
}
/**
* Send user to a third-party service to authorize before being redirected
*
* @return void
*/
abstract function authorizeAction();
/**
* Called when user successfully completes third-party authorization.
* Store the returned request token for future requests.
*
* @return void
*/
abstract function redirectAction();
/**
* Upload the file with the given id to a third-party service.
*
* @return void
*
* @throws Zend_Controller_Response_Exception thrown if upload fails for any reason
*/
abstract function uploadAction();
/**
* Clear the previously saved request token from the preferences.
*
* @return void
*/
abstract function deauthorizeAction();
}

View file

@ -9,25 +9,21 @@ class UpgradeController extends Zend_Controller_Action
$this->view->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
if (!$this->verifyAuth()) {
if (!RestAuth::verifyAuth(true, false, $this)) {
return;
}
$upgraders = array();
array_push($upgraders, new AirtimeUpgrader253());
array_push($upgraders, new AirtimeUpgrader254());
array_push($upgraders, new AirtimeUpgrader255());
array_push($upgraders, new AirtimeUpgrader259());
array_push($upgraders, new AirtimeUpgrader2510());
array_push($upgraders, new AirtimeUpgrader2511());
array_push($upgraders, new AirtimeUpgrader2512());
// Get all upgrades dynamically so we don't have to add them explicitly each time
$upgraders = getUpgrades();
Logging::info($upgraders);
$didWePerformAnUpgrade = false;
try
{
for ($i = 0; $i < count($upgraders); $i++)
foreach ($upgraders as $upgrader)
{
$upgrader = $upgraders[$i];
/** @var $upgrader AirtimeUpgrader */
$upgrader = new $upgrader();
if ($upgrader->checkIfUpgradeSupported())
{
// pass __DIR__ to the upgrades, since __DIR__ returns parent dir of file, not executor
@ -36,7 +32,6 @@ class UpgradeController extends Zend_Controller_Action
$this->getResponse()
->setHttpResponseCode(200)
->appendBody("Upgrade to Airtime " . $upgrader->getNewVersion() . " OK<br>");
$i = 0; //Start over, in case the upgrade handlers are not in ascending order.
}
}
@ -54,28 +49,4 @@ class UpgradeController extends Zend_Controller_Action
->appendBody($e->getMessage());
}
}
private function verifyAuth()
{
//The API key is passed in via HTTP "basic authentication":
//http://en.wikipedia.org/wiki/Basic_access_authentication
$CC_CONFIG = Config::getConfig();
//Decode the API key that was passed to us in the HTTP request.
$authHeader = $this->getRequest()->getHeader("Authorization");
$encodedRequestApiKey = substr($authHeader, strlen("Basic "));
$encodedStoredApiKey = base64_encode($CC_CONFIG["apiKey"][0] . ":");
if ($encodedRequestApiKey !== $encodedStoredApiKey)
{
$this->getResponse()
->setHttpResponseCode(401)
->appendBody("Error: Incorrect API key.<br>");
return false;
}
return true;
}
}

View file

@ -0,0 +1,14 @@
CREATE TABLE IF NOT EXISTS "third_party_track_references"
(
"id" serial NOT NULL,
"service" VARCHAR(512) NOT NULL,
"foreign_id" INTEGER NOT NULL,
"file_id" INTEGER NOT NULL,
"status" VARCHAR(256) NOT NULL,
PRIMARY KEY ("id")
);
ALTER TABLE "third_party_track_references" ADD CONSTRAINT "track_reference_fkey"
FOREIGN KEY ("file_id")
REFERENCES "cc_files" ("id")
ON DELETE CASCADE;