created a new controller action for chunked requests from media monitor

This commit is contained in:
Rudi Grinberg 2012-07-13 14:51:43 -04:00
parent a4c322efac
commit a6d51bea8d
2 changed files with 130 additions and 3 deletions

View file

@ -488,6 +488,100 @@ class ApiController extends Zend_Controller_Action
$this->view->watched_dirs = $watchedDirsPath;
}
public function dispatchMetaDataAction($md, $mode)
{
// NOTE : if you are using this method. Make sure you've checked authorization
// in its caller because this method does not do checkAuth
// update import timestamp
Application_Model_Preference::SetImportTimestamp();
if ($mode == "create") {
$filepath = $md['MDATA_KEY_FILEPATH'];
$filepath = Application_Common_OsPath::normpath($filepath);
$file = Application_Model_StoredFile::RecallByFilepath($filepath);
if (is_null($file)) {
$file = Application_Model_StoredFile::Insert($md);
} else {
// path already exist
if ($file->getFileExistsFlag()) {
// file marked as exists
$this->view->error = "File already exists in Airtime.";
return;
} else {
// file marked as not exists
$file->setFileExistsFlag(true);
$file->setMetadata($md);
}
}
}
else if ($mode == "modify") {
$filepath = $md['MDATA_KEY_FILEPATH'];
$file = Application_Model_StoredFile::RecallByFilepath($filepath);
//File is not in database anymore.
if (is_null($file)) {
$this->view->error = "File does not exist in Airtime.";
return;
}
//Updating a metadata change.
else {
$file->setMetadata($md);
}
}
else if ($mode == "moved") {
$md5 = $md['MDATA_KEY_MD5'];
$file = Application_Model_StoredFile::RecallByMd5($md5);
if (is_null($file)) {
$this->view->error = "File doesn't exist in Airtime.";
return;
}
else {
$filepath = $md['MDATA_KEY_FILEPATH'];
//$filepath = str_replace("\\", "", $filepath);
$file->setFilePath($filepath);
}
}
else if ($mode == "delete") {
$filepath = $md['MDATA_KEY_FILEPATH'];
//$filepath = str_replace("\\", "", $filepath);
$file = Application_Model_StoredFile::RecallByFilepath($filepath);
if (is_null($file)) {
$this->view->error = "File doesn't exist in Airtime.";
return;
}
else {
$file->deleteByMediaMonitor();
}
}
else if ($mode == "delete_dir") {
$filepath = $md['MDATA_KEY_FILEPATH'];
//$filepath = str_replace("\\", "", $filepath);
$files = Application_Model_StoredFile::RecallByPartialFilepath($filepath);
foreach($files as $file){
$file->deleteByMediaMonitor();
}
return;
}
return $file->getId();
}
public function reloadMetadataGroupAction() {
$this->checkAuth();
$request = $this->getRequest();
//extract all file metadata params from the request.
//The value is a json encoded hash that has all the information related to this action
//The key does not have any meaning as of yet but it could potentially correspond
//to some unique id.
$responses = array();
foreach ($request->getRequest()->getParams() as $action => $info_json) {
$json = json_decode($info_json, $assoc=true);
array_push($responses, $this->dispatchMetaDataAction($info_json, $info_json['mode']));
}
// TODO : do something with $responses here instead of doing nothing
}
public function reloadMetadataAction() {
$this->checkAuth();

View file

@ -362,6 +362,39 @@ class AirTimeApiClient():
return response
def send_media_monitor_requests(self, md_list, mode, is_record=False):
logger = self.logger
response = None
try:
url = "http://%s:%s/%s/%s" % (self.config["base_url"], str(self.config["base_port"]), self.config["api_base"], self.config["update_media_url"])
url = url.replace("%%api_key%%", self.config["api_key"])
url = url.replace("%%mode%%", mode)
md_list = convert_dict_value_to_utf8(md_list)
data = urllib.urlencode(md_list)
req = urllib2.Request(url, data)
response = self.get_response_from_server(req)
logger.info("update media %s, filepath: %s, mode: %s", response, md_list['MDATA_KEY_FILEPATH'], mode)
response = json.loads(response)
if("error" not in response and is_record):
url = "http://%s:%s/%s/%s" % (self.config["base_url"], str(self.config["base_port"]), self.config["api_base"], self.config["upload_recorded"])
url = url.replace("%%fileid%%", str(response[u'id']))
url = url.replace("%%showinstanceid%%", str(md_list['MDATA_KEY_TRACKNUMBER']))
url = url.replace("%%api_key%%", self.config["api_key"])
response = self.get_response_from_server(url)
response = json.loads(response)
logger.info("associate recorded %s", response)
except Exception, e:
response = None
logger.error('Exception: %s', e)
logger.error("traceback: %s", traceback.format_exc())
return response
#returns a list of all db files for a given directory in JSON format:
#{"files":["path/to/file1", "path/to/file2"]}
#Note that these are relative paths to the given directory. The full