From 13dfb34dd12dffbc4eb3efe21e1a47e011f17b7f Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Sat, 12 Jun 2021 18:55:30 +0200 Subject: [PATCH] Add Glide for Assets --- config/packages/oneup_flysystem.yaml | 7 +++++ config/services.yaml | 5 +++ ecs.php | 1 + public/main/inc/lib/extra_field.lib.php | 5 ++- src/CoreBundle/Component/Utils/GlideAsset.php | 11 +++++++ src/CoreBundle/Controller/AssetController.php | 31 +++++++++++++++++-- .../Controller/ResourceController.php | 2 +- src/CoreBundle/Repository/AssetRepository.php | 31 +++++++++++++------ .../Serializer/ResourceNormalizer.php | 3 +- 9 files changed, 81 insertions(+), 15 deletions(-) create mode 100644 src/CoreBundle/Component/Utils/GlideAsset.php diff --git a/config/packages/oneup_flysystem.yaml b/config/packages/oneup_flysystem.yaml index bd7a172d8d..d66f4dcede 100644 --- a/config/packages/oneup_flysystem.yaml +++ b/config/packages/oneup_flysystem.yaml @@ -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 diff --git a/config/services.yaml b/config/services.yaml index 3d71825c67..1e88186ad3 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -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 diff --git a/ecs.php b/ecs.php index 53fca59ada..c0d638704a 100644 --- a/ecs.php +++ b/ecs.php @@ -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', ] diff --git a/public/main/inc/lib/extra_field.lib.php b/public/main/inc/lib/extra_field.lib.php index ed4d97f701..4768912902 100644 --- a/public/main/inc/lib/extra_field.lib.php +++ b/public/main/inc/lib/extra_field.lib.php @@ -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, diff --git a/src/CoreBundle/Component/Utils/GlideAsset.php b/src/CoreBundle/Component/Utils/GlideAsset.php new file mode 100644 index 0000000000..871979b2d2 --- /dev/null +++ b/src/CoreBundle/Component/Utils/GlideAsset.php @@ -0,0 +1,11 @@ +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( diff --git a/src/CoreBundle/Controller/ResourceController.php b/src/CoreBundle/Controller/ResourceController.php index 70fc4214f8..8ae4186820 100644 --- a/src/CoreBundle/Controller/ResourceController.php +++ b/src/CoreBundle/Controller/ResourceController.php @@ -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( diff --git a/src/CoreBundle/Repository/AssetRepository.php b/src/CoreBundle/Repository/AssetRepository.php index 330ae0eeb1..60459565f1 100644 --- a/src/CoreBundle/Repository/AssetRepository.php +++ b/src/CoreBundle/Repository/AssetRepository.php @@ -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', - [ - 'category' => $asset->getCategory(), - 'path' => $asset->getTitle(), - ] - ); + $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 diff --git a/src/CoreBundle/Serializer/ResourceNormalizer.php b/src/CoreBundle/Serializer/ResourceNormalizer.php index 1b1c6c518f..a7e0ab95d3 100644 --- a/src/CoreBundle/Serializer/ResourceNormalizer.php +++ b/src/CoreBundle/Serializer/ResourceNormalizer.php @@ -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); }