CC-5709: Airtime Analyzer
* Fixed error in media API authentication * Improved documentation
This commit is contained in:
parent
13a664207f
commit
16c56e6aff
|
@ -1,5 +1,6 @@
|
|||
<?php
|
||||
|
||||
|
||||
class Rest_MediaController extends Zend_Rest_Controller
|
||||
{
|
||||
//fields that are not modifiable via our RESTful API
|
||||
|
@ -29,7 +30,8 @@ class Rest_MediaController extends Zend_Rest_Controller
|
|||
|
||||
public function indexAction()
|
||||
{
|
||||
if (!$this->verifyApiKey() && !$this->verifySession()) {
|
||||
if (!$this->verifyAuth(true, true))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -52,9 +54,11 @@ class Rest_MediaController extends Zend_Rest_Controller
|
|||
|
||||
public function getAction()
|
||||
{
|
||||
if (!$this->verifyApiKey() && !$this->verifySession()) {
|
||||
if (!$this->verifyAuth(true, true))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
$id = $this->getId();
|
||||
if (!$id) {
|
||||
return;
|
||||
|
@ -73,9 +77,11 @@ class Rest_MediaController extends Zend_Rest_Controller
|
|||
|
||||
public function postAction()
|
||||
{
|
||||
if (!$this->verifyApiKey() && !$this->verifySession()) {
|
||||
if (!$this->verifyAuth(true, true))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//If we do get an ID on a POST, then that doesn't make any sense
|
||||
//since POST is only for creating.
|
||||
if ($id = $this->_getParam('id', false)) {
|
||||
|
@ -104,9 +110,11 @@ class Rest_MediaController extends Zend_Rest_Controller
|
|||
|
||||
public function putAction()
|
||||
{
|
||||
if (!$this->verifyApiKey() && !$this->verifySession()) {
|
||||
if (!$this->verifyAuth(true, true))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
$id = $this->getId();
|
||||
if (!$id) {
|
||||
return;
|
||||
|
@ -150,9 +158,11 @@ class Rest_MediaController extends Zend_Rest_Controller
|
|||
|
||||
public function deleteAction()
|
||||
{
|
||||
if (!$this->verifyApiKey() && !$this->verifySession()) {
|
||||
if (!$this->verifyAuth(true, true))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
$id = $this->getId();
|
||||
if (!$id) {
|
||||
return;
|
||||
|
@ -180,6 +190,27 @@ class Rest_MediaController extends Zend_Rest_Controller
|
|||
return $id;
|
||||
}
|
||||
|
||||
private function verifyAuth($checkApiKey, $checkSession)
|
||||
{
|
||||
//Session takes precedence over API key for now:
|
||||
if ($checkSession && $this->verifySession())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($checkApiKey && $this->verifyAPIKey())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
$resp = $this->getResponse();
|
||||
$resp->setHttpResponseCode(401);
|
||||
$resp->appendBody("ERROR: Incorrect API key.");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
private function verifyAPIKey()
|
||||
{
|
||||
//The API key is passed in via HTTP "basic authentication":
|
||||
|
@ -196,11 +227,10 @@ class Rest_MediaController extends Zend_Rest_Controller
|
|||
{
|
||||
return true;
|
||||
} else {
|
||||
$resp = $this->getResponse();
|
||||
$resp->setHttpResponseCode(401);
|
||||
$resp->appendBody("ERROR: Incorrect API key.");
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function verifySession()
|
||||
|
@ -210,6 +240,7 @@ class Rest_MediaController extends Zend_Rest_Controller
|
|||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
//Token checking stub code. We'd need to change LoginController.php to generate a token too, but
|
||||
//but luckily all the token code already exists and works.
|
||||
|
|
|
@ -12,6 +12,15 @@ You will need to allow the "airtime" RabbitMQ user to access all exchanges and q
|
|||
Usage
|
||||
==========
|
||||
|
||||
This program must run as a user with permissions to write to your Airtime music library
|
||||
directory. For standard Airtime installations, run it as the www-data user:
|
||||
|
||||
$ sudo -u www-data airtime_analyzer --debug
|
||||
|
||||
Or during development, add the --debug flag for more verbose output:
|
||||
|
||||
$ sudo -u www-data airtime_analyzer --debug
|
||||
|
||||
To print usage instructions, run:
|
||||
|
||||
$ airtime_analyzer --help
|
||||
|
@ -35,6 +44,8 @@ For example, run:
|
|||
|
||||
$ php tools/message_sender.php '{ "tmp_file_path" : "foo.mp3", "final_directory" : ".", "callback_url" : "http://airtime.localhost/rest/media/1", "api_key" : "YOUR_API_KEY" }'
|
||||
|
||||
$ php tools/message_sender.php '{"tmp_file_path":"foo.mp3", "import_directory":"/srv/airtime/stor/imported/1","original_filename":"foo.mp3","callback_url": "http://airtime.localhost/rest/media/1", "api_key":"YOUR_API_KEY"}'
|
||||
|
||||
Logging
|
||||
=========
|
||||
|
||||
|
|
|
@ -112,12 +112,13 @@ class MessageListener:
|
|||
|
||||
# TODO: Report this as a failed upload to the File Upload REST API.
|
||||
#
|
||||
# TODO: If the JSON was invalid, then don't report to the REST API
|
||||
# TODO: If the JSON was invalid or the web server is down,
|
||||
# then don't report that failure to the REST API
|
||||
|
||||
StatusReporter.report_failure_to_callback_url(callback_url, api_key, error_status=1,
|
||||
reason=u'An error occurred while importing this file')
|
||||
|
||||
logging.error(e)
|
||||
logging.exception(e)
|
||||
|
||||
else:
|
||||
# ACK at the very end, after the message has been successfully processed.
|
||||
|
|
|
@ -19,9 +19,9 @@ class StatusReporter():
|
|||
timeout=StatusReporter._HTTP_REQUEST_TIMEOUT)
|
||||
logging.debug("HTTP request returned status: " + str(r.status_code))
|
||||
logging.debug(r.text) # Log the response body
|
||||
r.raise_for_status() # Raise an exception if there was an HTTP error code returned
|
||||
|
||||
#TODO: Queue up failed requests and try them again later.
|
||||
r.raise_for_status() # Raise an exception if there was an HTTP error code returned
|
||||
|
||||
@classmethod
|
||||
def report_failure_to_callback_url(self, callback_url, api_key, error_status, reason):
|
||||
|
|
Loading…
Reference in New Issue