Cleaner error handling

This commit is contained in:
Pierre Rudloff 2016-05-01 01:25:08 +02:00
parent 43804eebad
commit aba0243fd2
2 changed files with 116 additions and 118 deletions

View file

@ -115,135 +115,131 @@ class FrontController
if (isset($params["url"])) { if (isset($params["url"])) {
if (isset($params['audio'])) { if (isset($params['audio'])) {
try { try {
try { $url = $this->download->getURL($params["url"], 'bestaudio[protocol^=http]');
$url = $this->download->getURL($params["url"], 'bestaudio[protocol^=http]'); return $response->withRedirect($url);
return $response->withRedirect($url); } catch (\Exception $e) {
} catch (\Exception $e) { $video = $this->download->getJSON($params["url"], 'best');
$video = $this->download->getJSON($params["url"], 'best');
$avconvProc = ProcessBuilder::create( $avconvProc = ProcessBuilder::create(
array(
$this->config->avconv,
'-v', 'quiet',
'-i', '-',
'-f', 'mp3',
'-vn',
'pipe:1'
)
);
//Vimeo needs a correct user-agent
ini_set(
'user_agent',
$video->http_headers->{'User-Agent'}
);
$response = $response->withHeader(
'Content-Disposition',
'attachment; filename="'.
html_entity_decode(
pathinfo(
$video->_filename,
PATHINFO_FILENAME
).'.mp3',
ENT_COMPAT,
'ISO-8859-1'
).'"'
);
$response = $response->withHeader('Content-Type', 'audio/mpeg');
if (parse_url($video->url, PHP_URL_SCHEME) == 'rtmp') {
$builder = new ProcessBuilder(
array( array(
$this->config->avconv, $this->config->rtmpdump,
'-v', 'quiet', '-q',
'-i', '-', '-r',
'-f', 'mp3', $video->url,
'-vn', '--pageUrl', $video->webpage_url
'pipe:1'
) )
); );
if (isset($video->player_url)) {
//Vimeo needs a correct user-agent $builder->add('--swfVfy');
ini_set( $builder->add($video->player_url);
'user_agent',
$video->http_headers->{'User-Agent'}
);
$response = $response->withHeader(
'Content-Disposition',
'attachment; filename="'.
html_entity_decode(
pathinfo(
$video->_filename,
PATHINFO_FILENAME
).'.mp3',
ENT_COMPAT,
'ISO-8859-1'
).'"'
);
$response = $response->withHeader('Content-Type', 'audio/mpeg');
if (parse_url($video->url, PHP_URL_SCHEME) == 'rtmp') {
$builder = new ProcessBuilder(
array(
$this->config->rtmpdump,
'-q',
'-r',
$video->url,
'--pageUrl', $video->webpage_url
)
);
if (isset($video->player_url)) {
$builder->add('--swfVfy');
$builder->add($video->player_url);
}
if (isset($video->flash_version)) {
$builder->add('--flashVer');
$builder->add($video->flash_version);
}
if (isset($video->play_path)) {
$builder->add('--playpath');
$builder->add($video->play_path);
}
foreach ($video->rtmp_conn as $conn) {
$builder->add('--conn');
$builder->add($conn);
}
$chain = new Chain($builder->getProcess());
$chain->add('|', $avconvProc);
} else {
$chain = new Chain(
ProcessBuilder::create(
array_merge(
array(
'curl',
'--silent',
'--user-agent', $video->http_headers->{'User-Agent'},
$video->url
),
$this->config->curl_params
)
)
);
$chain->add('|', $avconvProc);
} }
if ($request->isGet()) { if (isset($video->flash_version)) {
$response = $response->withBody(new PopenStream($chain->getProcess()->getCommandLine())); $builder->add('--flashVer');
$builder->add($video->flash_version);
} }
return $response; if (isset($video->play_path)) {
$builder->add('--playpath');
$builder->add($video->play_path);
}
foreach ($video->rtmp_conn as $conn) {
$builder->add('--conn');
$builder->add($conn);
}
$chain = new Chain($builder->getProcess());
$chain->add('|', $avconvProc);
} else {
$chain = new Chain(
ProcessBuilder::create(
array_merge(
array(
'curl',
'--silent',
'--user-agent', $video->http_headers->{'User-Agent'},
$video->url
),
$this->config->curl_params
)
)
);
$chain->add('|', $avconvProc);
} }
} catch (\Exception $e) { if ($request->isGet()) {
$error = $e->getMessage(); $response = $response->withBody(new PopenStream($chain->getProcess()->getCommandLine()));
}
return $response;
} }
} else { } else {
try { $video = $this->download->getJSON($params["url"]);
$video = $this->download->getJSON($params["url"]); $container->view->render(
$container->view->render( $response,
$response, 'head.tpl',
'head.tpl', array(
array( 'class'=>'video'
'class'=>'video' )
) );
); $container->view->render(
$container->view->render( $response,
$response, 'video.tpl',
'video.tpl', array(
array( 'video'=>$video
'video'=>$video )
) );
); $container->view->render($response, 'footer.tpl');
$container->view->render($response, 'footer.tpl');
} catch (\Exception $e) {
$error = $e->getMessage();
}
} }
} }
if (isset($error)) { }
$container->view->render(
$response, public function error($request, $response, $exception)
'head.tpl', {
array( global $container;
'class'=>'video' $container->view->render(
) $response,
); 'head.tpl',
$container->view->render( array(
$response, 'class'=>'video'
'error.tpl', )
array( );
'errors'=>$error $container->view->render(
) $response,
); 'error.tpl',
$container->view->render($response, 'footer.tpl'); array(
} 'errors'=>$exception->getMessage()
)
);
$container->view->render($response, 'footer.tpl');
return $response;
} }
/** /**

View file

@ -30,6 +30,8 @@ $container['view'] = function ($c) {
$controller = new FrontController(); $controller = new FrontController();
$container['errorHandler'] = array($controller, 'error');
$app->get( $app->get(
'/', '/',
array($controller, 'index') array($controller, 'index')