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-beta2'
Browse files Browse the repository at this point in the history
  • Loading branch information
Rudloff committed Oct 19, 2020
2 parents 7f934ae + f29a61f commit e975739
Show file tree
Hide file tree
Showing 57 changed files with 3,042 additions and 1,981 deletions.
3 changes: 1 addition & 2 deletions .htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,5 @@ FileETag None
Header set X-Content-Type-Options nosniff
Header set X-XSS-Protection "1; mode=block"
Header set Referrer-Policy no-referrer
Header set Content-Security-Policy "default-src 'self'; object-src 'none'; script-src 'none'; style-src 'self' 'unsafe-inline'; img-src http:"
Header add Link "</css/fonts.css>; rel=preload, </css/style.css>; rel=preload" "expr=%{CONTENT_TYPE} =~ m#text/html#"
Header add Link "</css/style.css>; rel=preload" "expr=%{CONTENT_TYPE} =~ m#text/html#"
</ifmodule>
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ You will need PHP 7.2 (or higher) and the following PHP modules:

## Web server configuration

If you want to serve the application under a basepath and/or with a different internal than external port (scenario: nginx->docker setup) Alltube supports the following X-Forwarded headers:

* X-Forwarded-Host (ex. `another.domain.com`)
* X-Forwarded-Path (ex: `/alltube`)
* X-Forwarded-Port (ex: `5555`)

### Apache

The following modules are recommended:
Expand Down
46 changes: 0 additions & 46 deletions RoboFile.php

This file was deleted.

