Add Glide for Assets

pull/3924/head
Julio Montoya 3 years ago
parent ce386d40b8
commit 13dfb34dd1
  1. 7
      config/packages/oneup_flysystem.yaml
  2. 5
      config/services.yaml
  3. 1
      ecs.php
  4. 5
      public/main/inc/lib/extra_field.lib.php
  5. 11
      src/CoreBundle/Component/Utils/GlideAsset.php
  6. 31
      src/CoreBundle/Controller/AssetController.php
  7. 2
      src/CoreBundle/Controller/ResourceController.php
  8. 27
      src/CoreBundle/Repository/AssetRepository.php
  9. 3
      src/CoreBundle/Serializer/ResourceNormalizer.php

@ -3,6 +3,9 @@ oneup_flysystem:
asset_adapter:
local:
location: '%kernel.project_dir%/var/upload/assets'
asset_cache_adapter:
local:
location: '%kernel.project_dir%/var/cache/assets'
resource_adapter:
local:
location: '%kernel.project_dir%/var/upload/resource'
@ -13,6 +16,10 @@ oneup_flysystem:
asset:
adapter: asset_adapter
mount: asset
asset_cache:
adapter: asset_cache_adapter
mount: asset_cache
resource:
adapter: resource_adapter
mount: resource

@ -58,6 +58,11 @@ services:
- {source: '@oneup_flysystem.resource_filesystem', cache: '@oneup_flysystem.resource_cache_filesystem'}
- '%glide_media_filters%'
Chamilo\CoreBundle\Component\Utils\GlideAsset:
arguments:
- { source: '@oneup_flysystem.asset_filesystem', cache: '@oneup_flysystem.asset_cache_filesystem' }
- '%glide_media_filters%'
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name

@ -115,6 +115,7 @@ return static function (ContainerConfigurator $containerConfigurator): void {
PhpCsFixer\Fixer\StringNotation\ExplicitStringVariableFixer::class,
WrapEncapsedVariableInCurlyBracesRector::class,
ClassAttributesSeparationFixer::class,
\PhpCsFixer\Fixer\FunctionNotation\FopenFlagsFixer::class,
//UnusedVariableSniff::class . '.ignoreUnusedValuesWhenOnlyKeysAreUsedInForeach' => true,
//UnusedVariableSniff::class => 'ignoreUnusedValuesWhenOnlyKeysAreUsedInForeach',
]

