CC-5709: Airtime Analyzer

* Added session auth to the Media API (if you're logged in)
* Started reworking our Plupload interaction with the server to be less
  awkward.
* Basic uploading works with the Add Media page again, but messages
  aren't dispatched to airtime_analyzer yet (coming next...)
This commit is contained in:
Albert Santoni 2014-03-13 11:14:30 -04:00
parent 6d7117f670
commit e6cbbdff33
5 changed files with 64 additions and 17 deletions

View File

@ -65,7 +65,7 @@ class LoginController extends Zend_Controller_Action
Application_Model_LoginAttempts::resetAttempts($_SERVER['REMOTE_ADDR']);
Application_Model_Subjects::resetLoginAttempts($username);
$tempSess = new Zend_Session_Namespace("referrer");
$tempSess->referrer = 'login';

View File

@ -6,8 +6,8 @@ class PluploadController extends Zend_Controller_Action
public function init()
{
$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->addActionContext('upload', 'json')
->addActionContext('copyfile', 'json')
$ajaxContext->addActionContext('upload', 'json')
->addActionContext('uploadFinished', 'json')
->initContext();
}

View File

@ -76,4 +76,12 @@ class Application_Model_RabbitMq
self::sendMessage($exchange, $data);
}
public static function SendMessageToAnalyzer()
{
$exchange = 'airtime-uploads';
//$data = json_encode($md);
//TODO: Finish me
//self::sendMessage($exchange, $data);
}
}

View File

@ -9,7 +9,7 @@ class Rest_MediaController extends Zend_Rest_Controller
public function indexAction()
{
if (!$this->verifyApiKey()) {
if (!$this->verifyApiKey() && !$this->verifySession()) {
return;
}
@ -32,7 +32,7 @@ class Rest_MediaController extends Zend_Rest_Controller
public function getAction()
{
if (!$this->verifyApiKey()) {
if (!$this->verifyApiKey() && !$this->verifySession()) {
return;
}
$id = $this->getId();
@ -42,6 +42,8 @@ class Rest_MediaController extends Zend_Rest_Controller
$file = CcFilesQuery::create()->findPk($id);
if ($file) {
//TODO: Strip or sanitize the JSON output
$this->getResponse()
->setHttpResponseCode(200)
->appendBody(json_encode($file->toArray(BasePeer::TYPE_FIELDNAME)));
@ -52,7 +54,7 @@ class Rest_MediaController extends Zend_Rest_Controller
public function postAction()
{
if (!$this->verifyApiKey()) {
if (!$this->verifyApiKey() && !$this->verifySession()) {
return;
}
//If we do get an ID on a POST, then that doesn't make any sense
@ -60,10 +62,13 @@ class Rest_MediaController extends Zend_Rest_Controller
if ($id = $this->_getParam('id', false)) {
$resp = $this->getResponse();
$resp->setHttpResponseCode(400);
$resp->appendBody("ERROR: ID should not be specified when using POST. POST is only used for show creation, and an ID will be chosen by Airtime");
$resp->appendBody("ERROR: ID should not be specified when using POST. POST is only used for file creation, and an ID will be chosen by Airtime");
return;
}
$this->processUpload();
//TODO: Strip or sanitize the JSON output
$file = new CcFiles();
$file->fromArray($this->getRequest()->getPost());
$file->save();
@ -75,7 +80,7 @@ class Rest_MediaController extends Zend_Rest_Controller
public function putAction()
{
if (!$this->verifyApiKey()) {
if (!$this->verifyApiKey() && !$this->verifySession()) {
return;
}
$id = $this->getId();
@ -86,6 +91,8 @@ class Rest_MediaController extends Zend_Rest_Controller
$file = CcFilesQuery::create()->findPk($id);
if ($file)
{
//TODO: Strip or sanitize the JSON output
$file->fromArray(json_decode($this->getRequest()->getRawBody(), true), BasePeer::TYPE_FIELDNAME);
$file->save();
$this->getResponse()
@ -98,7 +105,7 @@ class Rest_MediaController extends Zend_Rest_Controller
public function deleteAction()
{
if (!$this->verifyApiKey()) {
if (!$this->verifyApiKey() && !$this->verifySession()) {
return;
}
$id = $this->getId();
@ -107,6 +114,8 @@ class Rest_MediaController extends Zend_Rest_Controller
}
$file = CcFilesQuery::create()->findPk($id);
if ($file) {
$storedFile = Application_Model_StoredFile($file);
$storedFile->delete(); //TODO: This checks your session permissions... Make it work without a session?
$file->delete();
$this->getResponse()
->setHttpResponseCode(204);
@ -148,6 +157,20 @@ class Rest_MediaController extends Zend_Rest_Controller
return false;
}
}
private function verifySession()
{
$auth = Zend_Auth::getInstance();
if ($auth->hasIdentity())
{
return true;
}
//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.
//$auth = new Application_Model_Auth();
//$auth->checkToken(Application_Model_Preference::getUserId(), $token);
}
private function fileNotFoundResponse()
{
@ -155,4 +178,14 @@ class Rest_MediaController extends Zend_Rest_Controller
$resp->setHttpResponseCode(404);
$resp->appendBody("ERROR: Media not found.");
}
private function processUpload()
{
$upload_dir = ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "plupload";
$tempFilePath = Application_Model_StoredFile::uploadFile($upload_dir);
$tempFileName = basename($tempFilePath);
//TODO: Dispatch a message to airtime_analyzer through RabbitMQ!
}
}

View File

@ -5,8 +5,9 @@ $(document).ready(function() {
$("#plupload_files").pluploadQueue({
// General settings
runtimes : 'gears, html5, html4',
url : baseUrl+'Plupload/upload/format/json',
chunk_size : '5mb',
//url : baseUrl+'Plupload/upload/format/json',
url : baseUrl+'rest/media',
//chunk_size : '5mb', //Disabling chunking since we're using the File Upload REST API now
unique_names : 'true',
multiple_queues : 'true',
filters : [
@ -17,16 +18,21 @@ $(document).ready(function() {
uploader = $("#plupload_files").pluploadQueue();
uploader.bind('FileUploaded', function(up, file, json) {
/*
var j = jQuery.parseJSON(json.response);
if(j.error !== undefined) {
console.log(json.response);
if (j.error !== undefined) {
var row = $("<tr/>")
.append('<td>' + file.name +'</td>')
.append('<td>' + j.error.message + '</td>');
$("#plupload_error").find("table").append(row);
$("#plupload_error table").css("display", "inline-table");
}else{
} else {
//FIXME: This should just update something in the GUI, not communicate with the backend -- Albert
/*
var tempFileName = j.tempfilepath;
$.get(baseUrl+'Plupload/copyfile/format/json/name/'+
encodeURIComponent(file.name)+'/tempname/' +
@ -35,12 +41,12 @@ $(document).ready(function() {
var row = $("<tr/>")
.append('<td>' + file.name +'</td>')
.append('<td>' + jr.error.message + '</td>');
$("#plupload_error").find("table").append(row);
$("#plupload_error table").css("display", "inline-table");
}
});
}
}*/
});
var uploadProgress = false;