Skip to content

Commit

Permalink
refactor: Use route names that make more sense
Browse files Browse the repository at this point in the history
  • Loading branch information
Rudloff committed Apr 22, 2019
1 parent 660f322 commit 1df06f7
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 98 deletions.
33 changes: 21 additions & 12 deletions controllers/FrontController.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ private function getAudioResponse(Request $request, Response $response)
*
* @return Response HTTP response
*/
private function getVideoResponse(Request $request, Response $response)
private function getInfoResponse(Request $request, Response $response)
{
try {
$this->video->getJson();
Expand All @@ -285,7 +285,7 @@ private function getVideoResponse(Request $request, Response $response)
if (isset($this->video->entries)) {
$template = 'playlist.tpl';
} else {
$template = 'video.tpl';
$template = 'info.tpl';
}
$title = _('Video download');
$description = _('Download video from ').$this->video->extractor_key;
Expand All @@ -298,7 +298,7 @@ private function getVideoResponse(Request $request, Response $response)
$template,
[
'video' => $this->video,
'class' => 'video',
'class' => 'info',
'title' => $title,
'description' => $description,
'config' => $this->config,
Expand All @@ -319,7 +319,7 @@ private function getVideoResponse(Request $request, Response $response)
*
* @return Response HTTP response
*/
public function video(Request $request, Response $response)
public function info(Request $request, Response $response)
{
$url = $request->getQueryParam('url') ?: $request->getQueryParam('v');

Expand All @@ -332,9 +332,13 @@ public function video(Request $request, Response $response)
$this->video = new Video($url, $this->defaultFormat, $password);

if ($this->config->convert && $request->getQueryParam('audio')) {
return $this->getAudioResponse($request, $response);
// We skip the info page and get directly to the download.
return $response->withRedirect(
$this->container->get('router')->pathFor('download').
'?'.http_build_query($request->getQueryParams())
);
} else {
return $this->getVideoResponse($request, $response);
return $this->getInfoResponse($request, $response);
}
} else {
return $response->withRedirect($this->container->get('router')->pathFor('index'));
Expand Down Expand Up @@ -464,15 +468,15 @@ private function getFormat(Request $request)
}

/**
* Get approriate HTTP response to redirect query
* Get approriate HTTP response to download query.
* Depends on whether we want to stream, remux or simply redirect.
*
* @param Response $response PSR-7 response
* @param Request $request PSR-7 request
*
* @return Response HTTP response
*/
private function getRedirectResponse(Request $request, Response $response)
private function getDownloadResponse(Request $request, Response $response)
{
try {
$videoUrls = $this->video->getUrl();
Expand Down Expand Up @@ -532,7 +536,7 @@ private function getConvertedResponse(Request $request, Response $response)
*
* @return Response HTTP response
*/
public function redirect(Request $request, Response $response)
public function download(Request $request, Response $response)
{
$format = $this->getFormat($request);
$url = $request->getQueryParam('url');
Expand All @@ -541,14 +545,19 @@ public function redirect(Request $request, Response $response)
$this->video = new Video($url, $format, $this->sessionSegment->getFlash($url));

try {
if ($this->config->convertAdvanced && !is_null($request->getQueryParam('customConvert'))) {
if ($this->config->convert && $request->getQueryParam('audio')) {
// Audio convert.
return $this->getAudioResponse($request, $response);
} elseif ($this->config->convertAdvanced && !is_null($request->getQueryParam('customConvert'))) {
// Advance convert.
return $this->getConvertedResponse($request, $response);
}

return $this->getRedirectResponse($request, $response);
// Regular download.
return $this->getDownloadResponse($request, $response);
} catch (PasswordException $e) {
return $response->withRedirect(
$this->container->get('router')->pathFor('video').'?url='.urlencode($url)
$this->container->get('router')->pathFor('info').'?url='.urlencode($url)
);
} catch (Exception $e) {
$response->getBody()->write($e->getMessage());
Expand Down
22 changes: 16 additions & 6 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,36 @@
'/',
[$controller, 'index']
)->setName('index');

$app->get(
'/extractors',
[$controller, 'extractors']
)->setName('extractors');

$app->any(
'/video',
[$controller, 'video']
)->setName('video');
'/info',
[$controller, 'info']
)->setName('info');
// Legacy route.
$app->any('/video', [$controller, 'info']);

$app->any(
'/watch',
[$controller, 'video']
);

$app->get(
'/redirect',
[$controller, 'redirect']
)->setName('redirect');
'/download',
[$controller, 'download']
)->setName('download');
// Legacy route.
$app->get('/redirect', [$controller, 'download']);

$app->get(
'/json',
[$controller, 'json']
)->setName('json');

$app->get(
'/locale/{locale}',
[$controller, 'locale']
Expand Down
6 changes: 3 additions & 3 deletions templates/index.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<main class="main">
<div><img class="logo" src="{base_url}/img/logo.png"
alt="{$config->appName}" width="328" height="284"></div>
<form action="{path_for name="video"}">
<form action="{path_for name="info"}">
<label class="labelurl" for="url">
{t}Copy here the URL of your video (Youtube, Dailymotion, etc.){/t}
</label>
Expand All @@ -14,7 +14,7 @@
required placeholder="http://example.com/video" />
</span>
{if $config->uglyUrls}
<input type="hidden" name="page" value="video" />
<input type="hidden" name="page" value="info" />
{/if}
<input class="downloadBtn large-font" type="submit" value="{t}Download{/t}" /><br/>
{if $config->convert}
Expand All @@ -36,7 +36,7 @@
<a class="combatiblelink small-font" href="{path_for name="extractors"}">{t}See all supported websites{/t}</a>
<div id="bookmarklet" class="bookmarklet_wrapper">
<p> {t}Drag this to your bookmarks bar:{/t} </p>
<a class="bookmarklet small-font" href="javascript:window.location='{$domain}{path_for name='video'}?url='+encodeURIComponent(location.href);">{t}Bookmarklet{/t}</a>
<a class="bookmarklet small-font" href="javascript:window.location='{$domain}{path_for name='info'}?url='+encodeURIComponent(location.href);">{t}Bookmarklet{/t}</a>
</div>

</main>
Expand Down
4 changes: 2 additions & 2 deletions templates/video.tpl → templates/info.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
<meta itemprop="uploadDate" content="{$video->upload_date}" />
{/if}
<br/>
<form action="{path_for name="redirect"}">
<form action="{path_for name="download"}">
<input type="hidden" name="url" value="{$video->webpage_url}" />
{if isset($video->formats)}
<h3><label for="format">{t}Available formats:{/t}</label></h3>
{if $config->uglyUrls}
<input type="hidden" name="page" value="redirect" />
<input type="hidden" name="page" value="download" />
{/if}
<select name="format" id="format" class="formats monospace">
<optgroup label="{t}Generic formats{/t}">
Expand Down
6 changes: 3 additions & 3 deletions templates/playlist.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{$video->title}</a></i>{/if}{t}:{/t}
</p>
{if $config->stream}
<a href="{path_for name="redirect"}?url={$video->webpage_url}" class="downloadBtn">Download everything</a>
<a href="{path_for name="download"}?url={$video->webpage_url}" class="downloadBtn">Download everything</a>
{/if}
{foreach $video->entries as $entry}
<div class="playlist-entry">
Expand All @@ -27,8 +27,8 @@
{$entry->title}
{/if}
</a></h3>
<a target="_blank" class="downloadBtn" href="{path_for name="redirect"}?url={$entry->url}">{t}Download{/t}</a>
<a target="_blank" href="{path_for name="video"}?url={$entry->url}">{t}More options{/t}</a>
<a target="_blank" class="downloadBtn" href="{path_for name="download"}?url={$entry->url}">{t}Download{/t}</a>
<a target="_blank" href="{path_for name="info"}?url={$entry->url}">{t}More options{/t}</a>
</div>
{/foreach}

Expand Down
Loading

0 comments on commit 1df06f7

Please sign in to comment.