@ -2975,6 +2975,7 @@ JAVASCRIPT;
}
$assetId = $valueData['value'];
$assetRepo = Container::getAssetRepository();
/** @var Asset $asset */
$asset = $assetRepo->find($assetId);
if (null === $asset) {
@ -2982,11 +2983,13 @@ JAVASCRIPT;
}
$url = $assetRepo->getAssetUrl($asset);
if (self::FIELD_TYPE_FILE_IMAGE === $fieldType) {
$image = Display::img(
$url,
$field['display_text'],
['width' => '300']
['width' => '300'],
false
);
$displayedValue = Display::url(
$image,

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Component\Utils;
class GlideAsset extends Glide
{
}

@ -6,11 +6,15 @@ declare(strict_types=1);
namespace Chamilo\CoreBundle\Controller;
use Chamilo\CoreBundle\Component\Utils\Glide;
use Chamilo\CoreBundle\Component\Utils\GlideAsset;
use Chamilo\CoreBundle\Repository\AssetRepository;
use Chamilo\CoreBundle\Traits\ControllerTrait;
use Chamilo\CoreBundle\Traits\CourseControllerTrait;
use Chamilo\CoreBundle\Traits\ResourceControllerTrait;
use League\MimeTypeDetection\ExtensionMimeTypeDetector;
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\Annotation\Route;
@ -27,17 +31,40 @@ class AssetController
/**
* @Route("/{category}/{path}", methods={"GET"}, requirements={"path"=".+"}, name="chamilo_core_asset_showfile")
*/
public function showFile($category, $path, AssetRepository $assetRepository)
public function showFile($category, $path, AssetRepository $assetRepository, GlideAsset $glide, RequestStack $requestStack)
{
$filePath = $category.'/'.$path;
$exists = $assetRepository->getFileSystem()->fileExists($filePath);
if ($exists) {
$fileName = basename($filePath);
$detector = new ExtensionMimeTypeDetector();
$mimeType = $detector->detectMimeTypeFromFile($filePath);
// If image use glide, because why not.
if (false !== strpos($mimeType, 'image')) {
$server = $glide->getServer();
$request = $requestStack->getCurrentRequest();
$params = $request->query->all();
// The filter overwrites the params from GET.
/*if (!empty($filter)) {
$params = $glide->getFilters()[$filter] ?? [];
}*/
// The image was cropped manually by the user, so we force to render this version,
// no matter other crop parameters.
//$crop = $resourceFile->getCrop();
/*if (!empty($crop)) {
$params['crop'] = $crop;
}*/
return $server->getImageResponse($filePath, $params);
}
$stream = $assetRepository->getFileSystem()->readStream($filePath);
$response = new StreamedResponse(
function () use ($stream): void {
stream_copy_to_stream($stream, fopen('php://output', 'w'));
stream_copy_to_stream($stream, fopen('php://output', 'wb'));
}
);
$disposition = $response->headers->makeDisposition(

@ -740,7 +740,7 @@ class ResourceController extends AbstractResourceController implements CourseCon
$response = new StreamedResponse(
function () use ($stream): void {
stream_copy_to_stream($stream, fopen('php://output', 'w'));
stream_copy_to_stream($stream, fopen('php://output', 'wb'));
}
);
$disposition = $response->headers->makeDisposition(

@ -13,6 +13,7 @@ use League\Flysystem\FilesystemOperator;
use PhpZip\ZipFile;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Routing\RouterInterface;
use Vich\UploaderBundle\Storage\FlysystemStorage;
use Vich\UploaderBundle\Templating\Helper\UploaderHelper;
class AssetRepository extends ServiceEntityRepository
@ -20,14 +21,21 @@ class AssetRepository extends ServiceEntityRepository
protected RouterInterface $router;
protected UploaderHelper $uploaderHelper;
protected FilesystemOperator $filesystem;
protected FlysystemStorage $storage;
public function __construct(ManagerRegistry $registry, RouterInterface $router, UploaderHelper $uploaderHelper, FilesystemOperator $assetFilesystem)
public function __construct(ManagerRegistry $registry, RouterInterface $router, UploaderHelper $uploaderHelper, FilesystemOperator $assetFilesystem, FlysystemStorage $storage)
{
parent::__construct($registry, Asset::class);
$this->router = $router;
$this->uploaderHelper = $uploaderHelper;
// Flysystem mount name is saved in config/packages/oneup_flysystem.yaml
$this->filesystem = $assetFilesystem;
$this->storage = $storage;
}
public function getStorage()
{
return $this->storage;
}
public function getFileSystem()
@ -80,19 +88,24 @@ class AssetRepository extends ServiceEntityRepository
public function getAssetUrl(Asset $asset)
{
if (Asset::SCORM === $asset->getCategory()) {
return $this->router->generate(
'chamilo_core_asset_showfile',
[
$params = [
'category' => $asset->getCategory(),
'path' => $asset->getTitle(),
]
);
];
return $this->router->generate('chamilo_core_asset_showfile', $params);
}
// Classic.
$helper = $this->uploaderHelper;
return '/assets'.$helper->asset($asset);
$cropFilter = '';
$crop = $asset->getCrop();
if (!empty($crop)) {
$cropFilter = '?crop='.$crop;
}
return '/assets'.$helper->asset($asset).$cropFilter;
}
public function createFromRequest(Asset $asset, $file): Asset

@ -82,10 +82,9 @@ final class ResourceNormalizer implements ContextAwareNormalizerInterface, Norma
$object->contentUrl = $this->generator->generate('chamilo_core_resource_view', $params);
$object->downloadUrl = $this->generator->generate('chamilo_core_resource_download', $params);
error_log($object->getResourceName());
// Get illustration of a resource, instead of looking for the node children to get the illustration.
if ($object instanceof ResourceIllustrationInterface) {
error_log('check illustration');
$object->illustrationUrl = $this->illustrationRepository->getIllustrationUrl($object);
}

Loading…
Cancel
Save