Refactor redirect() function

This commit is contained in:
Pierre Rudloff 2017-04-25 22:47:52 +02:00
parent cbcf58c5fb
commit df7bc9a088

View file

@ -351,8 +351,8 @@ class FrontController
/** /**
* Get a remuxed stream piped through the server. * Get a remuxed stream piped through the server.
* *
* @param string $urls URLs of the video and audio files * @param array $urls URLs of the video and audio files
* @param string $webpageUrl URL of the webpage containing the video * @param string $format Requested format
* @param Response $response PSR-7 response * @param Response $response PSR-7 response
* @param Request $request PSR-7 request * @param Request $request PSR-7 request
* *
@ -381,6 +381,56 @@ class FrontController
).'.mkv"'); ).'.mkv"');
} }
/**
* Get video format from request parameters or default format if none is specified
*
* @param Request $request PSR-7 request
*
* @return string format
*/
private function getFormat(Request $request)
{
$format = $request->getQueryParam('format');
if (!isset($format)) {
$format = $this->defaultFormat;
}
return $format;
}
/**
* Get approriate HTTP response to redirect query
* Depends on whether we want to stream, remux or simply redirect
*
* @param string $url URL of the video
* @param string $format Requested format
* @param Response $response PSR-7 response
* @param Request $request PSR-7 request
*
* @return Response HTTP response
*/
private function getRedirectResponse($url, $format, Response $response, Request $request)
{
$videoUrls = $this->download->getURL(
$url,
$format,
$this->sessionSegment->getFlash($url)
);
if (count($videoUrls) > 1) {
return $this->getRemuxStream($videoUrls, $format, $response, $request);
} elseif ($this->config->stream) {
return $this->getStream(
$url,
$format,
$response,
$request,
$this->sessionSegment->getFlash($url)
);
} else {
return $response->withRedirect($videoUrls[0]);
}
}
/** /**
* Redirect to video file. * Redirect to video file.
* *
@ -391,35 +441,14 @@ class FrontController
*/ */
public function redirect(Request $request, Response $response) public function redirect(Request $request, Response $response)
{ {
$params = $request->getQueryParams(); $url = $request->getQueryParam('url');
if (isset($params['format'])) { $format = $this->getFormat($request);
$format = $params['format']; if (isset($url)) {
} else {
$format = $this->defaultFormat;
}
if (isset($params['url'])) {
try { try {
$urls = $this->download->getURL( return $this->getRedirectResponse($url, $format, $response, $request);
$params['url'],
$format,
$this->sessionSegment->getFlash($params['url'])
);
if (count($urls) > 1) {
return $this->getRemuxStream($urls, $format, $response, $request);
} elseif ($this->config->stream) {
return $this->getStream(
$params['url'],
$format,
$response,
$request,
$this->sessionSegment->getFlash($params['url'])
);
} else {
return $response->withRedirect($urls[0]);
}
} catch (PasswordException $e) { } catch (PasswordException $e) {
return $response->withRedirect( return $response->withRedirect(
$this->container->get('router')->pathFor('video').'?url='.urlencode($params['url']) $this->container->get('router')->pathFor('video').'?url='.urlencode($url)
); );
} catch (\Exception $e) { } catch (\Exception $e) {
$response->getBody()->write($e->getMessage()); $response->getBody()->write($e->getMessage());