Simplify PSR-4 autoload
This commit is contained in:
parent
65ccf95437
commit
d127964eff
12 changed files with 81 additions and 84 deletions
146
classes/Controller/BaseController.php
Normal file
146
classes/Controller/BaseController.php
Normal file
|
@ -0,0 +1,146 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* BaseController class.
|
||||
*/
|
||||
|
||||
namespace Alltube\Controller;
|
||||
|
||||
use Alltube\Config;
|
||||
use Alltube\Library\Downloader;
|
||||
use Alltube\Library\Video;
|
||||
use Alltube\LocaleManager;
|
||||
use Alltube\SessionManager;
|
||||
use Aura\Session\Segment;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Slim\Http\Request;
|
||||
use Slim\Http\Response;
|
||||
|
||||
/**
|
||||
* Abstract class used by every controller.
|
||||
*/
|
||||
abstract class BaseController
|
||||
{
|
||||
/**
|
||||
* Current video.
|
||||
*
|
||||
* @var Video
|
||||
*/
|
||||
protected $video;
|
||||
|
||||
/**
|
||||
* Default youtube-dl format.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $defaultFormat = 'best/bestvideo';
|
||||
|
||||
/**
|
||||
* Slim dependency container.
|
||||
*
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
protected $container;
|
||||
|
||||
/**
|
||||
* Config instance.
|
||||
*
|
||||
* @var Config
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* Session segment used to store session variables.
|
||||
*
|
||||
* @var Segment
|
||||
*/
|
||||
protected $sessionSegment;
|
||||
|
||||
/**
|
||||
* LocaleManager instance.
|
||||
*
|
||||
* @var LocaleManager
|
||||
*/
|
||||
protected $localeManager;
|
||||
|
||||
/**
|
||||
* Downloader instance.
|
||||
*
|
||||
* @var Downloader
|
||||
*/
|
||||
protected $downloader;
|
||||
|
||||
/**
|
||||
* BaseController constructor.
|
||||
*
|
||||
* @param ContainerInterface $container Slim dependency container
|
||||
*/
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->config = Config::getInstance();
|
||||
$this->container = $container;
|
||||
$session = SessionManager::getSession();
|
||||
$this->sessionSegment = $session->getSegment(self::class);
|
||||
$this->localeManager = $this->container->get('locale');
|
||||
$this->downloader = $this->config->getDownloader();
|
||||
|
||||
if (!$this->config->stream) {
|
||||
// Force HTTP if stream is not enabled.
|
||||
$this->defaultFormat = Config::addHttpToFormat($this->defaultFormat);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get video format from request parameters or default format if none is specified.
|
||||
*
|
||||
* @param Request $request PSR-7 request
|
||||
*
|
||||
* @return string format
|
||||
*/
|
||||
protected function getFormat(Request $request)
|
||||
{
|
||||
$format = $request->getQueryParam('format');
|
||||
if (!isset($format)) {
|
||||
$format = $this->defaultFormat;
|
||||
}
|
||||
|
||||
return $format;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the password entered for the current video.
|
||||
*
|
||||
* @param Request $request PSR-7 request
|
||||
*
|
||||
* @return string Password
|
||||
*/
|
||||
protected function getPassword(Request $request)
|
||||
{
|
||||
$url = $request->getQueryParam('url');
|
||||
|
||||
$password = $request->getParam('password');
|
||||
if (isset($password)) {
|
||||
$this->sessionSegment->setFlash($url, $password);
|
||||
} else {
|
||||
$password = $this->sessionSegment->getFlash($url);
|
||||
}
|
||||
|
||||
return $password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display an user-friendly error.
|
||||
*
|
||||
* @param Request $request PSR-7 request
|
||||
* @param Response $response PSR-7 response
|
||||
* @param string $message Error message
|
||||
*
|
||||
* @return Response HTTP response
|
||||
*/
|
||||
protected function displayError(Request $request, Response $response, $message)
|
||||
{
|
||||
$controller = new FrontController($this->container);
|
||||
|
||||
return $controller->displayError($request, $response, $message);
|
||||
}
|
||||
}
|
324
classes/Controller/DownloadController.php
Normal file
324
classes/Controller/DownloadController.php
Normal file
|
@ -0,0 +1,324 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* DownloadController class.
|
||||
*/
|
||||
|
||||
namespace Alltube\Controller;
|
||||
|
||||
use Alltube\Config;
|
||||
use Alltube\Library\Exception\EmptyUrlException;
|
||||
use Alltube\Library\Exception\InvalidProtocolConversionException;
|
||||
use Alltube\Library\Exception\PasswordException;
|
||||
use Alltube\Library\Exception\AlltubeLibraryException;
|
||||
use Alltube\Library\Exception\PlaylistConversionException;
|
||||
use Alltube\Library\Exception\PopenStreamException;
|
||||
use Alltube\Library\Exception\RemuxException;
|
||||
use Alltube\Library\Exception\WrongPasswordException;
|
||||
use Alltube\Library\Exception\YoutubedlException;
|
||||
use Alltube\Stream\ConvertedPlaylistArchiveStream;
|
||||
use Alltube\Stream\PlaylistArchiveStream;
|
||||
use Alltube\Stream\YoutubeStream;
|
||||
use Slim\Http\Request;
|
||||
use Slim\Http\Response;
|
||||
use Slim\Http\Stream;
|
||||
|
||||
/**
|
||||
* Controller that returns a video or audio file.
|
||||
*/
|
||||
class DownloadController extends BaseController
|
||||
{
|
||||
/**
|
||||
* Redirect to video file.
|
||||
*
|
||||
* @param Request $request PSR-7 request
|
||||
* @param Response $response PSR-7 response
|
||||
*
|
||||
* @return Response HTTP response
|
||||
* @throws AlltubeLibraryException
|
||||
*/
|
||||
public function download(Request $request, Response $response)
|
||||
{
|
||||
$url = $request->getQueryParam('url');
|
||||
|
||||
if (isset($url)) {
|
||||
$this->video = $this->downloader->getVideo($url, $this->getFormat($request), $this->getPassword($request));
|
||||
|
||||
try {
|
||||
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);
|
||||
}
|
||||
|
||||
// Regular download.
|
||||
return $this->getDownloadResponse($request, $response);
|
||||
} catch (PasswordException $e) {
|
||||
$frontController = new FrontController($this->container);
|
||||
|
||||
return $frontController->password($request, $response);
|
||||
} catch (WrongPasswordException $e) {
|
||||
return $this->displayError($request, $response, $this->localeManager->t('Wrong password'));
|
||||
} catch (PlaylistConversionException $e) {
|
||||
return $this->displayError(
|
||||
$request,
|
||||
$response,
|
||||
$this->localeManager->t('Conversion of playlists is not supported.')
|
||||
);
|
||||
} catch (InvalidProtocolConversionException $e) {
|
||||
if (in_array($this->video->protocol, ['m3u8', 'm3u8_native'])) {
|
||||
return $this->displayError(
|
||||
$request,
|
||||
$response,
|
||||
$this->localeManager->t('Conversion of M3U8 files is not supported.')
|
||||
);
|
||||
} elseif ($this->video->protocol == 'http_dash_segments') {
|
||||
return $this->displayError(
|
||||
$request,
|
||||
$response,
|
||||
$this->localeManager->t('Conversion of DASH segments is not supported.')
|
||||
);
|
||||
} else {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return $response->withRedirect($this->container->get('router')->pathFor('index'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a converted MP3 file.
|
||||
*
|
||||
* @param Request $request PSR-7 request
|
||||
* @param Response $response PSR-7 response
|
||||
*
|
||||
* @return Response HTTP response
|
||||
* @throws AlltubeLibraryException
|
||||
*/
|
||||
private function getConvertedAudioResponse(Request $request, Response $response)
|
||||
{
|
||||
$from = $request->getQueryParam('from');
|
||||
$to = $request->getQueryParam('to');
|
||||
|
||||
$response = $response->withHeader(
|
||||
'Content-Disposition',
|
||||
'attachment; filename="' .
|
||||
$this->video->getFileNameWithExtension('mp3') . '"'
|
||||
);
|
||||
$response = $response->withHeader('Content-Type', 'audio/mpeg');
|
||||
|
||||
if ($request->isGet() || $request->isPost()) {
|
||||
$process = $this->downloader->getAudioStream($this->video, $this->config->audioBitrate, $from, $to);
|
||||
$response = $response->withBody(new Stream($process));
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the MP3 file.
|
||||
*
|
||||
* @param Request $request PSR-7 request
|
||||
* @param Response $response PSR-7 response
|
||||
*
|
||||
* @return Response HTTP response
|
||||
* @throws AlltubeLibraryException
|
||||
* @throws EmptyUrlException
|
||||
* @throws PasswordException
|
||||
* @throws WrongPasswordException
|
||||
*/
|
||||
private function getAudioResponse(Request $request, Response $response)
|
||||
{
|
||||
if (!empty($request->getQueryParam('from')) || !empty($request->getQueryParam('to'))) {
|
||||
// Force convert when we need to seek.
|
||||
$this->video = $this->video->withFormat('bestaudio/' . $this->defaultFormat);
|
||||
|
||||
return $this->getConvertedAudioResponse($request, $response);
|
||||
} else {
|
||||
try {
|
||||
// First, we try to get a MP3 file directly.
|
||||
if ($this->config->stream) {
|
||||
$this->video = $this->video->withFormat('mp3');
|
||||
|
||||
return $this->getStream($request, $response);
|
||||
} else {
|
||||
$this->video = $this->video->withFormat(Config::addHttpToFormat('mp3'));
|
||||
|
||||
$urls = $this->video->getUrl();
|
||||
|
||||
return $response->withRedirect($urls[0]);
|
||||
}
|
||||
} catch (YoutubedlException $e) {
|
||||
// If MP3 is not available, we convert it.
|
||||
$this->video = $this->video->withFormat('bestaudio/' . $this->defaultFormat);
|
||||
|
||||
return $this->getConvertedAudioResponse($request, $response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a video/audio stream piped through the server.
|
||||
*
|
||||
* @param Request $request PSR-7 request
|
||||
*
|
||||
* @param Response $response PSR-7 response
|
||||
* @return Response HTTP response
|
||||
* @throws AlltubeLibraryException
|
||||
*/
|
||||
private function getStream(Request $request, Response $response)
|
||||
{
|
||||
if (isset($this->video->entries)) {
|
||||
if ($this->config->convert && $request->getQueryParam('audio')) {
|
||||
$stream = new ConvertedPlaylistArchiveStream($this->downloader, $this->video);
|
||||
} else {
|
||||
$stream = new PlaylistArchiveStream($this->downloader, $this->video);
|
||||
}
|
||||
$response = $response->withHeader('Content-Type', 'application/zip');
|
||||
$response = $response->withHeader(
|
||||
'Content-Disposition',
|
||||
'attachment; filename="' . $this->video->title . '.zip"'
|
||||
);
|
||||
|
||||
return $response->withBody($stream);
|
||||
} elseif ($this->video->protocol == 'rtmp') {
|
||||
$response = $response->withHeader('Content-Type', 'video/' . $this->video->ext);
|
||||
$body = new Stream($this->downloader->getRtmpStream($this->video));
|
||||
} elseif ($this->video->protocol == 'm3u8' || $this->video->protocol == 'm3u8_native') {
|
||||
$response = $response->withHeader('Content-Type', 'video/' . $this->video->ext);
|
||||
$body = new Stream($this->downloader->getM3uStream($this->video));
|
||||
} else {
|
||||
$headers = [];
|
||||
$range = $request->getHeader('Range');
|
||||
|
||||
if (!empty($range)) {
|
||||
$headers['Range'] = $range;
|
||||
}
|
||||
$stream = $this->downloader->getHttpResponse($this->video, $headers);
|
||||
|
||||
$response = $response->withHeader('Content-Type', $stream->getHeader('Content-Type'));
|
||||
$response = $response->withHeader('Content-Length', $stream->getHeader('Content-Length'));
|
||||
$response = $response->withHeader('Accept-Ranges', $stream->getHeader('Accept-Ranges'));
|
||||
$response = $response->withHeader('Content-Range', $stream->getHeader('Content-Range'));
|
||||
if ($stream->getStatusCode() == 206) {
|
||||
$response = $response->withStatus(206);
|
||||
}
|
||||
|
||||
if (isset($this->video->downloader_options->http_chunk_size)) {
|
||||
// Workaround for Youtube throttling the download speed.
|
||||
$body = new YoutubeStream($this->downloader, $this->video);
|
||||
} else {
|
||||
$body = $stream->getBody();
|
||||
}
|
||||
}
|
||||
if ($request->isGet()) {
|
||||
$response = $response->withBody($body);
|
||||
}
|
||||
$response = $response->withHeader(
|
||||
'Content-Disposition',
|
||||
'attachment; filename="' .
|
||||
$this->video->getFilename() . '"'
|
||||
);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a remuxed stream piped through the server.
|
||||
*
|
||||
* @param Request $request PSR-7 request
|
||||
*
|
||||
* @param Response $response PSR-7 response
|
||||
* @return Response HTTP response
|
||||
* @throws AlltubeLibraryException
|
||||
*/
|
||||
private function getRemuxStream(Request $request, Response $response)
|
||||
{
|
||||
if (!$this->config->remux) {
|
||||
throw new RemuxException('You need to enable remux mode to merge two formats.');
|
||||
}
|
||||
$stream = $this->downloader->getRemuxStream($this->video);
|
||||
$response = $response->withHeader('Content-Type', 'video/x-matroska');
|
||||
if ($request->isGet()) {
|
||||
$response = $response->withBody(new Stream($stream));
|
||||
}
|
||||
|
||||
return $response->withHeader(
|
||||
'Content-Disposition',
|
||||
'attachment; filename="' . $this->video->getFileNameWithExtension('mkv') . '"'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get approriate HTTP response to download query.
|
||||
* Depends on whether we want to stream, remux or simply redirect.
|
||||
*
|
||||
* @param Request $request PSR-7 request
|
||||
*
|
||||
* @param Response $response PSR-7 response
|
||||
* @return Response HTTP response
|
||||
* @throws AlltubeLibraryException
|
||||
*/
|
||||
private function getDownloadResponse(Request $request, Response $response)
|
||||
{
|
||||
try {
|
||||
$videoUrls = $this->video->getUrl();
|
||||
} catch (EmptyUrlException $e) {
|
||||
/*
|
||||
If this happens it is probably a playlist
|
||||
so it will either be handled by getStream() or throw an exception anyway.
|
||||
*/
|
||||
$videoUrls = [];
|
||||
}
|
||||
if (count($videoUrls) > 1) {
|
||||
return $this->getRemuxStream($request, $response);
|
||||
} elseif ($this->config->stream && (isset($this->video->entries) || $request->getQueryParam('stream'))) {
|
||||
return $this->getStream($request, $response);
|
||||
} else {
|
||||
if (empty($videoUrls[0])) {
|
||||
throw new EmptyUrlException("Can't find URL of video.");
|
||||
}
|
||||
|
||||
return $response->withRedirect($videoUrls[0]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a converted video file.
|
||||
*
|
||||
* @param Request $request PSR-7 request
|
||||
* @param Response $response PSR-7 response
|
||||
*
|
||||
* @return Response HTTP response
|
||||
* @throws AlltubeLibraryException
|
||||
* @throws InvalidProtocolConversionException
|
||||
* @throws PasswordException
|
||||
* @throws PlaylistConversionException
|
||||
* @throws WrongPasswordException
|
||||
* @throws YoutubedlException
|
||||
* @throws PopenStreamException
|
||||
*/
|
||||
private function getConvertedResponse(Request $request, Response $response)
|
||||
{
|
||||
$response = $response->withHeader(
|
||||
'Content-Disposition',
|
||||
'attachment; filename="' .
|
||||
$this->video->getFileNameWithExtension($request->getQueryParam('customFormat')) . '"'
|
||||
);
|
||||
$response = $response->withHeader('Content-Type', 'video/' . $request->getQueryParam('customFormat'));
|
||||
|
||||
if ($request->isGet() || $request->isPost()) {
|
||||
$process = $this->downloader->getConvertedStream(
|
||||
$this->video,
|
||||
$request->getQueryParam('customBitrate'),
|
||||
$request->getQueryParam('customFormat')
|
||||
);
|
||||
$response = $response->withBody(new Stream($process));
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
318
classes/Controller/FrontController.php
Normal file
318
classes/Controller/FrontController.php
Normal file
|
@ -0,0 +1,318 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* FrontController class.
|
||||
*/
|
||||
|
||||
namespace Alltube\Controller;
|
||||
|
||||
use Alltube\Library\Exception\PasswordException;
|
||||
use Alltube\Library\Exception\AlltubeLibraryException;
|
||||
use Alltube\Library\Exception\WrongPasswordException;
|
||||
use Alltube\Locale;
|
||||
use Exception;
|
||||
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
|
||||
use Throwable;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Slim\Http\Request;
|
||||
use Slim\Http\Response;
|
||||
use Slim\Views\Smarty;
|
||||
|
||||
/**
|
||||
* Main controller.
|
||||
*/
|
||||
class FrontController extends BaseController
|
||||
{
|
||||
/**
|
||||
* Smarty view.
|
||||
*
|
||||
* @var Smarty
|
||||
*/
|
||||
private $view;
|
||||
|
||||
/**
|
||||
* BaseController constructor.
|
||||
*
|
||||
* @param ContainerInterface $container Slim dependency container
|
||||
*/
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
parent::__construct($container);
|
||||
|
||||
$this->view = $this->container->get('view');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display index page.
|
||||
*
|
||||
* @param Request $request PSR-7 request
|
||||
* @param Response $response PSR-7 response
|
||||
*
|
||||
* @return Response HTTP response
|
||||
*/
|
||||
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(),
|
||||
]
|
||||
);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Switch locale.
|
||||
*
|
||||
* @param Request $request PSR-7 request
|
||||
* @param Response $response PSR-7 response
|
||||
* @param string[] $data Query parameters
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
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'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a list of extractors.
|
||||
*
|
||||
* @param Request $request PSR-7 request
|
||||
* @param Response $response PSR-7 response
|
||||
*
|
||||
* @return Response HTTP response
|
||||
* @throws AlltubeLibraryException
|
||||
*/
|
||||
public function extractors(Request $request, Response $response)
|
||||
{
|
||||
$this->view->render(
|
||||
$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(),
|
||||
]
|
||||
);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a password prompt.
|
||||
*
|
||||
* @param Request $request PSR-7 request
|
||||
* @param Response $response PSR-7 response
|
||||
*
|
||||
* @return Response HTTP response
|
||||
*/
|
||||
public function password(Request $request, Response $response)
|
||||
{
|
||||
$this->view->render(
|
||||
$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(),
|
||||
]
|
||||
);
|
||||
|
||||
return $response->withStatus(403);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the video description page.
|
||||
*
|
||||
* @param Request $request PSR-7 request
|
||||
* @param Response $response PSR-7 response
|
||||
*
|
||||
* @return Response HTTP response
|
||||
* @throws AlltubeLibraryException
|
||||
*/
|
||||
private function getInfoResponse(Request $request, Response $response)
|
||||
{
|
||||
try {
|
||||
$this->video->getJson();
|
||||
} catch (PasswordException $e) {
|
||||
return $this->password($request, $response);
|
||||
} catch (WrongPasswordException $e) {
|
||||
return $this->displayError($request, $response, $this->localeManager->t('Wrong password'));
|
||||
}
|
||||
|
||||
if (isset($this->video->entries)) {
|
||||
$template = 'playlist.tpl';
|
||||
} else {
|
||||
$template = 'info.tpl';
|
||||
}
|
||||
$title = $this->localeManager->t('Video download');
|
||||
$description = $this->localeManager->t(
|
||||
'Download video from @extractor',
|
||||
['@extractor' => $this->video->extractor_key]
|
||||
);
|
||||
if (isset($this->video->title)) {
|
||||
$title = $this->video->title;
|
||||
$description = $this->localeManager->t(
|
||||
'Download @title from @extractor',
|
||||
[
|
||||
'@title' => $this->video->title,
|
||||
'@extractor' => $this->video->extractor_key
|
||||
]
|
||||
);
|
||||
}
|
||||
$this->view->render(
|
||||
$response,
|
||||
$template,
|
||||
[
|
||||
'video' => $this->video,
|
||||
'class' => 'info',
|
||||
'title' => $title,
|
||||
'description' => $description,
|
||||
'config' => $this->config,
|
||||
'canonical' => $this->getCanonicalUrl($request),
|
||||
'locale' => $this->localeManager->getLocale(),
|
||||
'defaultFormat' => $this->defaultFormat,
|
||||
]
|
||||
);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dislay information about the video.
|
||||
*
|
||||
* @param Request $request PSR-7 request
|
||||
* @param Response $response PSR-7 response
|
||||
*
|
||||
* @return Response HTTP response
|
||||
* @throws AlltubeLibraryException
|
||||
*/
|
||||
public function info(Request $request, Response $response)
|
||||
{
|
||||
$url = $request->getQueryParam('url') ?: $request->getQueryParam('v');
|
||||
|
||||
if (isset($url) && !empty($url)) {
|
||||
$this->video = $this->downloader->getVideo($url, $this->getFormat($request), $this->getPassword($request));
|
||||
|
||||
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())
|
||||
);
|
||||
} else {
|
||||
return $this->getInfoResponse($request, $response);
|
||||
}
|
||||
} else {
|
||||
return $response->withRedirect($this->container->get('router')->pathFor('index'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display an user-friendly error.
|
||||
*
|
||||
* @param Request $request PSR-7 request
|
||||
* @param Response $response PSR-7 response
|
||||
* @param string $message Error message
|
||||
*
|
||||
* @return Response HTTP response
|
||||
*/
|
||||
protected function displayError(Request $request, Response $response, $message)
|
||||
{
|
||||
$this->view->render(
|
||||
$response,
|
||||
'error.tpl',
|
||||
[
|
||||
'config' => $this->config,
|
||||
'error' => $message,
|
||||
'class' => 'video',
|
||||
'title' => $this->localeManager->t('Error'),
|
||||
'canonical' => $this->getCanonicalUrl($request),
|
||||
'locale' => $this->localeManager->getLocale(),
|
||||
]
|
||||
);
|
||||
|
||||
return $response->withStatus(500);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display an error page.
|
||||
*
|
||||
* @param Request $request PSR-7 request
|
||||
* @param Response $response PSR-7 response
|
||||
* @param Throwable $error Error to display
|
||||
*
|
||||
* @return Response HTTP response
|
||||
*/
|
||||
public function error(Request $request, Response $response, Throwable $error)
|
||||
{
|
||||
if ($this->config->debug) {
|
||||
$renderer = new HtmlErrorRenderer(true);
|
||||
$exception = $renderer->render($error);
|
||||
|
||||
$response->getBody()->write($exception->getAsString());
|
||||
foreach ($exception->getHeaders() as $header => $value) {
|
||||
$response = $response->withHeader($header, $value);
|
||||
}
|
||||
|
||||
return $response->withStatus($exception->getStatusCode());
|
||||
} else {
|
||||
if ($error instanceof Exception) {
|
||||
$message = $error->getMessage();
|
||||
} else {
|
||||
$message = '';
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
44
classes/Controller/JsonController.php
Normal file
44
classes/Controller/JsonController.php
Normal file
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* JsonController class.
|
||||
*/
|
||||
|
||||
namespace Alltube\Controller;
|
||||
|
||||
use Alltube\Library\Exception\AlltubeLibraryException;
|
||||
use Slim\Http\Request;
|
||||
use Slim\Http\Response;
|
||||
|
||||
/**
|
||||
* Controller that returns JSON.
|
||||
*/
|
||||
class JsonController extends BaseController
|
||||
{
|
||||
/**
|
||||
* Return the JSON object generated by youtube-dl.
|
||||
*
|
||||
* @param Request $request PSR-7 request
|
||||
* @param Response $response PSR-7 response
|
||||
*
|
||||
* @return Response HTTP response
|
||||
* @throws AlltubeLibraryException
|
||||
*/
|
||||
public function json(Request $request, Response $response)
|
||||
{
|
||||
$url = $request->getQueryParam('url');
|
||||
|
||||
if (isset($url)) {
|
||||
$this->video = $this->downloader->getVideo(
|
||||
$url,
|
||||
$this->getFormat($request),
|
||||
$this->getPassword($request)
|
||||
);
|
||||
|
||||
return $response->withJson($this->video->getJson());
|
||||
} else {
|
||||
return $response->withJson(['error' => 'You need to provide the url parameter'])
|
||||
->withStatus(400);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue