Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Merge branch 'release-3.0.0-beta3'
Browse files Browse the repository at this point in the history
  • Loading branch information
Rudloff committed Oct 28, 2020
2 parents 68f2255 + c15f1e6 commit ac6cd1d
Show file tree
Hide file tree
Showing 25 changed files with 344 additions and 266 deletions.
105 changes: 105 additions & 0 deletions classes/App.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace Alltube;

use Alltube\Controller\DownloadController;
use Alltube\Controller\FrontController;
use Alltube\Controller\JsonController;
use Alltube\Exception\ConfigException;
use Alltube\Exception\DependencyException;
use Alltube\Factory\ConfigFactory;
use Alltube\Factory\LocaleManagerFactory;
use Alltube\Factory\LoggerFactory;
use Alltube\Factory\SessionFactory;
use Alltube\Factory\ViewFactory;
use Alltube\Middleware\CspMiddleware;
use Alltube\Middleware\LinkHeaderMiddleware;
use Alltube\Middleware\LocaleMiddleware;
use Alltube\Middleware\RouterPathMiddleware;
use Slim\Container;
use SmartyException;

class App extends \Slim\App
{
/**
* App constructor.
* @throws ConfigException
* @throws DependencyException
* @throws SmartyException
*/
public function __construct()
{
parent::__construct();

/** @var Container $container */
$container = $this->getContainer();

// Config.
$container['config'] = ConfigFactory::create($container);

// Session.
$container['session'] = SessionFactory::create($container);

// Locales.
$container['locale'] = LocaleManagerFactory::create($container);

// Smarty.
$container['view'] = ViewFactory::create($container);

// Logger.
$container['logger'] = LoggerFactory::create($container);

// Middlewares.
$this->add(new LocaleMiddleware($container));
$this->add(new CspMiddleware($container));
$this->add(new LinkHeaderMiddleware($container));
$this->add(new RouterPathMiddleware($container));

// Controllers.
$frontController = new FrontController($container);
$jsonController = new JsonController($container);
$downloadController = new DownloadController($container);

// Error handling.
$container['errorHandler'] = [$frontController, 'error'];
$container['phpErrorHandler'] = [$frontController, 'error'];
$container['notFoundHandler'] = [$frontController, 'notFound'];
$container['notAllowedHandler'] = [$frontController, 'notAllowed'];

// Routes.
$this->get(
'/',
[$frontController, 'index']
)->setName('index');

$this->get(
'/extractors',
[$frontController, 'extractors']
)->setName('extractors');

$this->any(
'/info',
[$frontController, 'info']
)->setName('info');

$this->any(
'/watch',
[$frontController, 'info']
);

$this->any(
'/download',
[$downloadController, 'download']
)->setName('download');

$this->get(
'/locale/{locale}',
[$frontController, 'locale']
)->setName('locale');

$this->get(
'/json',
[$jsonController, 'json']
)->setName('json');
}
}
8 changes: 7 additions & 1 deletion classes/Controller/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
use Alltube\Library\Downloader;
use Alltube\Library\Video;
use Alltube\LocaleManager;
use Alltube\SessionFactory;
use Aura\Session\Segment;
use Consolidation\Log\Logger;
use Psr\Container\ContainerInterface;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Router;

/**
* Abstract class used by every controller.
Expand Down Expand Up @@ -76,6 +76,11 @@ abstract class BaseController
*/
protected $logger;

/**
* @var Router
*/
protected $router;

