Refactor controller
This commit is contained in:
parent
929123c08e
commit
5ce84badec
1 changed files with 73 additions and 47 deletions
|
@ -143,6 +143,77 @@ class FrontController
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the converted MP3 file
|
||||||
|
* @param Request $request PSR-7 request
|
||||||
|
* @param Response $response PSR-7 response
|
||||||
|
* @param array $params GET query parameters
|
||||||
|
* @param string $password Video password
|
||||||
|
* @return Response
|
||||||
|
*/
|
||||||
|
private function getAudioResponse(Request $request, Response $response, array $params, $password = null)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
if ($this->config->stream) {
|
||||||
|
return $this->getStream($params['url'], 'mp3', $response, $request, $password);
|
||||||
|
} else {
|
||||||
|
$url = $this->download->getURL($params['url'], 'mp3[protocol^=http]', $password);
|
||||||
|
|
||||||
|
return $response->withRedirect($url);
|
||||||
|
}
|
||||||
|
} catch (PasswordException $e) {
|
||||||
|
return $this->password($request, $response);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$response = $response->withHeader(
|
||||||
|
'Content-Disposition',
|
||||||
|
'attachment; filename="'.
|
||||||
|
$this->download->getAudioFilename($params['url'], 'bestaudio/best', $password).'"'
|
||||||
|
);
|
||||||
|
$response = $response->withHeader('Content-Type', 'audio/mpeg');
|
||||||
|
|
||||||
|
if ($request->isGet() || $request->isPost()) {
|
||||||
|
$process = $this->download->getAudioStream($params['url'], 'bestaudio/best', $password);
|
||||||
|
$response = $response->withBody(new Stream($process));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the video description page
|
||||||
|
* @param Request $request PSR-7 request
|
||||||
|
* @param Response $response PSR-7 response
|
||||||
|
* @param array $params GET query parameters
|
||||||
|
* @param string $password Video password
|
||||||
|
* @return Response
|
||||||
|
*/
|
||||||
|
private function getVideoResponse(Request $request, Response $response, array $params, $password = null)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$video = $this->download->getJSON($params['url'], $this->defaultFormat, $password);
|
||||||
|
} catch (PasswordException $e) {
|
||||||
|
return $this->password($request, $response);
|
||||||
|
}
|
||||||
|
if ($this->config->stream) {
|
||||||
|
$protocol = '';
|
||||||
|
} else {
|
||||||
|
$protocol = '[protocol^=http]';
|
||||||
|
}
|
||||||
|
$this->view->render(
|
||||||
|
$response,
|
||||||
|
'video.tpl',
|
||||||
|
[
|
||||||
|
'video' => $video,
|
||||||
|
'class' => 'video',
|
||||||
|
'title' => $video->title,
|
||||||
|
'description' => 'Download "'.$video->title.'" from '.$video->extractor_key,
|
||||||
|
'protocol' => $protocol,
|
||||||
|
'config' => $this->config,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dislay information about the video.
|
* Dislay information about the video.
|
||||||
*
|
*
|
||||||
|
@ -160,54 +231,9 @@ class FrontController
|
||||||
$this->sessionSegment->setFlash($params['url'], $password);
|
$this->sessionSegment->setFlash($params['url'], $password);
|
||||||
}
|
}
|
||||||
if (isset($params['audio'])) {
|
if (isset($params['audio'])) {
|
||||||
try {
|
return $this->getAudioResponse($request, $response, $params, $password);
|
||||||
if ($this->config->stream) {
|
|
||||||
return $this->getStream($params['url'], 'mp3', $response, $request, $password);
|
|
||||||
} else {
|
|
||||||
$url = $this->download->getURL($params['url'], 'mp3[protocol^=http]', $password);
|
|
||||||
|
|
||||||
return $response->withRedirect($url);
|
|
||||||
}
|
|
||||||
} catch (PasswordException $e) {
|
|
||||||
return $this->password($request, $response);
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
$response = $response->withHeader(
|
|
||||||
'Content-Disposition',
|
|
||||||
'attachment; filename="'.
|
|
||||||
$this->download->getAudioFilename($params['url'], 'bestaudio/best', $password).'"'
|
|
||||||
);
|
|
||||||
$response = $response->withHeader('Content-Type', 'audio/mpeg');
|
|
||||||
|
|
||||||
if ($request->isGet() || $request->isPost()) {
|
|
||||||
$process = $this->download->getAudioStream($params['url'], 'bestaudio/best', $password);
|
|
||||||
$response = $response->withBody(new Stream($process));
|
|
||||||
}
|
|
||||||
|
|
||||||
return $response;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
try {
|
return $this->getVideoResponse($request, $response, $params, $password);
|
||||||
$video = $this->download->getJSON($params['url'], $this->defaultFormat, $password);
|
|
||||||
} catch (PasswordException $e) {
|
|
||||||
return $this->password($request, $response);
|
|
||||||
}
|
|
||||||
if ($this->config->stream) {
|
|
||||||
$protocol = '';
|
|
||||||
} else {
|
|
||||||
$protocol = '[protocol^=http]';
|
|
||||||
}
|
|
||||||
$this->view->render(
|
|
||||||
$response,
|
|
||||||
'video.tpl',
|
|
||||||
[
|
|
||||||
'video' => $video,
|
|
||||||
'class' => 'video',
|
|
||||||
'title' => $video->title,
|
|
||||||
'description' => 'Download "'.$video->title.'" from '.$video->extractor_key,
|
|
||||||
'protocol' => $protocol,
|
|
||||||
'config' => $this->config,
|
|
||||||
]
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return $response->withRedirect($this->container->get('router')->pathFor('index'));
|
return $response->withRedirect($this->container->get('router')->pathFor('index'));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue