fix: improve stability and type-safety in FrontController
- Added assertion to ensure 'view' is an instance of Smarty to avoid potential type errors. - Included plugin registration for 'filter_var' modifier to enhance view capabilities. - Added a check for successful file_get_contents call to prevent potential errors when fetching thumbnails. - Incorporated type assertion for 'projectDir' to enhance type safety and eliminate potential runtime errors.
This commit is contained in:
parent
c88a11f35f
commit
2b40b1b786
1 changed files with 26 additions and 13 deletions
|
@ -13,6 +13,7 @@ use Alltube\Locale;
|
||||||
use Alltube\Middleware\CspMiddleware;
|
use Alltube\Middleware\CspMiddleware;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Exception\InvalidURLException;
|
use Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Exception\InvalidURLException;
|
||||||
|
use GrumPHP\Util\Str;
|
||||||
use Slim\Http\StatusCode;
|
use Slim\Http\StatusCode;
|
||||||
use Slim\Http\Uri;
|
use Slim\Http\Uri;
|
||||||
use stdClass;
|
use stdClass;
|
||||||
|
@ -44,7 +45,12 @@ class FrontController extends BaseController
|
||||||
{
|
{
|
||||||
parent::__construct($container);
|
parent::__construct($container);
|
||||||
|
|
||||||
$this->view = $this->container->get('view');
|
$view = $this->container->get('view');
|
||||||
|
assert($view instanceof Smarty);
|
||||||
|
$this->view = $view;
|
||||||
|
|
||||||
|
// Plugins
|
||||||
|
$this->view->registerPlugin('modifier', 'filter_var', 'filter_var');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -226,19 +232,22 @@ class FrontController extends BaseController
|
||||||
if (isset($this->video->thumbnail) && $this->video->thumbnail !== '') {
|
if (isset($this->video->thumbnail) && $this->video->thumbnail !== '') {
|
||||||
/* Fetch the thumbnail */
|
/* Fetch the thumbnail */
|
||||||
$thumbnailData = file_get_contents($this->video->thumbnail);
|
$thumbnailData = file_get_contents($this->video->thumbnail);
|
||||||
$thumbnailData = base64_encode($thumbnailData);
|
|
||||||
/* Guess the mime type */
|
|
||||||
$thumbnailMime = 'image/jpeg';
|
|
||||||
|
|
||||||
if (strpos($this->video->thumbnail, '.png') !== false) {
|
if ($thumbnailData !== false) {
|
||||||
$thumbnailMime = 'image/png';
|
$thumbnailData = base64_encode($thumbnailData);
|
||||||
} elseif (strpos($this->video->thumbnail, '.gif') !== false) {
|
/* Guess the mime type */
|
||||||
$thumbnailMime = 'image/gif';
|
$thumbnailMime = 'image/jpeg';
|
||||||
} elseif (strpos($this->video->thumbnail, '.webp') !== false) {
|
|
||||||
$thumbnailMime = 'image/webp';
|
if (strpos($this->video->thumbnail, '.png') !== false) {
|
||||||
|
$thumbnailMime = 'image/png';
|
||||||
|
} elseif (strpos($this->video->thumbnail, '.gif') !== false) {
|
||||||
|
$thumbnailMime = 'image/gif';
|
||||||
|
} elseif (strpos($this->video->thumbnail, '.webp') !== false) {
|
||||||
|
$thumbnailMime = 'image/webp';
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->video->thumbnail = 'data:' . $thumbnailMime . ';base64,' . $thumbnailData;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->video->thumbnail = 'data:' . $thumbnailMime . ';base64,' . $thumbnailData;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->view->render(
|
$this->view->render(
|
||||||
|
@ -349,7 +358,11 @@ class FrontController extends BaseController
|
||||||
$response = $cspMiddleware->applyHeader($response);
|
$response = $cspMiddleware->applyHeader($response);
|
||||||
|
|
||||||
if ($this->config->debug) {
|
if ($this->config->debug) {
|
||||||
$renderer = new HtmlErrorRenderer(true, null, null, $this->container->get('root_path'));
|
$projectDir = $this->container->get('root_path');
|
||||||
|
|
||||||
|
assert(is_string($projectDir) || is_null($projectDir));
|
||||||
|
|
||||||
|
$renderer = new HtmlErrorRenderer(true, null, null, $projectDir);
|
||||||
$exception = $renderer->render($error);
|
$exception = $renderer->render($error);
|
||||||
|
|
||||||
$response->getBody()->write($exception->getAsString());
|
$response->getBody()->write($exception->getAsString());
|
||||||
|
|
Loading…
Reference in a new issue