74 changes: 25 additions & 49 deletions classes/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Alltube\Library\Downloader;
use Jawira\CaseConverter\CaseConverterException;
use Jean85\PrettyVersions;
use PackageVersions\Versions;
use Symfony\Component\ErrorHandler\Debug;
use Symfony\Component\Yaml\Yaml;
use Jawira\CaseConverter\Convert;
Expand All @@ -20,12 +19,6 @@
*/
class Config
{
/**
* Singleton instance.
*
* @var Config|null
*/
private static $instance;

/**
* youtube-dl binary path.
Expand Down Expand Up @@ -140,17 +133,32 @@ class Config
*/
public $debug = false;

/**
* Default to audio.
*
* @var bool
*/
public $defaultAudio = false;

/**
* Disable audio conversion from/to seeker.
*
* @var bool
*/
public $convertSeek = true;

/**
* Config constructor.
*
* @param mixed[] $options Options
* @throws ConfigException
*/
private function __construct(array $options = [])
public function __construct(array $options = [])
{
$this->applyOptions($options);
$this->getEnv();
$localeManager = LocaleManager::getInstance();
$this->validateOptions();
$localeManager = new LocaleManager();

if (empty($this->genericFormats)) {
// We don't put this in the class definition so it can be detected by xgettext.
Expand Down Expand Up @@ -185,7 +193,7 @@ private function __construct(array $options = [])
*
* @return string
*/
public static function addHttpToFormat($format)
public static function addHttpToFormat(string $format)
{
$newFormat = [];
foreach (explode('/', $format) as $subformat) {
Expand Down Expand Up @@ -257,33 +265,17 @@ private function getEnv()
}
}

/**
* Get Config singleton instance.
*
* @return Config
*/
public static function getInstance()
{
if (!isset(self::$instance)) {
self::$instance = new self();
}

return self::$instance;
}

/**
* Set options from a YAML file.
*
* @param string $file Path to the YAML file
* @return void
* @return Config
* @throws ConfigException
*/
public static function setFile($file)
public static function fromFile(string $file)
{
if (is_file($file)) {
$options = Yaml::parse(strval(file_get_contents($file)));
self::$instance = new self($options);
self::$instance->validateOptions();
return new self(Yaml::parse(strval(file_get_contents($file))));
} else {
throw new ConfigException("Can't find config file at " . $file);
}
Expand All @@ -293,29 +285,13 @@ public static function setFile($file)
* Manually set some options.
*
* @param mixed[] $options Options (see `config/config.example.yml` for available options)
* @param bool $update True to update an existing instance
* @return void
* @throws ConfigException
*/
public static function setOptions(array $options, $update = true)
public function setOptions(array $options)
{
if ($update) {
$config = self::getInstance();
$config->applyOptions($options);
$config->validateOptions();
} else {
self::$instance = new self($options);
}
}

/**
* Destroy singleton instance.
*
* @return void
*/
public static function destroyInstance()
{
self::$instance = null;
$this->applyOptions($options);
$this->validateOptions();
}

/**
Expand All @@ -340,7 +316,7 @@ public function getDownloader()
*/
public function getAppVersion()
{
$version = PrettyVersions::getVersion(Versions::ROOT_PACKAGE_NAME);
$version = PrettyVersions::getRootPackageVersion();

return $version->getPrettyVersion();
}
Expand Down
41 changes: 41 additions & 0 deletions classes/ConfigFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Alltube;

use Slim\Container;
use Symfony\Component\ErrorHandler\Debug;

/**
* Class ConfigFactory
* @package Alltube
*/
class ConfigFactory
{

/**
* @return Config
* @throws Exception\ConfigException
*/
public static function create(Container $container)
{
$configPath = __DIR__ . '/../config/config.yml';
if (is_file($configPath)) {
$config = Config::fromFile($configPath);
} else {
$config = new Config();
}
if ($config->uglyUrls) {
$container['router'] = new UglyRouter();
}
if ($config->debug) {
/*
We want to enable this as soon as possible,
in order to catch errors that are thrown
before the Slim error handler is ready.
*/
Debug::enable();
}

return $config;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Alltube\LocaleManager;
use Alltube\SessionManager;
use Aura\Session\Segment;
use Consolidation\Log\Logger;
use Psr\Container\ContainerInterface;
use Slim\Http\Request;
use Slim\Http\Response;
Expand Down Expand Up @@ -70,19 +71,26 @@ abstract class BaseController
*/
protected $downloader;

/**
* @var Logger
*/
protected $logger;

/**
* BaseController constructor.
*
* @param ContainerInterface $container Slim dependency container
*/
public function __construct(ContainerInterface $container)
{
$this->config = Config::getInstance();
$this->config = $container->get('config');
$this->container = $container;
$session = SessionManager::getSession();
$this->sessionSegment = $session->getSegment(self::class);
$this->localeManager = $this->container->get('locale');
$this->downloader = $this->config->getDownloader();
$this->logger = $this->container->get('logger');
$this->downloader->setLogger($this->logger);

if (!$this->config->stream) {
// Force HTTP if stream is not enabled.
Expand Down Expand Up @@ -137,7 +145,7 @@ protected function getPassword(Request $request)
*
* @return Response HTTP response
*/
protected function displayError(Request $request, Response $response, $message)
protected function displayError(Request $request, Response $response, string $message)
{
$controller = new FrontController($this->container);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Alltube\Stream\YoutubeStream;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Http\StatusCode;
use Slim\Http\Stream;

/**
Expand Down Expand Up @@ -100,8 +101,12 @@ public function download(Request $request, Response $response)
*/
private function getConvertedAudioResponse(Request $request, Response $response)
{
$from = $request->getQueryParam('from');
$to = $request->getQueryParam('to');
$from = null;
$to = null;
if ($this->config->convertSeek) {
$from = $request->getQueryParam('from');
$to = $request->getQueryParam('to');
}

$response = $response->withHeader(
'Content-Disposition',
Expand Down Expand Up @@ -203,8 +208,8 @@ private function getStream(Request $request, Response $response)
$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 ($stream->getStatusCode() == StatusCode::HTTP_PARTIAL_CONTENT) {
$response = $response->withStatus(StatusCode::HTTP_PARTIAL_CONTENT);
}

if (isset($this->video->downloader_options->http_chunk_size)) {
Expand Down
Loading

0 comments on commit e975739

Please sign in to comment.