diff --git a/airtime_mvc/application/controllers/LoginController.php b/airtime_mvc/application/controllers/LoginController.php index 4c58a6b57..ee4ced5e4 100644 --- a/airtime_mvc/application/controllers/LoginController.php +++ b/airtime_mvc/application/controllers/LoginController.php @@ -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'; diff --git a/airtime_mvc/application/controllers/PluploadController.php b/airtime_mvc/application/controllers/PluploadController.php index 3c4c1048c..c7f4f29d9 100644 --- a/airtime_mvc/application/controllers/PluploadController.php +++ b/airtime_mvc/application/controllers/PluploadController.php @@ -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(); } diff --git a/airtime_mvc/application/models/RabbitMq.php b/airtime_mvc/application/models/RabbitMq.php index 371fab0b9..d14d8249f 100644 --- a/airtime_mvc/application/models/RabbitMq.php +++ b/airtime_mvc/application/models/RabbitMq.php @@ -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); + } } diff --git a/airtime_mvc/application/modules/rest/controllers/MediaController.php b/airtime_mvc/application/modules/rest/controllers/MediaController.php index fd626c796..83269de35 100644 --- a/airtime_mvc/application/modules/rest/controllers/MediaController.php +++ b/airtime_mvc/application/modules/rest/controllers/MediaController.php @@ -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! + + } } \ No newline at end of file diff --git a/airtime_mvc/public/js/airtime/library/plupload.js b/airtime_mvc/public/js/airtime/library/plupload.js index 2d76b3383..2b27166c4 100644 --- a/airtime_mvc/public/js/airtime/library/plupload.js +++ b/airtime_mvc/public/js/airtime/library/plupload.js @@ -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 = $("") .append('' + file.name +'') .append('' + j.error.message + ''); - + $("#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 = $("") .append('' + file.name +'') .append('' + jr.error.message + ''); - + $("#plupload_error").find("table").append(row); $("#plupload_error table").css("display", "inline-table"); } }); - } + }*/ }); var uploadProgress = false;