Internal: Add Twig function to get base64-encoded asset - refs BT#21621

pull/5654/head
Angel Fernando Quiroz Campos 5 months ago
parent 4234e363ed
commit ab8638caff
No known key found for this signature in database
GPG Key ID: B284841AE3E562CD
  1. 43
      src/CoreBundle/ServiceHelper/ThemeHelper.php
  2. 10
      src/CoreBundle/Twig/Extension/ChamiloExtension.php

@ -10,6 +10,8 @@ use Chamilo\CoreBundle\Settings\SettingsManager;
use Chamilo\CourseBundle\Settings\SettingsCourseManager;
use League\Flysystem\FilesystemException;
use League\Flysystem\FilesystemOperator;
use League\Flysystem\UnableToReadFile;
use League\MimeTypeDetection\ExtensionMimeTypeDetector;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
@ -71,7 +73,7 @@ final class ThemeHelper
return $visualTheme;
}
public function getThemeAssetUrl(string $path, bool $absolute = false): string
public function getThemeAssetUrl(string $path, bool $absoluteUrl = false): string
{
$themeName = $this->getVisualTheme();
@ -86,7 +88,7 @@ final class ThemeHelper
return $this->router->generate(
'theme_asset',
['name' => $themeName, 'path' => $path],
$absolute ? UrlGeneratorInterface::ABSOLUTE_URL : UrlGeneratorInterface::ABSOLUTE_PATH
$absoluteUrl ? UrlGeneratorInterface::ABSOLUTE_URL : UrlGeneratorInterface::ABSOLUTE_PATH
);
}
@ -100,4 +102,41 @@ final class ThemeHelper
return sprintf('<link rel="stylesheet" href="%s">', $url);
}
public function getAssetContents(string $path): string
{
$themeName = $this->getVisualTheme();
$fullPath = $themeName.DIRECTORY_SEPARATOR.$path;
try {
if ($this->filesystem->fileExists($fullPath)) {
$stream = $this->filesystem->readStream($fullPath);
return stream_get_contents($stream);
}
} catch (FilesystemException|UnableToReadFile) {
return '';
}
return '';
}
public function getAssetBase64Encoded(string $path): string
{
$visualTheme = $this->getVisualTheme();
$fullPath = $visualTheme.DIRECTORY_SEPARATOR.$path;
try {
if ($this->filesystem->fileExists($fullPath)) {
$detector = new ExtensionMimeTypeDetector();
$mimeType = (string) $detector->detectMimeTypeFromFile($fullPath);
return 'data:'.$mimeType.';base64,'.base64_encode($this->getAssetContents($path));
}
} catch (FilesystemException) {
return '';
}
return '';
}
}

@ -74,6 +74,7 @@ class ChamiloExtension extends AbstractExtension
new TwigFunction('password_checker_js', [$this, 'getPasswordCheckerJs'], ['is_safe' => ['html']]),
new TwigFunction('theme_asset', $this->getThemeAssetUrl(...)),
new TwigFunction('theme_asset_link_tag', $this->getThemeAssetLinkTag(...), ['is_safe' => ['html']]),
new TwigFunction('theme_asset_base64', $this->getThemeAssetBase64Encoded(...)),
];
}
@ -228,13 +229,18 @@ class ChamiloExtension extends AbstractExtension
return 'chamilo_extension';
}
public function getThemeAssetUrl(string $path, bool $absolute = false): string
public function getThemeAssetUrl(string $path, bool $absoluteUrl = false): string
{
return $this->themeHelper->getThemeAssetUrl($path, $absolute);
return $this->themeHelper->getThemeAssetUrl($path, $absoluteUrl);
}
public function getThemeAssetLinkTag(string $path, bool $absoluteUrl = false): string
{
return $this->themeHelper->getThemeAssetLinkTag($path, $absoluteUrl);
}
public function getThemeAssetBase64Encoded(string $path): string
{
return $this->themeHelper->getAssetBase64Encoded($path);
}
}

Loading…
Cancel
Save