From 1df06f7c22cecab74fd63f7964b00816a500ab7e Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Mon, 22 Apr 2019 15:20:05 +0200 Subject: [PATCH] refactor: Use route names that make more sense --- controllers/FrontController.php | 33 ++++--- index.php | 22 +++-- templates/index.tpl | 6 +- templates/{video.tpl => info.tpl} | 4 +- templates/playlist.tpl | 6 +- tests/FrontControllerTest.php | 144 +++++++++++++++--------------- 6 files changed, 117 insertions(+), 98 deletions(-) rename templates/{video.tpl => info.tpl} (97%) diff --git a/controllers/FrontController.php b/controllers/FrontController.php index a7d5c3e..87ee5e8 100644 --- a/controllers/FrontController.php +++ b/controllers/FrontController.php @@ -274,7 +274,7 @@ class FrontController * * @return Response HTTP response */ - private function getVideoResponse(Request $request, Response $response) + private function getInfoResponse(Request $request, Response $response) { try { $this->video->getJson(); @@ -285,7 +285,7 @@ class FrontController 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; @@ -298,7 +298,7 @@ class FrontController $template, [ 'video' => $this->video, - 'class' => 'video', + 'class' => 'info', 'title' => $title, 'description' => $description, 'config' => $this->config, @@ -319,7 +319,7 @@ class FrontController * * @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'); @@ -332,9 +332,13 @@ class FrontController $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')); @@ -464,7 +468,7 @@ class FrontController } /** - * 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 @@ -472,7 +476,7 @@ class FrontController * * @return Response HTTP response */ - private function getRedirectResponse(Request $request, Response $response) + private function getDownloadResponse(Request $request, Response $response) { try { $videoUrls = $this->video->getUrl(); @@ -532,7 +536,7 @@ class FrontController * * @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'); @@ -541,14 +545,19 @@ class FrontController $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()); diff --git a/index.php b/index.php index 77f8a85..43f7e8b 100644 --- a/index.php +++ b/index.php @@ -40,26 +40,36 @@ $app->get( '/', [$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'] diff --git a/templates/index.tpl b/templates/index.tpl index 1f3c252..707c45d 100644 --- a/templates/index.tpl +++ b/templates/index.tpl @@ -3,7 +3,7 @@
-
+ @@ -14,7 +14,7 @@ required placeholder="http://example.com/video" /> {if $config->uglyUrls} - + {/if}
{if $config->convert} @@ -36,7 +36,7 @@ {t}See all supported websites{/t}

{t}Drag this to your bookmarks bar:{/t}

- {t}Bookmarklet{/t} + {t}Bookmarklet{/t}
diff --git a/templates/video.tpl b/templates/info.tpl similarity index 97% rename from templates/video.tpl rename to templates/info.tpl index b677ff0..ff977df 100644 --- a/templates/video.tpl +++ b/templates/info.tpl @@ -18,12 +18,12 @@ {/if}
- + {if isset($video->formats)}

{if $config->uglyUrls} - + {/if}