2019-04-22 14:05:58 +00:00
|
|
|
<?php
|
2019-10-03 19:24:12 +00:00
|
|
|
|
2019-04-22 14:05:58 +00:00
|
|
|
/**
|
|
|
|
* DownloadController class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Alltube\Controller;
|
|
|
|
|
2020-06-20 23:44:20 +00:00
|
|
|
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;
|
2019-04-22 19:08:36 +00:00
|
|
|
use Alltube\Stream\ConvertedPlaylistArchiveStream;
|
2019-04-22 19:06:05 +00:00
|
|
|
use Alltube\Stream\PlaylistArchiveStream;
|
|
|
|
use Alltube\Stream\YoutubeStream;
|
2019-04-22 14:05:58 +00:00
|
|
|
use Slim\Http\Request;
|
|
|
|
use Slim\Http\Response;
|
2020-07-05 09:22:55 +00:00
|
|
|
use Slim\Http\StatusCode;
|
2019-04-22 14:05:58 +00:00
|
|
|
use Slim\Http\Stream;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Controller that returns a video or audio file.
|
|
|
|
*/
|
|
|
|
class DownloadController extends BaseController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Redirect to video file.
|
|
|
|
*
|
2020-06-20 23:44:20 +00:00
|
|
|
* @param Request $request PSR-7 request
|
2019-04-22 14:05:58 +00:00
|
|
|
* @param Response $response PSR-7 response
|
|
|
|
*
|
|
|
|
* @return Response HTTP response
|
2020-06-20 23:44:20 +00:00
|
|
|
* @throws AlltubeLibraryException
|
2019-04-22 14:05:58 +00:00
|
|
|
*/
|
|
|
|
public function download(Request $request, Response $response)
|
|
|
|
{
|
|
|
|
$url = $request->getQueryParam('url');
|
|
|
|
|
|
|
|
if (isset($url)) {
|
2020-06-20 23:44:20 +00:00
|
|
|
$this->video = $this->downloader->getVideo($url, $this->getFormat($request), $this->getPassword($request));
|
2019-04-22 14:05:58 +00:00
|
|
|
|
|
|
|
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) {
|
2020-06-20 23:44:20 +00:00
|
|
|
$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.')
|
2019-04-22 14:05:58 +00:00
|
|
|
);
|
2020-06-20 23:44:20 +00:00
|
|
|
} 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;
|
|
|
|
}
|
2019-04-22 14:05:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return $response->withRedirect($this->container->get('router')->pathFor('index'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a converted MP3 file.
|
|
|
|
*
|
2020-05-13 19:18:32 +00:00
|
|
|
* @param Request $request PSR-7 request
|
2019-04-22 14:05:58 +00:00
|
|
|
* @param Response $response PSR-7 response
|
|
|
|
*
|
|
|
|
* @return Response HTTP response
|
2020-06-20 23:44:20 +00:00
|
|
|
* @throws AlltubeLibraryException
|
2019-04-22 14:05:58 +00:00
|
|
|
*/
|
|
|
|
private function getConvertedAudioResponse(Request $request, Response $response)
|
|
|
|
{
|
2020-10-17 12:14:36 +00:00
|
|
|
$from = null;
|
|
|
|
$to = null;
|
|
|
|
if ($this->config->convertSeek) {
|
|
|
|
$from = $request->getQueryParam('from');
|
|
|
|
$to = $request->getQueryParam('to');
|
|
|
|
}
|
2019-04-22 14:05:58 +00:00
|
|
|
|
|
|
|
$response = $response->withHeader(
|
|
|
|
'Content-Disposition',
|
2019-10-03 19:24:12 +00:00
|
|
|
'attachment; filename="' .
|
|
|
|
$this->video->getFileNameWithExtension('mp3') . '"'
|
2019-04-22 14:05:58 +00:00
|
|
|
);
|
|
|
|
$response = $response->withHeader('Content-Type', 'audio/mpeg');
|
|
|
|
|
|
|
|
if ($request->isGet() || $request->isPost()) {
|
2020-06-20 23:44:20 +00:00
|
|
|
$process = $this->downloader->getAudioStream($this->video, $this->config->audioBitrate, $from, $to);
|
2019-04-22 14:05:58 +00:00
|
|
|
$response = $response->withBody(new Stream($process));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the MP3 file.
|
|
|
|
*
|
2020-05-13 19:18:32 +00:00
|
|
|
* @param Request $request PSR-7 request
|
2019-04-22 14:05:58 +00:00
|
|
|
* @param Response $response PSR-7 response
|
|
|
|
*
|
|
|
|
* @return Response HTTP response
|
2020-06-20 23:44:20 +00:00
|
|
|
* @throws AlltubeLibraryException
|
|
|
|
* @throws EmptyUrlException
|
2020-05-13 19:18:32 +00:00
|
|
|
* @throws PasswordException
|
2020-06-20 23:44:20 +00:00
|
|
|
* @throws WrongPasswordException
|
2019-04-22 14:05:58 +00:00
|
|
|
*/
|
|
|
|
private function getAudioResponse(Request $request, Response $response)
|
|
|
|
{
|
2020-06-20 23:44:20 +00:00
|
|
|
if (!empty($request->getQueryParam('from')) || !empty($request->getQueryParam('to'))) {
|
|
|
|
// Force convert when we need to seek.
|
|
|
|
$this->video = $this->video->withFormat('bestaudio/' . $this->defaultFormat);
|
2019-04-22 14:05:58 +00:00
|
|
|
|
2020-06-20 23:44:20 +00:00
|
|
|
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');
|
2019-04-22 14:05:58 +00:00
|
|
|
|
2020-06-20 23:44:20 +00:00
|
|
|
return $this->getStream($request, $response);
|
|
|
|
} else {
|
|
|
|
$this->video = $this->video->withFormat(Config::addHttpToFormat('mp3'));
|
2019-04-22 14:05:58 +00:00
|
|
|
|
2020-06-20 23:44:20 +00:00
|
|
|
$urls = $this->video->getUrl();
|
2019-04-22 14:05:58 +00:00
|
|
|
|
2020-06-20 23:44:20 +00:00
|
|
|
return $response->withRedirect($urls[0]);
|
|
|
|
}
|
|
|
|
} catch (YoutubedlException $e) {
|
|
|
|
// If MP3 is not available, we convert it.
|
|
|
|
$this->video = $this->video->withFormat('bestaudio/' . $this->defaultFormat);
|
2019-04-22 14:05:58 +00:00
|
|
|
|
2020-06-20 23:44:20 +00:00
|
|
|
return $this->getConvertedAudioResponse($request, $response);
|
|
|
|
}
|
2019-04-22 14:05:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a video/audio stream piped through the server.
|
|
|
|
*
|
2020-05-13 19:18:32 +00:00
|
|
|
* @param Request $request PSR-7 request
|
2019-04-22 14:05:58 +00:00
|
|
|
*
|
2020-05-13 19:18:32 +00:00
|
|
|
* @param Response $response PSR-7 response
|
2019-04-22 14:05:58 +00:00
|
|
|
* @return Response HTTP response
|
2020-06-20 23:44:20 +00:00
|
|
|
* @throws AlltubeLibraryException
|
2019-04-22 14:05:58 +00:00
|
|
|
*/
|
|
|
|
private function getStream(Request $request, Response $response)
|
|
|
|
{
|
|
|
|
if (isset($this->video->entries)) {
|
|
|
|
if ($this->config->convert && $request->getQueryParam('audio')) {
|
2020-06-20 23:44:20 +00:00
|
|
|
$stream = new ConvertedPlaylistArchiveStream($this->downloader, $this->video);
|
2019-04-22 14:05:58 +00:00
|
|
|
} else {
|
2020-06-20 23:44:20 +00:00
|
|
|
$stream = new PlaylistArchiveStream($this->downloader, $this->video);
|
2019-04-22 14:05:58 +00:00
|
|
|
}
|
|
|
|
$response = $response->withHeader('Content-Type', 'application/zip');
|
|
|
|
$response = $response->withHeader(
|
|
|
|
'Content-Disposition',
|
2019-10-03 19:24:12 +00:00
|
|
|
'attachment; filename="' . $this->video->title . '.zip"'
|
2019-04-22 14:05:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return $response->withBody($stream);
|
|
|
|
} elseif ($this->video->protocol == 'rtmp') {
|
2019-10-03 19:24:12 +00:00
|
|
|
$response = $response->withHeader('Content-Type', 'video/' . $this->video->ext);
|
2020-06-20 23:44:20 +00:00
|
|
|
$body = new Stream($this->downloader->getRtmpStream($this->video));
|
2019-04-22 14:05:58 +00:00
|
|
|
} elseif ($this->video->protocol == 'm3u8' || $this->video->protocol == 'm3u8_native') {
|
2019-10-03 19:24:12 +00:00
|
|
|
$response = $response->withHeader('Content-Type', 'video/' . $this->video->ext);
|
2020-06-20 23:44:20 +00:00
|
|
|
$body = new Stream($this->downloader->getM3uStream($this->video));
|
2019-04-22 14:05:58 +00:00
|
|
|
} else {
|
2019-10-16 21:00:05 +00:00
|
|
|
$headers = [];
|
2019-07-04 21:12:44 +00:00
|
|
|
$range = $request->getHeader('Range');
|
2019-09-26 19:36:40 +00:00
|
|
|
|
|
|
|
if (!empty($range)) {
|
|
|
|
$headers['Range'] = $range;
|
2019-07-04 21:12:44 +00:00
|
|
|
}
|
2020-06-20 23:44:20 +00:00
|
|
|
$stream = $this->downloader->getHttpResponse($this->video, $headers);
|
2019-04-22 14:05:58 +00:00
|
|
|
|
|
|
|
$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'));
|
2020-07-05 09:22:55 +00:00
|
|
|
if ($stream->getStatusCode() == StatusCode::HTTP_PARTIAL_CONTENT) {
|
|
|
|
$response = $response->withStatus(StatusCode::HTTP_PARTIAL_CONTENT);
|
2019-04-22 14:05:58 +00:00
|
|
|
}
|
2019-04-22 18:20:04 +00:00
|
|
|
|
|
|
|
if (isset($this->video->downloader_options->http_chunk_size)) {
|
|
|
|
// Workaround for Youtube throttling the download speed.
|
2020-06-20 23:44:20 +00:00
|
|
|
$body = new YoutubeStream($this->downloader, $this->video);
|
2019-04-22 18:20:04 +00:00
|
|
|
} else {
|
|
|
|
$body = $stream->getBody();
|
|
|
|
}
|
2019-04-22 14:05:58 +00:00
|
|
|
}
|
|
|
|
if ($request->isGet()) {
|
|
|
|
$response = $response->withBody($body);
|
|
|
|
}
|
|
|
|
$response = $response->withHeader(
|
|
|
|
'Content-Disposition',
|
2019-10-03 19:24:12 +00:00
|
|
|
'attachment; filename="' .
|
2020-06-20 23:44:20 +00:00
|
|
|
$this->video->getFilename() . '"'
|
2019-04-22 14:05:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a remuxed stream piped through the server.
|
|
|
|
*
|
2020-05-13 19:18:32 +00:00
|
|
|
* @param Request $request PSR-7 request
|
2019-04-22 14:05:58 +00:00
|
|
|
*
|
2020-06-20 23:44:20 +00:00
|
|
|
* @param Response $response PSR-7 response
|
2019-04-22 14:05:58 +00:00
|
|
|
* @return Response HTTP response
|
2020-06-20 23:44:20 +00:00
|
|
|
* @throws AlltubeLibraryException
|
2019-04-22 14:05:58 +00:00
|
|
|
*/
|
|
|
|
private function getRemuxStream(Request $request, Response $response)
|
|
|
|
{
|
|
|
|
if (!$this->config->remux) {
|
2020-06-20 23:44:20 +00:00
|
|
|
throw new RemuxException('You need to enable remux mode to merge two formats.');
|
2019-04-22 14:05:58 +00:00
|
|
|
}
|
2020-06-20 23:44:20 +00:00
|
|
|
$stream = $this->downloader->getRemuxStream($this->video);
|
2019-04-22 14:05:58 +00:00
|
|
|
$response = $response->withHeader('Content-Type', 'video/x-matroska');
|
|
|
|
if ($request->isGet()) {
|
|
|
|
$response = $response->withBody(new Stream($stream));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response->withHeader(
|
|
|
|
'Content-Disposition',
|
2019-10-31 12:41:16 +00:00
|
|
|
'attachment; filename="' . $this->video->getFileNameWithExtension('mkv') . '"'
|
2019-04-22 14:05:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get approriate HTTP response to download query.
|
|
|
|
* Depends on whether we want to stream, remux or simply redirect.
|
|
|
|
*
|
2020-05-13 19:18:32 +00:00
|
|
|
* @param Request $request PSR-7 request
|
2019-04-22 14:05:58 +00:00
|
|
|
*
|
2020-05-13 19:18:32 +00:00
|
|
|
* @param Response $response PSR-7 response
|
2019-04-22 14:05:58 +00:00
|
|
|
* @return Response HTTP response
|
2020-06-20 23:44:20 +00:00
|
|
|
* @throws AlltubeLibraryException
|
2019-04-22 14:05:58 +00:00
|
|
|
*/
|
|
|
|
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);
|
2019-04-27 22:25:03 +00:00
|
|
|
} elseif ($this->config->stream && (isset($this->video->entries) || $request->getQueryParam('stream'))) {
|
2019-04-22 14:05:58 +00:00
|
|
|
return $this->getStream($request, $response);
|
|
|
|
} else {
|
|
|
|
if (empty($videoUrls[0])) {
|
2020-06-20 23:44:20 +00:00
|
|
|
throw new EmptyUrlException("Can't find URL of video.");
|
2019-04-22 14:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $response->withRedirect($videoUrls[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a converted video file.
|
|
|
|
*
|
2020-05-13 19:18:32 +00:00
|
|
|
* @param Request $request PSR-7 request
|
2019-04-22 14:05:58 +00:00
|
|
|
* @param Response $response PSR-7 response
|
|
|
|
*
|
|
|
|
* @return Response HTTP response
|
2020-06-20 23:44:20 +00:00
|
|
|
* @throws AlltubeLibraryException
|
|
|
|
* @throws InvalidProtocolConversionException
|
2020-05-13 19:18:32 +00:00
|
|
|
* @throws PasswordException
|
2020-06-20 23:44:20 +00:00
|
|
|
* @throws PlaylistConversionException
|
|
|
|
* @throws WrongPasswordException
|
|
|
|
* @throws YoutubedlException
|
|
|
|
* @throws PopenStreamException
|
2019-04-22 14:05:58 +00:00
|
|
|
*/
|
|
|
|
private function getConvertedResponse(Request $request, Response $response)
|
|
|
|
{
|
|
|
|
$response = $response->withHeader(
|
|
|
|
'Content-Disposition',
|
2019-10-03 19:24:12 +00:00
|
|
|
'attachment; filename="' .
|
|
|
|
$this->video->getFileNameWithExtension($request->getQueryParam('customFormat')) . '"'
|
2019-04-22 14:05:58 +00:00
|
|
|
);
|
2019-10-03 19:24:12 +00:00
|
|
|
$response = $response->withHeader('Content-Type', 'video/' . $request->getQueryParam('customFormat'));
|
2019-04-22 14:05:58 +00:00
|
|
|
|
|
|
|
if ($request->isGet() || $request->isPost()) {
|
2020-06-20 23:44:20 +00:00
|
|
|
$process = $this->downloader->getConvertedStream(
|
|
|
|
$this->video,
|
2019-04-22 14:05:58 +00:00
|
|
|
$request->getQueryParam('customBitrate'),
|
|
|
|
$request->getQueryParam('customFormat')
|
|
|
|
);
|
|
|
|
$response = $response->withBody(new Stream($process));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
}
|