From 9f1cc26b4e2627b4b69a959037376b5e4fe49693 Mon Sep 17 00:00:00 2001 From: konradoboza Date: Wed, 28 Feb 2024 13:22:27 +0100 Subject: [PATCH] IBX-7852: Exposed `maxFileSize` values per each `image` Content Type --- phpstan-baseline.neon | 55 ------------- .../Config/Provider/ContentTypeMappings.php | 77 +++++++++++++------ 2 files changed, 54 insertions(+), 78 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 11ef8f679a..b552f30326 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -9280,61 +9280,6 @@ parameters: count: 1 path: src/lib/UI/Config/Provider/Autosave.php - - - message: "#^Method Ibexa\\\\AdminUi\\\\UI\\\\Config\\\\Provider\\\\ContentTypeMappings\\:\\:__construct\\(\\) has parameter \\$defaultMappings with no value type specified in iterable type array\\.$#" - count: 1 - path: src/lib/UI/Config/Provider/ContentTypeMappings.php - - - - message: "#^Method Ibexa\\\\AdminUi\\\\UI\\\\Config\\\\Provider\\\\ContentTypeMappings\\:\\:__construct\\(\\) has parameter \\$fallbackContentType with no value type specified in iterable type array\\.$#" - count: 1 - path: src/lib/UI/Config/Provider/ContentTypeMappings.php - - - - message: "#^Method Ibexa\\\\AdminUi\\\\UI\\\\Config\\\\Provider\\\\ContentTypeMappings\\:\\:__construct\\(\\) has parameter \\$locationMappings with no value type specified in iterable type array\\.$#" - count: 1 - path: src/lib/UI/Config/Provider/ContentTypeMappings.php - - - - message: "#^Method Ibexa\\\\AdminUi\\\\UI\\\\Config\\\\Provider\\\\ContentTypeMappings\\:\\:buildFallbackContentTypeStructure\\(\\) has parameter \\$fallbackContentType with no value type specified in iterable type array\\.$#" - count: 1 - path: src/lib/UI/Config/Provider/ContentTypeMappings.php - - - - message: "#^Method Ibexa\\\\AdminUi\\\\UI\\\\Config\\\\Provider\\\\ContentTypeMappings\\:\\:buildFallbackContentTypeStructure\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/lib/UI/Config/Provider/ContentTypeMappings.php - - - - message: "#^Method Ibexa\\\\AdminUi\\\\UI\\\\Config\\\\Provider\\\\ContentTypeMappings\\:\\:buildMappingGroupStructure\\(\\) has parameter \\$mappingGroup with no value type specified in iterable type array\\.$#" - count: 1 - path: src/lib/UI/Config/Provider/ContentTypeMappings.php - - - - message: "#^Method Ibexa\\\\AdminUi\\\\UI\\\\Config\\\\Provider\\\\ContentTypeMappings\\:\\:buildMappingGroupStructure\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/lib/UI/Config/Provider/ContentTypeMappings.php - - - - message: "#^Method Ibexa\\\\AdminUi\\\\UI\\\\Config\\\\Provider\\\\ContentTypeMappings\\:\\:getConfig\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/lib/UI/Config/Provider/ContentTypeMappings.php - - - - message: "#^Property Ibexa\\\\AdminUi\\\\UI\\\\Config\\\\Provider\\\\ContentTypeMappings\\:\\:\\$defaultMappings type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/lib/UI/Config/Provider/ContentTypeMappings.php - - - - message: "#^Property Ibexa\\\\AdminUi\\\\UI\\\\Config\\\\Provider\\\\ContentTypeMappings\\:\\:\\$fallbackContentType type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/lib/UI/Config/Provider/ContentTypeMappings.php - - - - message: "#^Property Ibexa\\\\AdminUi\\\\UI\\\\Config\\\\Provider\\\\ContentTypeMappings\\:\\:\\$locationMappings type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/lib/UI/Config/Provider/ContentTypeMappings.php - - message: "#^Offset 'dirname' does not exist on array\\{dirname\\?\\: string, basename\\: string, extension\\?\\: string, filename\\: string\\}\\.$#" count: 1 diff --git a/src/lib/UI/Config/Provider/ContentTypeMappings.php b/src/lib/UI/Config/Provider/ContentTypeMappings.php index f3a03624ec..9e15ddd7d6 100644 --- a/src/lib/UI/Config/Provider/ContentTypeMappings.php +++ b/src/lib/UI/Config/Provider/ContentTypeMappings.php @@ -8,36 +8,39 @@ namespace Ibexa\AdminUi\UI\Config\Provider; use Ibexa\Contracts\AdminUi\UI\Config\ProviderInterface; +use Ibexa\Contracts\Core\Repository\ContentTypeService; /** * Class responsible for generating PlatformUI configuration for Multi File Upload functionality. */ class ContentTypeMappings implements ProviderInterface { - /** @var array */ - protected $locationMappings = []; + private ContentTypeService $contentTypeService; - /** @var array */ - protected $defaultMappings = []; + /** @var array */ + protected array $locationMappings = []; - /** @var array */ - protected $fallbackContentType = []; + /** @var array */ + protected array $defaultMappings = []; - /** @var int */ - protected $maxFileSize = 0; + /** @var array */ + protected array $fallbackContentType = []; + + protected int $maxFileSize = 0; /** - * @param array $locationMappings - * @param array $defaultMappings - * @param array $fallbackContentType - * @param int $maxFileSize + * @param array $locationMappings + * @param array $defaultMappings + * @param array $fallbackContentType */ public function __construct( + ContentTypeService $contentTypeService, array $locationMappings, array $defaultMappings, array $fallbackContentType, - $maxFileSize + int $maxFileSize ) { + $this->contentTypeService = $contentTypeService; $this->locationMappings = $locationMappings; $this->defaultMappings = $defaultMappings; $this->fallbackContentType = $fallbackContentType; @@ -45,9 +48,9 @@ public function __construct( } /** - * Returns configuration structure compatible with PlatformUI. + * Returns configuration structure compatible with AdminUI. * - * @return array + * @return array */ public function getConfig(): array { @@ -78,26 +81,33 @@ public function getConfig(): array } /** - * @param array $mappingGroup + * @param array $mappingGroup * - * @return array + * @return array */ - private function buildMappingGroupStructure(array $mappingGroup) + private function buildMappingGroupStructure(array $mappingGroup): array { + $contentTypeIdentifier = $mappingGroup['content_type_identifier']; + $contentFieldIdentifier = $mappingGroup['content_field_identifier']; + return [ 'mimeTypes' => $mappingGroup['mime_types'], - 'contentTypeIdentifier' => $mappingGroup['content_type_identifier'], - 'contentFieldIdentifier' => $mappingGroup['content_field_identifier'], + 'contentTypeIdentifier' => $contentTypeIdentifier, + 'contentFieldIdentifier' => $contentFieldIdentifier, 'nameFieldIdentifier' => $mappingGroup['name_field_identifier'], + 'maxFileSize' => $this->getContentTypeConfiguredMaxFileSize( + $contentTypeIdentifier, + $contentFieldIdentifier + ), ]; } /** - * @param array $fallbackContentType + * @param array $fallbackContentType * - * @return array + * @return array */ - private function buildFallbackContentTypeStructure(array $fallbackContentType) + private function buildFallbackContentTypeStructure(array $fallbackContentType): array { return [ 'contentTypeIdentifier' => $fallbackContentType['content_type_identifier'], @@ -105,6 +115,27 @@ private function buildFallbackContentTypeStructure(array $fallbackContentType) 'nameFieldIdentifier' => $fallbackContentType['name_field_identifier'], ]; } + + private function getContentTypeConfiguredMaxFileSize( + string $contentTypeIdentifier, + string $imageFieldTypeIdentifier + ): int { + $contentType = $this->contentTypeService->loadContentTypeByIdentifier( + $contentTypeIdentifier + ); + + $imgFieldType = $contentType->getFieldDefinition($imageFieldTypeIdentifier); + if ($imgFieldType === null) { + return $this->maxFileSize; + } + + $validatorConfig = $imgFieldType->getValidatorConfiguration(); + if (isset($validatorConfig['FileSizeValidator']['maxFileSize'])) { + return (int)$validatorConfig['FileSizeValidator']['maxFileSize'] * 1024 * 1024; + } + + return $this->maxFileSize; + } } class_alias(ContentTypeMappings::class, 'EzSystems\EzPlatformAdminUi\UI\Config\Provider\ContentTypeMappings');