/**
* BaseController constructor.
*
Expand All @@ -89,6 +94,7 @@ public function __construct(ContainerInterface $container)
$this->sessionSegment = $session->getSegment(self::class);
$this->localeManager = $this->container->get('locale');
$this->downloader = $this->config->getDownloader();
$this->router = $this->container->get('router');
$this->logger = $this->container->get('logger');
$this->downloader->setLogger($this->logger);

Expand Down
2 changes: 1 addition & 1 deletion classes/Controller/DownloadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function download(Request $request, Response $response)
}
}
} else {
return $response->withRedirect($this->container->get('router')->pathFor('index'));
return $response->withRedirect($this->router->pathFor('index'));
}
}

Expand Down
49 changes: 3 additions & 46 deletions classes/Controller/FrontController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,15 @@ public function __construct(ContainerInterface $container)
*/
public function index(Request $request, Response $response)
{
$uri = $request->getUri()->withUserInfo('');
$this->view->render(
$response,
'index.tpl',
[
'config' => $this->config,
'class' => 'index',
'description' => $this->localeManager->t(
'Easily download videos from Youtube, Dailymotion, Vimeo and other websites.'
),
'domain' => $uri->getScheme() . '://' . $uri->getAuthority(),
'canonical' => $this->getCanonicalUrl($request),
'supportedLocales' => $this->localeManager->getSupportedLocales(),
'locale' => $this->localeManager->getLocale(),
]
);

Expand All @@ -87,7 +82,7 @@ public function locale(Request $request, Response $response, array $data)
{
$this->localeManager->setLocale(new Locale($data['locale']));

return $response->withRedirect($this->container->get('router')->pathFor('index'));
return $response->withRedirect($this->router->pathFor('index'));
}

/**
Expand All @@ -105,14 +100,11 @@ public function extractors(Request $request, Response $response)
$response,
'extractors.tpl',
[
'config' => $this->config,
'extractors' => $this->downloader->getExtractors(),
'class' => 'extractors',
'title' => $this->localeManager->t('Supported websites'),
'description' => $this->localeManager->t('List of all supported websites from which Alltube Download ' .
'can extract video or audio files'),
'canonical' => $this->getCanonicalUrl($request),
'locale' => $this->localeManager->getLocale(),
]
);

Expand All @@ -133,14 +125,11 @@ public function password(Request $request, Response $response)
$response,
'password.tpl',
[
'config' => $this->config,
'class' => 'password',
'title' => $this->localeManager->t('Password prompt'),
'description' => $this->localeManager->t(
'You need a password in order to download this video with Alltube Download'
),
'canonical' => $this->getCanonicalUrl($request),
'locale' => $this->localeManager->getLocale(),
]
);

Expand Down Expand Up @@ -194,9 +183,6 @@ private function getInfoResponse(Request $request, Response $response)
'class' => 'info',
'title' => $title,
'description' => $description,
'config' => $this->config,
'canonical' => $this->getCanonicalUrl($request),
'locale' => $this->localeManager->getLocale(),
'defaultFormat' => $this->defaultFormat,
]
);
Expand All @@ -223,14 +209,13 @@ public function info(Request $request, Response $response)
if ($this->config->convert && $request->getQueryParam('audio')) {
// 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())
$this->router->pathFor('download', [], $request->getQueryParams())
);
} else {
return $this->getInfoResponse($request, $response);
}
} else {
return $response->withRedirect($this->container->get('router')->pathFor('index'));
return $response->withRedirect($this->router->pathFor('index'));
}
}

Expand All @@ -249,12 +234,9 @@ protected function displayError(Request $request, Response $response, string $me
$response,
'error.tpl',
[
'config' => $this->config,
'error' => $message,
'class' => 'video',
'title' => $this->localeManager->t('Error'),
'canonical' => $this->getCanonicalUrl($request),
'locale' => $this->localeManager->getLocale(),
]
);

Expand Down Expand Up @@ -322,29 +304,4 @@ public function error(Request $request, Response $response, Throwable $error)
return $this->displayError($request, $response, $message);
}
}

/**
* Generate the canonical URL of the current page.
*
* @param Request $request PSR-7 Request
*
* @return string URL
*/
private function getCanonicalUrl(Request $request)
{
$uri = $request->getUri();
$return = 'https://alltubedownload.net/';

$path = $uri->getPath();
if ($path != '/') {
$return .= $path;
}

$query = $uri->getQuery();
if (!empty($query)) {
$return .= '?' . $query;
}

return $return;
}
}
22 changes: 22 additions & 0 deletions classes/Factory/ViewFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@
*/
class ViewFactory
{
/**
* Generate the canonical URL of the current page.
*
* @param Request $request PSR-7 Request
*
* @return string URL
*/
private static function getCanonicalUrl(Request $request)
{
/** @var Uri $uri */
$uri = $request->getUri();

return $uri->withBasePath('')
->withHost('alltubedownload.net')
->withScheme('https');
}

/**
* Create Smarty view object.
*
Expand Down Expand Up @@ -63,6 +80,11 @@ public static function create(ContainerInterface $container, Request $request =
$view->registerPlugin('function', 'base_url', [$smartyPlugins, 'baseUrl']);
$view->registerPlugin('block', 't', [$localeManager, 'smartyTranslate']);

$view->offsetSet('canonical', self::getCanonicalUrl($request));
$view->offsetSet('locale', $container->get('locale')->getLocale());
$view->offsetSet('config', $container->get('config'));
$view->offsetSet('domain', $uri->withBasePath('')->getBaseUrl());

return $view;
}
}
18 changes: 11 additions & 7 deletions classes/UglyRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use InvalidArgumentException;
use Psr\Http\Message\ServerRequestInterface;
use RuntimeException;
use Slim\Http\Uri;
use Slim\Router;

/**
Expand All @@ -27,15 +28,17 @@ class UglyRouter extends Router
*/
public function dispatch(ServerRequestInterface $request)
{
parse_str($request->getUri()->getQuery(), $args);
$uri = '/';
if (isset($args['page'])) {
$uri .= $args['page'];
$params = $request->getQueryParams();
$uri = new Uri('', '');

if (isset($params['page'])) {
// Build an URI that the router can understand.
$uri = $uri->withPath($params['page']);
}

return $this->createDispatcher()->dispatch(
$request->getMethod(),
$uri
(string) $uri
);
}

Expand All @@ -52,10 +55,11 @@ public function dispatch(ServerRequestInterface $request)
*/
public function pathFor($name, array $data = [], array $queryParams = [])
{
$url = str_replace('/', '/?page=', $this->relativePathFor($name, $data, $queryParams));
$queryParams['page'] = $name;
$url = Uri::createFromString($this->relativePathFor($name, $data, $queryParams))->withPath('');

if ($this->basePath) {
$url = $this->basePath . $url;
$url = $url->withBasePath($this->basePath);
}

return $url;
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"oomphinc/composer-installers-extender": "^2.0",
"paragonie/csp-builder": "^2.5",
"rinvex/countries": "^6.1",
"rudloff/alltube-library": "dev-develop",
"rudloff/alltube-library": "^0.1.1",
"symfony/finder": "^5.0",
"symfony/translation": "^4.0",
"symfony/yaml": "^4.0",
Expand Down Expand Up @@ -88,8 +88,8 @@
"name": "ytdl-org/youtube-dl",
"version": "2020.09.20",
"dist": {
"type": "zip",
"url": "https://github.com/ytdl-org/youtube-dl/archive/2020.09.20.zip"
"type": "tar",
"url": "https://files.pythonhosted.org/packages/12/8b/51cae2929739d637fdfbc706b2d5f8925b5710d8f408b5319a07ea45fe99/youtube_dl-2020.9.20.tar.gz"
}
}
}
Expand Down
Loading

0 comments on commit ac6cd1d

Please sign in to comment.