alltube/controllers/FrontController.php

231 lines
6.7 KiB
PHP
Raw Normal View History

2015-10-29 22:32:36 +01:00
<?php
2015-10-31 15:50:32 +01:00
/**
2016-09-08 00:28:28 +02:00
* FrontController class.
2016-08-01 13:29:13 +02:00
*/
2015-10-29 22:32:36 +01:00
namespace Alltube\Controller;
2016-03-30 01:49:08 +02:00
2015-10-31 15:42:25 +01:00
use Alltube\Config;
2016-09-08 00:28:28 +02:00
use Alltube\VideoDownload;
use Slim\Container;
2016-07-22 13:58:33 +02:00
use Slim\Http\Request;
use Slim\Http\Response;
2016-09-08 00:28:28 +02:00
use Slim\Http\Stream;
2016-03-30 01:49:08 +02:00
2015-10-31 15:50:32 +01:00
/**
2016-09-08 00:28:28 +02:00
* Main controller.
2016-08-01 13:29:13 +02:00
*/
2015-10-31 15:50:32 +01:00
class FrontController
{
2016-08-01 13:29:13 +02:00
/**
2016-09-08 00:28:28 +02:00
* Config instance.
*
2016-08-01 13:29:13 +02:00
* @var Config
*/
2016-08-01 03:16:48 +02:00
private $config;
2016-08-01 13:29:13 +02:00
/**
2016-09-08 00:28:28 +02:00
* VideoDownload instance.
*
2016-08-01 13:29:13 +02:00
* @var VideoDownload
*/
2016-08-01 03:16:48 +02:00
private $download;
2016-08-01 13:29:13 +02:00
/**
2016-09-08 00:28:28 +02:00
* Slim dependency container.
*
2016-08-01 13:29:13 +02:00
* @var Container
*/
2016-08-01 03:16:48 +02:00
private $container;
2016-08-01 13:29:13 +02:00
/**
2016-09-08 00:28:28 +02:00
* FrontController constructor.
*
2016-08-01 13:29:13 +02:00
* @param Container $container Slim dependency container
*/
2016-08-01 03:05:14 +02:00
public function __construct(Container $container)
2016-04-08 19:06:41 +02:00
{
$this->config = Config::getInstance();
$this->download = new VideoDownload();
2016-07-22 14:43:50 +02:00
$this->container = $container;
2016-04-08 19:06:41 +02:00
}
2015-10-29 22:32:36 +01:00
2015-10-31 15:50:32 +01:00
/**
2016-09-08 00:28:28 +02:00
* Display index page.
2016-02-28 23:04:53 +01:00
*
2016-03-30 01:39:47 +02:00
* @param Request $request PSR-7 request
* @param Response $response PSR-7 response
*
2015-10-31 15:50:32 +01:00
* @return void
*/
2016-07-22 13:58:33 +02:00
public function index(Request $request, Response $response)
2015-10-31 15:50:32 +01:00
{
2016-07-22 14:43:50 +02:00
$this->container->view->render(
2016-03-30 01:39:47 +02:00
$response,
2016-08-01 01:01:10 +02:00
'index.tpl',
2016-09-08 00:28:28 +02:00
[
'convert' => $this->config->convert,
'class' => 'index',
'description' => 'Easily download videos from Youtube, Dailymotion, Vimeo and other websites.',
]
2015-10-29 22:32:36 +01:00
);
}
2015-10-31 15:50:32 +01:00
/**
2016-09-08 00:28:28 +02:00
* Display a list of extractors.
2016-02-28 23:04:53 +01:00
*
2016-03-30 01:39:47 +02:00
* @param Request $request PSR-7 request
* @param Response $response PSR-7 response
*
2015-10-31 15:50:32 +01:00
* @return void
*/
2016-07-22 13:58:33 +02:00
public function extractors(Request $request, Response $response)
2015-10-31 15:50:32 +01:00
{
2016-07-22 14:43:50 +02:00
$this->container->view->render(
2016-03-30 01:39:47 +02:00
$response,
2016-08-01 01:01:10 +02:00
'extractors.tpl',
2016-09-08 00:28:28 +02:00
[
'extractors' => $this->download->listExtractors(),
'class' => 'extractors',
'title' => 'Supported websites',
'description' => 'List of all supported websites from which Alltube Download can extract video or audio files',
]
2015-10-29 22:32:36 +01:00
);
}
2015-10-31 15:50:32 +01:00
/**
2016-09-08 00:28:28 +02:00
* Dislay information about the video.
2016-02-28 23:04:53 +01:00
*
2016-03-30 01:39:47 +02:00
* @param Request $request PSR-7 request
* @param Response $response PSR-7 response
*
2016-08-01 13:29:13 +02:00
* @return Response HTTP response
2015-10-31 15:50:32 +01:00
*/
2016-07-22 13:58:33 +02:00
public function video(Request $request, Response $response)
2015-10-31 15:50:32 +01:00
{
2016-04-07 02:09:34 +02:00
$params = $request->getQueryParams();
2016-04-08 19:06:41 +02:00
$this->config = Config::getInstance();
2016-09-08 00:28:28 +02:00
if (isset($params['url'])) {
2016-04-07 02:09:34 +02:00
if (isset($params['audio'])) {
2015-10-29 22:32:36 +01:00
try {
return $this->getStream($params["url"], 'mp3[protocol^=http]', $response, $request);
2016-05-01 01:25:08 +02:00
} catch (\Exception $e) {
$response = $response->withHeader(
'Content-Disposition',
'attachment; filename="'.
2016-09-08 00:28:28 +02:00
$this->download->getAudioFilename($params['url'], 'bestaudio/best').'"'
2016-05-01 01:25:08 +02:00
);
$response = $response->withHeader('Content-Type', 'audio/mpeg');
2016-04-14 12:42:13 +02:00
2016-05-01 01:25:08 +02:00
if ($request->isGet()) {
2016-09-08 00:28:28 +02:00
$process = $this->download->getAudioStream($params['url'], 'bestaudio/best');
$response = $response->withBody(new Stream($process));
2016-05-01 01:25:08 +02:00
}
2016-09-08 00:28:28 +02:00
2016-05-01 01:25:08 +02:00
return $response;
2015-10-29 22:32:36 +01:00
}
} else {
2016-09-08 00:28:28 +02:00
$video = $this->download->getJSON($params['url']);
2016-07-22 14:43:50 +02:00
$this->container->view->render(
2016-05-01 01:25:08 +02:00
$response,
2016-08-01 01:01:10 +02:00
'video.tpl',
2016-09-08 00:28:28 +02:00
[
'video' => $video,
'class' => 'video',
'title' => $video->title,
'description' => 'Download "'.$video->title.'" from '.$video->extractor_key,
]
2016-05-01 01:25:08 +02:00
);
2015-10-29 22:32:36 +01:00
}
2016-06-09 21:15:44 +02:00
} else {
2016-07-22 14:43:50 +02:00
return $response->withRedirect($this->container->get('router')->pathFor('index'));
2015-10-29 22:32:36 +01:00
}
2016-05-01 01:25:08 +02:00
}
2016-08-01 13:29:13 +02:00
/**
2016-09-08 00:28:28 +02:00
* Display an error page.
*
* @param Request $request PSR-7 request
* @param Response $response PSR-7 response
* @param \Exception $exception Error to display
*
2016-08-01 13:29:13 +02:00
* @return Response HTTP response
*/
2016-07-22 13:58:33 +02:00
public function error(Request $request, Response $response, \Exception $exception)
2016-05-01 01:25:08 +02:00
{
2016-07-22 14:43:50 +02:00
$this->container->view->render(
2016-05-01 01:25:08 +02:00
$response,
2016-08-01 01:01:10 +02:00
'error.tpl',
2016-09-08 00:28:28 +02:00
[
'errors' => $exception->getMessage(),
'class' => 'video',
'title' => 'Error',
]
2016-05-01 01:25:08 +02:00
);
2016-09-08 00:28:28 +02:00
return $response->withStatus(500);
2015-10-29 22:32:36 +01:00
}
2015-10-31 11:48:14 +01:00
2016-04-08 23:01:07 +02:00
private function getStream($url, $format, $response, $request)
{
if (!isset($format)) {
$format = 'best';
}
$video = $this->download->getJSON($url, $format);
$client = new \GuzzleHttp\Client();
$stream = $client->request('GET', $video->url, array('stream'=>true));
2016-04-09 00:12:57 +02:00
$response = $response->withHeader('Content-Disposition', 'attachment; filename="'.$video->_filename.'"');
2016-04-08 23:01:07 +02:00
$response = $response->withHeader('Content-Type', $stream->getHeader('Content-Type'));
$response = $response->withHeader('Content-Length', $stream->getHeader('Content-Length'));
2016-04-08 23:01:07 +02:00
if ($request->isGet()) {
$response = $response->withBody($stream->getBody());
}
return $response;
}
2015-10-31 15:50:32 +01:00
/**
2016-09-08 00:28:28 +02:00
* Redirect to video file.
2016-02-28 23:04:53 +01:00
*
2016-03-30 01:39:47 +02:00
* @param Request $request PSR-7 request
* @param Response $response PSR-7 response
*
2016-08-01 13:29:13 +02:00
* @return Response HTTP response
2015-10-31 15:50:32 +01:00
*/
2016-07-22 13:58:33 +02:00
public function redirect(Request $request, Response $response)
2015-10-31 15:50:32 +01:00
{
2016-04-07 02:09:34 +02:00
$params = $request->getQueryParams();
2016-09-08 00:28:28 +02:00
if (isset($params['url'])) {
2015-10-31 11:48:14 +01:00
try {
2016-04-08 23:01:07 +02:00
return $this->getStream($params["url"], $params["format"], $response, $request);
2015-10-31 11:48:14 +01:00
} catch (\Exception $e) {
2016-04-10 19:28:59 +02:00
$response->getBody()->write($e->getMessage());
2016-09-08 00:28:28 +02:00
2016-03-30 01:39:47 +02:00
return $response->withHeader('Content-Type', 'text/plain');
2015-10-31 11:48:14 +01:00
}
}
}
2015-10-31 15:50:32 +01:00
/**
2016-09-08 00:28:28 +02:00
* Output JSON info about the video.
2016-02-28 23:04:53 +01:00
*
2016-03-30 01:39:47 +02:00
* @param Request $request PSR-7 request
* @param Response $response PSR-7 response
*
2016-08-01 13:29:13 +02:00
* @return Response HTTP response
2015-10-31 15:50:32 +01:00
*/
2016-07-22 13:58:33 +02:00
public function json(Request $request, Response $response)
2015-10-31 15:50:32 +01:00
{
2016-04-07 02:09:34 +02:00
$params = $request->getQueryParams();
2016-09-08 00:28:28 +02:00
if (isset($params['url'])) {
2015-10-31 11:48:14 +01:00
try {
2016-09-08 00:28:28 +02:00
$video = $this->download->getJSON($params['url']);
2016-03-30 01:39:47 +02:00
return $response->withJson($video);
2015-10-31 11:48:14 +01:00
} catch (\Exception $e) {
2016-03-30 01:39:47 +02:00
return $response->withJson(
2016-09-08 00:28:28 +02:00
['success' => false, 'error' => $e->getMessage()]
2016-03-30 01:39:47 +02:00
);
2015-10-31 11:48:14 +01:00
}
}
}
2015-10-29 22:32:36 +01:00
}