Merge branch 'cc-5709-airtime-analyzer' into cc-5709-airtime-analyzer-saas

This commit is contained in:
Albert Santoni 2014-04-02 16:48:04 -04:00
commit aabb8ec01f
2 changed files with 52 additions and 32 deletions

View File

@ -9,7 +9,6 @@ class Rest_MediaController extends Zend_Rest_Controller
'directory', 'directory',
'filepath', 'filepath',
'file_exists', 'file_exists',
'hidden',
'mtime', 'mtime',
'utime', 'utime',
'lptime', 'lptime',
@ -122,22 +121,30 @@ class Rest_MediaController extends Zend_Rest_Controller
} }
$file = new CcFiles(); $file = new CcFiles();
$file->fromArray($this->validateRequestData($this->getRequest()->getPost())); $whiteList = $this->removeBlacklistedFieldsFromRequestData($this->getRequest()->getPost());
$file->setDbOwnerId($this->getOwnerId());
$now = new DateTime("now", new DateTimeZone("UTC"));
$file->setDbTrackTitle($_FILES["file"]["name"]);
$file->setDbUtime($now);
$file->setDbMtime($now);
$file->setDbHidden(true);
$file->save();
$callbackUrl = $this->getRequest()->getScheme() . '://' . $this->getRequest()->getHttpHost() . $this->getRequest()->getRequestUri() . "/" . $file->getPrimaryKey();
$this->processUploadedFile($callbackUrl, $_FILES["file"]["name"], $this->getOwnerId()); if (!$this->validateRequestData($file, $whiteList)) {
$file->setDbTrackTitle($_FILES["file"]["name"]);
$file->setDbUtime(new DateTime("now", new DateTimeZone("UTC")));
$file->save();
return;
} else {
$this->getResponse() $file->fromArray($whiteList);
->setHttpResponseCode(201) $file->setDbOwnerId($this->getOwnerId());
->appendBody(json_encode($this->sanitizeResponse($file))); $now = new DateTime("now", new DateTimeZone("UTC"));
$file->setDbTrackTitle($_FILES["file"]["name"]);
$file->setDbUtime($now);
$file->save();
$callbackUrl = $this->getRequest()->getScheme() . '://' . $this->getRequest()->getHttpHost() . $this->getRequest()->getRequestUri() . "/" . $file->getPrimaryKey();
$this->processUploadedFile($callbackUrl, $_FILES["file"]["name"], $this->getOwnerId());
$this->getResponse()
->setHttpResponseCode(201)
->appendBody(json_encode($this->sanitizeResponse($file)));
}
} }
public function putAction() public function putAction()
@ -153,19 +160,15 @@ class Rest_MediaController extends Zend_Rest_Controller
} }
$file = CcFilesQuery::create()->findPk($id); $file = CcFilesQuery::create()->findPk($id);
//validate fields
$requestData = json_decode($this->getRequest()->getRawBody(), true);
//TODO: rename EditAudioMD form?
$fileForm = new Application_Form_EditAudioMD();
$fileForm->startForm($file->getDbId());
$fileForm->populate($requestData);
if (!$fileForm->isValidPartial($requestData)) { $requestData = json_decode($this->getRequest()->getRawBody(), true);
$file->setDbImportStatus(2)->save(); $whiteList = $this->removeBlacklistedFieldsFromRequestData($requestData);
$this->invalidDataResponse();
} else if ($file) if (!$this->validateRequestData($file, $whiteList)) {
{ $file->save();
$file->fromArray($this->validateRequestData($requestData), BasePeer::TYPE_FIELDNAME); return;
} else if ($file) {
$file->fromArray($whiteList, BasePeer::TYPE_FIELDNAME);
//Our RESTful API takes "full_path" as a field, which we then split and translate to match //Our RESTful API takes "full_path" as a field, which we then split and translate to match
//our internal schema. Internally, file path is stored relative to a directory, with the directory //our internal schema. Internally, file path is stored relative to a directory, with the directory
@ -307,7 +310,23 @@ class Rest_MediaController extends Zend_Rest_Controller
$resp->setHttpResponseCode(400); $resp->setHttpResponseCode(400);
$resp->appendBody("ERROR: Invalid data"); $resp->appendBody("ERROR: Invalid data");
} }
private function validateRequestData($file, $whiteList)
{
// EditAudioMD form is used here for validation
$fileForm = new Application_Form_EditAudioMD();
$fileForm->startForm($file->getDbId());
$fileForm->populate($whiteList);
if (!$fileForm->isValidPartial($whiteList)) {
$file->setDbImportStatus(2);
$file->setDbHidden(true);
$this->invalidDataResponse();
return false;
}
return true;
}
private function processUploadedFile($callbackUrl, $originalFilename, $ownerId) private function processUploadedFile($callbackUrl, $originalFilename, $ownerId)
{ {
$CC_CONFIG = Config::getConfig(); $CC_CONFIG = Config::getConfig();
@ -376,11 +395,11 @@ class Rest_MediaController extends Zend_Rest_Controller
* from outside of Airtime * from outside of Airtime
* @param array $data * @param array $data
*/ */
private function validateRequestData($data) private function removeBlacklistedFieldsFromRequestData($data)
{ {
foreach ($this->blackList as $key) { foreach ($this->blackList as $key) {
unset($data[$key]); unset($data[$key]);
} }
return $data; return $data;
} }

View File

@ -116,8 +116,9 @@ class MessageListener:
# TODO: If the JSON was invalid or the web server is down, # TODO: If the JSON was invalid or the web server is down,
# then don't report that failure to the REST API # then don't report that failure to the REST API
#TODO: Catch exceptions from this HTTP request too: #TODO: Catch exceptions from this HTTP request too:
StatusReporter.report_failure_to_callback_url(callback_url, api_key, import_status=2, if callback_url: # If we got an invalid message, there might be no callback_url in the JSON
reason=u'An error occurred while importing this file') StatusReporter.report_failure_to_callback_url(callback_url, api_key, import_status=2,
reason=u'An error occurred while importing this file')
else: else: