SAAS-503: PYPO -> Use the REST API to download files

This commit is contained in:
drigato 2014-12-03 13:22:52 -05:00
parent e1f1807f5a
commit 16dc286420
3 changed files with 21 additions and 21 deletions

View File

@ -771,6 +771,9 @@ SQL;
* @param Array $item schedule info about one track * @param Array $item schedule info about one track
* @param Integer $media_id scheduled item's cc_files id * @param Integer $media_id scheduled item's cc_files id
* @param String $uri path to the scheduled item's physical location * @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) private static function createFileScheduleEvent(&$data, $item, $media_id, $uri, $downloadURL, $filesize)
{ {
@ -943,8 +946,9 @@ SQL;
$storedFile = Application_Model_StoredFile::RecallById($media_id); $storedFile = Application_Model_StoredFile::RecallById($media_id);
$file = $storedFile->getPropelOrm(); $file = $storedFile->getPropelOrm();
$uri = $file->getAbsoluteFilePath(); $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(); $filesize = $file->getFileSize();
self::createFileScheduleEvent($data, $item, $media_id, $uri, $downloadURL, $filesize); self::createFileScheduleEvent($data, $item, $media_id, $uri, $downloadURL, $filesize);

View File

@ -70,9 +70,10 @@ class Rest_MediaController extends Zend_Rest_Controller
$storedFile = new Application_Model_StoredFile($file, $con); $storedFile = new Application_Model_StoredFile($file, $con);
$baseUrl = Application_Common_OsPath::getBaseDir(); $baseUrl = Application_Common_OsPath::getBaseDir();
$CC_CONFIG = Config::getConfig();
$this->getResponse() $this->getResponse()
->setHttpResponseCode(200) ->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 { } else {
$this->fileNotFoundResponse(); $this->fileNotFoundResponse();
} }
@ -307,7 +308,6 @@ class Rest_MediaController extends Zend_Rest_Controller
$authHeader = $this->getRequest()->getHeader("Authorization"); $authHeader = $this->getRequest()->getHeader("Authorization");
$encodedRequestApiKey = substr($authHeader, strlen("Basic ")); $encodedRequestApiKey = substr($authHeader, strlen("Basic "));
$encodedStoredApiKey = base64_encode($CC_CONFIG["apiKey"][0] . ":"); $encodedStoredApiKey = base64_encode($CC_CONFIG["apiKey"][0] . ":");
if ($encodedRequestApiKey === $encodedStoredApiKey) if ($encodedRequestApiKey === $encodedStoredApiKey)
{ {
return true; return true;

View File

@ -2,15 +2,13 @@
from threading import Thread from threading import Thread
from Queue import Empty from Queue import Empty
from cloud_storage_downloader import CloudStorageDownloader
import logging import logging
import shutil import shutil
import os import os
import sys import sys
import stat import stat
import urllib2 import requests
import base64
import ConfigParser import ConfigParser
from std_err_override import LogWriter from std_err_override import LogWriter
@ -41,14 +39,7 @@ class PypoFile(Thread):
""" """
src = media_item['uri'] src = media_item['uri']
dst = media_item['dst'] 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'] src_size = media_item['filesize']
dst_exists = True dst_exists = True
@ -81,12 +72,17 @@ class PypoFile(Thread):
username = config.get(CONFIG_SECTION, 'api_key') username = config.get(CONFIG_SECTION, 'api_key')
url = media_item['download_url'] url = media_item['download_url']
""" with open(dst, "wb") as handle:
Make HTTP request here response = requests.get(url, auth=requests.auth.HTTPBasicAuth(username, ''), stream=True)
"""
if not response.ok:
with open(dst, "wb") as code: raise Exception("%s - Error occurred downloading file" % response.status_code)
code.write(file.read())
for chunk in response.iter_content(1024):
if not chunk:
break
handle.write(chunk)
#make file world readable #make file world readable
os.chmod(dst, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) os.chmod(dst, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)