Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-7852: Exposed maxFileSize values per each image Content Type #1194

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 0 additions & 55 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
77 changes: 54 additions & 23 deletions src/lib/UI/Config/Provider/ContentTypeMappings.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,49 @@
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<string, mixed> */
protected array $locationMappings = [];

/** @var array */
protected $fallbackContentType = [];
/** @var array<string, mixed> */
protected array $defaultMappings = [];

/** @var int */
protected $maxFileSize = 0;
/** @var array<string, mixed> */
protected array $fallbackContentType = [];

protected int $maxFileSize = 0;

/**
* @param array $locationMappings
* @param array $defaultMappings
* @param array $fallbackContentType
* @param int $maxFileSize
* @param array<string, mixed> $locationMappings
* @param array<string, mixed> $defaultMappings
* @param array<string, mixed> $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;
$this->maxFileSize = $maxFileSize;
}

/**
* Returns configuration structure compatible with PlatformUI.
* Returns configuration structure compatible with AdminUI.
*
* @return array
* @return array<string, mixed>
*/
public function getConfig(): array
{
Expand Down Expand Up @@ -78,33 +81,61 @@ public function getConfig(): array
}

/**
* @param array $mappingGroup
* @param array<string> $mappingGroup
*
* @return array
* @return array<string, mixed>
*/
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<string> $fallbackContentType
*
* @return array
* @return array<string, mixed>
*/
private function buildFallbackContentTypeStructure(array $fallbackContentType)
private function buildFallbackContentTypeStructure(array $fallbackContentType): array
{
return [
'contentTypeIdentifier' => $fallbackContentType['content_type_identifier'],
'contentFieldIdentifier' => $fallbackContentType['content_field_identifier'],
'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');
Loading