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 committed by Martin Konecny
parent 54116fe5aa
commit 9f1ec3c05c
2 changed files with 130 additions and 3 deletions

View file

@ -463,6 +463,100 @@ class ApiController extends Zend_Controller_Action
$this->view->watched_dirs = $watchedDirsPath; $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()
{
$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() public function reloadMetadataAction()
{ {
$request = $this->getRequest(); $request = $this->getRequest();

View file

@ -80,10 +80,10 @@ class AirTimeApiClient():
This function will query the server and download its response directly This function will query the server and download its response directly
into a temporary file. This is useful in the situation where the response into a temporary file. This is useful in the situation where the response
from the server can be huge and we don't want to store it into memory (potentially from the server can be huge and we don't want to store it into memory (potentially
causing Python to use hundreds of MB's of memory). By writing into a file we can causing Python to use hundreds of MB's of memory). By writing into a file we can
then open this file later, and read data a little bit at a time and be very mem then open this file later, and read data a little bit at a time and be very mem
efficient. efficient.
The return value of this function is the path of the temporary file. Unless specified using The return value of this function is the path of the temporary file. Unless specified using
block = False, this function will block until a successful HTTP 200 response is received. block = False, this function will block until a successful HTTP 200 response is received.
""" """
@ -362,6 +362,39 @@ class AirTimeApiClient():
return response 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: #returns a list of all db files for a given directory in JSON format:
#{"files":["path/to/file1", "path/to/file2"]} #{"files":["path/to/file1", "path/to/file2"]}
#Note that these are relative paths to the given directory. The full #Note that these are relative paths to the given directory. The full
@ -569,7 +602,7 @@ class AirTimeApiClient():
def get_files_without_replay_gain_value(self, dir_id): def get_files_without_replay_gain_value(self, dir_id):
""" """
Download a list of files that need to have their ReplayGain value calculated. This list Download a list of files that need to have their ReplayGain value calculated. This list
of files is downloaded into a file and the path to this file is the return value. of files is downloaded into a file and the path to this file is the return value.
""" """