diff --git a/airtime_mvc/application/models/Schedule.php b/airtime_mvc/application/models/Schedule.php index 339464906..269b70bc7 100644 --- a/airtime_mvc/application/models/Schedule.php +++ b/airtime_mvc/application/models/Schedule.php @@ -771,6 +771,9 @@ SQL; * @param Array $item schedule info about one track * @param Integer $media_id scheduled item's cc_files id * @param String $uri path to the scheduled item's physical location + * @param String $downloadURL URL PYPO makes to the REST API to download the file for playout + * @param Integer $filsize The file's file size in bytes + * */ private static function createFileScheduleEvent(&$data, $item, $media_id, $uri, $downloadURL, $filesize) { @@ -943,8 +946,9 @@ SQL; $storedFile = Application_Model_StoredFile::RecallById($media_id); $file = $storedFile->getPropelOrm(); $uri = $file->getAbsoluteFilePath(); - // TODO: fix this URL - $downloadURL = "http://localhost/rest/media/$media_id/download"; + + $baseUrl = Application_Common_OsPath::getBaseDir(); + $downloadURL = "http://".$_SERVER['HTTP_HOST'].$baseUrl."rest/media/$media_id/download"; $filesize = $file->getFileSize(); self::createFileScheduleEvent($data, $item, $media_id, $uri, $downloadURL, $filesize); diff --git a/airtime_mvc/application/modules/rest/controllers/MediaController.php b/airtime_mvc/application/modules/rest/controllers/MediaController.php index 6563e857a..8064c7f08 100644 --- a/airtime_mvc/application/modules/rest/controllers/MediaController.php +++ b/airtime_mvc/application/modules/rest/controllers/MediaController.php @@ -70,9 +70,10 @@ class Rest_MediaController extends Zend_Rest_Controller $storedFile = new Application_Model_StoredFile($file, $con); $baseUrl = Application_Common_OsPath::getBaseDir(); + $CC_CONFIG = Config::getConfig(); $this->getResponse() ->setHttpResponseCode(200) - ->appendBody($this->_redirect($storedFile->getRelativeFileUrl($baseUrl).'/download/true')); + ->appendBody($this->_redirect($storedFile->getRelativeFileUrl($baseUrl).'/download/true/api_key/'.$CC_CONFIG["apiKey"][0])); } else { $this->fileNotFoundResponse(); } @@ -307,7 +308,6 @@ class Rest_MediaController extends Zend_Rest_Controller $authHeader = $this->getRequest()->getHeader("Authorization"); $encodedRequestApiKey = substr($authHeader, strlen("Basic ")); $encodedStoredApiKey = base64_encode($CC_CONFIG["apiKey"][0] . ":"); - if ($encodedRequestApiKey === $encodedStoredApiKey) { return true; diff --git a/python_apps/pypo/pypofile.py b/python_apps/pypo/pypofile.py index c6caca342..998c7bd26 100644 --- a/python_apps/pypo/pypofile.py +++ b/python_apps/pypo/pypofile.py @@ -2,15 +2,13 @@ from threading import Thread from Queue import Empty -from cloud_storage_downloader import CloudStorageDownloader import logging import shutil import os import sys import stat -import urllib2 -import base64 +import requests import ConfigParser from std_err_override import LogWriter @@ -41,14 +39,7 @@ class PypoFile(Thread): """ src = media_item['uri'] dst = media_item['dst'] - - """ - try: - src_size = os.path.getsize(src) - except Exception, e: - self.logger.error("Could not get size of source file: %s", src) - return - """ + src_size = media_item['filesize'] dst_exists = True @@ -81,12 +72,17 @@ class PypoFile(Thread): username = config.get(CONFIG_SECTION, 'api_key') url = media_item['download_url'] - """ - Make HTTP request here - """ - - with open(dst, "wb") as code: - code.write(file.read()) + with open(dst, "wb") as handle: + response = requests.get(url, auth=requests.auth.HTTPBasicAuth(username, ''), stream=True) + + if not response.ok: + raise Exception("%s - Error occurred downloading file" % response.status_code) + + for chunk in response.iter_content(1024): + if not chunk: + break + + handle.write(chunk) #make file world readable os.chmod(dst, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)