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 @@
-
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 $config->stream}
- webpage_url}" class="downloadBtn">Download everything
+ webpage_url}" class="downloadBtn">Download everything
{/if}
{foreach $video->entries as $entry}
{/foreach}
diff --git a/tests/FrontControllerTest.php b/tests/FrontControllerTest.php
index 463722a..07e1c5e 100644
--- a/tests/FrontControllerTest.php
+++ b/tests/FrontControllerTest.php
@@ -65,12 +65,12 @@ class FrontControllerTest extends BaseTest
$this->container['router']->map(['GET'], '/', [$this->controller, 'index'])
->setName('index');
- $this->container['router']->map(['GET'], '/video', [$this->controller, 'video'])
- ->setName('video');
+ $this->container['router']->map(['GET'], '/video', [$this->controller, 'info'])
+ ->setName('info');
$this->container['router']->map(['GET'], '/extractors', [$this->controller, 'extractors'])
->setName('extractors');
- $this->container['router']->map(['GET'], '/redirect', [$this->controller, 'redirect'])
- ->setName('redirect');
+ $this->container['router']->map(['GET'], '/redirect', [$this->controller, 'download'])
+ ->setName('download');
$this->container['router']->map(['GET'], '/locale', [$this->controller, 'locale'])
->setName('locale');
}
@@ -211,43 +211,43 @@ class FrontControllerTest extends BaseTest
}
/**
- * Test the video() function without the url parameter.
+ * Test the info() function without the url parameter.
*
* @return void
*/
- public function testVideoWithoutUrl()
+ public function testInfoWithoutUrl()
{
- $this->assertRequestIsRedirect('video');
+ $this->assertRequestIsRedirect('info');
}
/**
- * Test the video() function.
+ * Test the info() function.
*
* @return void
*/
- public function testVideo()
+ public function testInfo()
{
- $this->assertRequestIsOk('video', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU']);
+ $this->assertRequestIsOk('info', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU']);
}
/**
- * Test the video() function with audio conversion.
+ * Test the info() function with audio conversion.
*
* @return void
*/
- public function testVideoWithAudio()
+ public function testInfoWithAudio()
{
Config::setOptions(['convert' => true]);
- $this->assertRequestIsOk('video', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'audio' => true]);
+ $this->assertRequestIsRedirect('info', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'audio' => true]);
}
/**
- * Test the video() function with audio conversion from a Vimeo video.
+ * Test the info() function with audio conversion from a Vimeo video.
*
* @return void
*/
- public function testVideoWithVimeoAudio()
+ public function testInfoWithVimeoAudio()
{
if (getenv('CI')) {
$this->markTestSkipped('Travis is blacklisted by Vimeo.');
@@ -255,20 +255,20 @@ class FrontControllerTest extends BaseTest
Config::setOptions(['convert' => true]);
// So we can test the fallback to default format
- $this->assertRequestIsOk('video', ['url' => 'https://vimeo.com/251997032', 'audio' => true]);
+ $this->assertRequestIsRedirect('info', ['url' => 'https://vimeo.com/251997032', 'audio' => true]);
}
/**
- * Test the video() function with audio enabled and an URL that doesn't need to be converted.
+ * Test the info() function with audio enabled and an URL that doesn't need to be converted.
*
* @return void
*/
- public function testVideoWithUnconvertedAudio()
+ public function testInfoWithUnconvertedAudio()
{
Config::setOptions(['convert' => true]);
$this->assertRequestIsRedirect(
- 'video',
+ 'info',
[
'url' => 'https://2080.bandcamp.com/track/cygnus-x-the-orange-theme-2080-faulty-chip-cover',
'audio' => true,
@@ -277,16 +277,16 @@ class FrontControllerTest extends BaseTest
}
/**
- * Test the video() function with a password.
+ * Test the info() function with a password.
*
* @return void
*/
- public function testVideoWithPassword()
+ public function testInfoWithPassword()
{
if (getenv('CI')) {
$this->markTestSkipped('Travis is blacklisted by Vimeo.');
}
- $result = $this->controller->video(
+ $result = $this->controller->info(
$this->request->withQueryParams(['url' => 'http://vimeo.com/68375962'])
->withParsedBody(['password' => 'youtube-dl']),
$this->response
@@ -295,44 +295,44 @@ class FrontControllerTest extends BaseTest
}
/**
- * Test the video() function with a missing password.
+ * Test the info() function with a missing password.
*
* @return void
*/
- public function testVideoWithMissingPassword()
+ public function testInfoWithMissingPassword()
{
if (getenv('CI')) {
$this->markTestSkipped('Travis is blacklisted by Vimeo.');
}
- $this->assertRequestIsOk('video', ['url' => 'http://vimeo.com/68375962']);
- $this->assertRequestIsOk('video', ['url' => 'http://vimeo.com/68375962', 'audio' => true]);
+ $this->assertRequestIsOk('info', ['url' => 'http://vimeo.com/68375962']);
+ $this->assertRequestIsOk('info', ['url' => 'http://vimeo.com/68375962', 'audio' => true]);
}
/**
- * Test the video() function with streams enabled.
+ * Test the info() function with streams enabled.
*
* @return void
*/
- public function testVideoWithStream()
+ public function testInfoWithStream()
{
Config::setOptions(['stream' => true]);
- $this->assertRequestIsOk('video', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU']);
+ $this->assertRequestIsOk('info', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU']);
$this->assertRequestIsOk(
- 'video',
+ 'info',
['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'audio' => true]
);
}
/**
- * Test the video() function with a playlist.
+ * Test the info() function with a playlist.
*
* @return void
*/
- public function testVideoWithPlaylist()
+ public function testInfoWithPlaylist()
{
$this->assertRequestIsOk(
- 'video',
+ 'info',
['url' => 'https://www.youtube.com/playlist?list=PLgdySZU6KUXL_8Jq5aUkyNV7wCa-4wZsC']
);
}
@@ -349,59 +349,59 @@ class FrontControllerTest extends BaseTest
}
/**
- * Test the redirect() function without the URL parameter.
+ * Test the download() function without the URL parameter.
*
* @return void
*/
- public function testRedirectWithoutUrl()
+ public function testDownloadWithoutUrl()
{
- $this->assertRequestIsRedirect('redirect');
+ $this->assertRequestIsRedirect('download');
}
/**
- * Test the redirect() function.
+ * Test the download() function.
*
* @return void
*/
- public function testRedirect()
+ public function testDownload()
{
- $this->assertRequestIsRedirect('redirect', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU']);
+ $this->assertRequestIsRedirect('download', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU']);
}
/**
- * Test the redirect() function with a specific format.
+ * Test the download() function with a specific format.
*
* @return void
*/
- public function testRedirectWithFormat()
+ public function testDownloadWithFormat()
{
$this->assertRequestIsRedirect(
- 'redirect',
+ 'download',
['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'format' => 'worst']
);
}
/**
- * Test the redirect() function with streams enabled.
+ * Test the download() function with streams enabled.
*
* @return void
*/
- public function testRedirectWithStream()
+ public function testDownloadWithStream()
{
Config::setOptions(['stream' => true]);
$this->assertRequestIsOk(
- 'redirect',
+ 'download',
['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU']
);
}
/**
- * Test the redirect() function with an M3U stream.
+ * Test the download() function with an M3U stream.
*
* @return void
*/
- public function testRedirectWithM3uStream()
+ public function testDownloadWithM3uStream()
{
if (getenv('CI')) {
$this->markTestSkipped('Twitter returns a 429 error when the test is ran too many times.');
@@ -410,7 +410,7 @@ class FrontControllerTest extends BaseTest
Config::setOptions(['stream' => true]);
$this->assertRequestIsOk(
- 'redirect',
+ 'download',
[
'url' => 'https://twitter.com/verge/status/813055465324056576/video/1',
'format' => 'hls-2176',
@@ -419,33 +419,33 @@ class FrontControllerTest extends BaseTest
}
/**
- * Test the redirect() function with an RTMP stream.
+ * Test the download() function with an RTMP stream.
*
* @return void
*/
- public function testRedirectWithRtmpStream()
+ public function testDownloadWithRtmpStream()
{
$this->markTestIncomplete('We need to find another RTMP video.');
Config::setOptions(['stream' => true]);
$this->assertRequestIsOk(
- 'redirect',
+ 'download',
['url' => 'http://www.rtvnh.nl/video/131946', 'format' => 'rtmp-264']
);
}
/**
- * Test the redirect() function with a remuxed video.
+ * Test the download() function with a remuxed video.
*
* @return void
*/
- public function testRedirectWithRemux()
+ public function testDownloadWithRemux()
{
Config::setOptions(['remux' => true]);
$this->assertRequestIsOk(
- 'redirect',
+ 'download',
[
'url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU',
'format' => 'bestvideo+bestaudio',
@@ -454,14 +454,14 @@ class FrontControllerTest extends BaseTest
}
/**
- * Test the redirect() function with a remuxed video but remux disabled.
+ * Test the download() function with a remuxed video but remux disabled.
*
* @return void
*/
- public function testRedirectWithRemuxDisabled()
+ public function testDownloadWithRemuxDisabled()
{
$this->assertRequestIsServerError(
- 'redirect',
+ 'download',
[
'url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU',
'format' => 'bestvideo+bestaudio',
@@ -470,69 +470,69 @@ class FrontControllerTest extends BaseTest
}
/**
- * Test the redirect() function with a missing password.
+ * Test the download() function with a missing password.
*
* @return void
*/
- public function testRedirectWithMissingPassword()
+ public function testDownloadWithMissingPassword()
{
if (getenv('CI')) {
$this->markTestSkipped('Travis is blacklisted by Vimeo.');
}
- $this->assertRequestIsRedirect('redirect', ['url' => 'http://vimeo.com/68375962']);
+ $this->assertRequestIsRedirect('download', ['url' => 'http://vimeo.com/68375962']);
}
/**
- * Test the redirect() function with an error.
+ * Test the download() function with an error.
*
* @return void
*/
- public function testRedirectWithError()
+ public function testDownloadWithError()
{
- $this->assertRequestIsServerError('redirect', ['url' => 'http://example.com/foo']);
+ $this->assertRequestIsServerError('download', ['url' => 'http://example.com/foo']);
}
/**
- * Test the redirect() function with an video that returns an empty URL.
+ * Test the download() function with an video that returns an empty URL.
* This can be caused by trying to redirect to a playlist.
*
* @return void
*/
- public function testRedirectWithEmptyUrl()
+ public function testDownloadWithEmptyUrl()
{
$this->assertRequestIsServerError(
- 'redirect',
+ 'download',
['url' => 'https://www.youtube.com/playlist?list=PLgdySZU6KUXL_8Jq5aUkyNV7wCa-4wZsC']
);
}
/**
- * Test the redirect() function with a playlist stream.
+ * Test the download() function with a playlist stream.
*
* @return void
* @requires OS Linux
*/
- public function testRedirectWithPlaylist()
+ public function testDownloadWithPlaylist()
{
Config::setOptions(['stream' => true]);
$this->assertRequestIsOk(
- 'redirect',
+ 'download',
['url' => 'https://www.youtube.com/playlist?list=PLgdySZU6KUXL_8Jq5aUkyNV7wCa-4wZsC']
);
}
/**
- * Test the redirect() function with an advanced conversion.
+ * Test the download() function with an advanced conversion.
*
* @return void
*/
- public function testRedirectWithAdvancedConversion()
+ public function testDownloadWithAdvancedConversion()
{
Config::setOptions(['convertAdvanced' => true]);
$this->assertRequestIsOk(
- 'redirect',
+ 'download',
[
'url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU',
'format' => 'best',