Skip to content

Commit

Permalink
IBX-6937: Changed maxFileSize value to float type for Image field type (
Browse files Browse the repository at this point in the history
#291)

For more details see https://issues.ibexa.co/browse/IBX-6937 and #291

Changed max file size value from integer to float (numeric) type for Image field type. Storing the setting has been moved from dataInt1 to dataFloat1 relevant relation's column.

Key changes:

* Changed maxFileSize value type to float

* [DB] Updated schema

* Added max file size unit to storage dataText1 column

* Adjusted integration tests

* Changed field to store max file size from dataInt1 to dataFloat1

* Adjusted clean data to store max file size in dataFloat1 column

* Dropped converting max file size from MB to KB

---------

Co-Authored-By: Maciej Kobus <[email protected]>
  • Loading branch information
ciastektk and webhdx authored Dec 6, 2023
1 parent 6190433 commit 1044c97
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 31 deletions.
4 changes: 2 additions & 2 deletions data/mysql/cleandata.sql
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ VALUES (1,'',2,0,0,0,0,255,0,0,0,'New article','','','','','ezstring',1,'title',
(0,'',4,0,0,0,0,7,10,0,0,'','^[^@]+$','','','','ezuser',12,'user_account',0,1,0,0,3,NULL,NULL,'a:2:{s:6:\"eng-GB\";s:12:\"User account\";s:16:\"always-available\";s:6:\"eng-GB\";}',0),
(1,'',5,0,0,0,0,150,0,0,0,'','','','',NULL,'ezstring',116,'name',0,1,1,0,1,NULL,NULL,'a:2:{s:6:\"eng-GB\";s:4:\"Name\";s:16:\"always-available\";s:6:\"eng-GB\";}',0),
(1,'',5,0,0,0,0,10,0,0,0,'','','','',NULL,'ezrichtext',117,'caption',0,0,1,0,2,NULL,NULL,'a:2:{s:6:\"eng-GB\";s:7:\"Caption\";s:16:\"always-available\";s:6:\"eng-GB\";}',0),
(1,'',5,0,0,0,0,10,0,0,0,'','','','',NULL,'ezimage',118,'image',0,0,0,1,3,NULL,NULL,'a:2:{s:6:\"eng-GB\";s:5:\"Image\";s:16:\"always-available\";s:6:\"eng-GB\";}',0),
(1,'',5,10.0,0,0,0,0,0,0,0,'MB','','','',NULL,'ezimage',118,'image',0,0,0,1,3,NULL,NULL,'a:2:{s:6:\"eng-GB\";s:5:\"Image\";s:16:\"always-available\";s:6:\"eng-GB\";}',0),
(1,'',1,NULL,NULL,NULL,NULL,5,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'ezrichtext',119,'short_description',0,0,1,0,3,'N;','a:0:{}','a:1:{s:6:\"eng-GB\";s:17:\"Short description\";}',0),
(1,'',2,0,0,0,0,10,0,0,0,'','','','','','ezrichtext',120,'intro',0,1,1,0,4,NULL,NULL,'a:2:{s:6:\"eng-GB\";s:5:\"Intro\";s:16:\"always-available\";s:6:\"eng-GB\";}',0),
(1,'',2,0,0,0,0,20,0,0,0,'','','','','','ezrichtext',121,'body',0,0,1,0,5,NULL,NULL,'a:2:{s:6:\"eng-GB\";s:4:\"Body\";s:16:\"always-available\";s:6:\"eng-GB\";}',0),
Expand All @@ -61,7 +61,7 @@ VALUES (1,'',2,0,0,0,0,255,0,0,0,'New article','','','','','ezstring',1,'title',
(1,'',1,NULL,NULL,NULL,NULL,100,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'ezstring',155,'short_name',0,0,1,0,2,'N;','a:0:{}','a:1:{s:6:\"eng-GB\";s:10:\"Short name\";}',0),
(1,'',1,NULL,NULL,NULL,NULL,20,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'ezrichtext',156,'description',0,0,1,0,4,'N;','a:0:{}','a:1:{s:6:\"eng-GB\";s:11:\"Description\";}',0),
(1,'',4,0,0,0,0,10,0,0,0,'','','','','','eztext',179,'signature',0,0,1,0,4,NULL,NULL,'a:2:{s:6:\"eng-GB\";s:9:\"Signature\";s:16:\"always-available\";s:6:\"eng-GB\";}',0),
(1,'',4,0,0,0,0,10,0,0,0,'','','','','','ezimage',180,'image',0,0,0,1,5,NULL,NULL,'a:2:{s:6:\"eng-GB\";s:5:\"Image\";s:16:\"always-available\";s:6:\"eng-GB\";}',0);
(1,'',4,10.0,0,0,0,0,0,0,0,'MB','','','','','ezimage',180,'image',0,0,0,1,5,NULL,NULL,'a:2:{s:6:\"eng-GB\";s:5:\"Image\";s:16:\"always-available\";s:6:\"eng-GB\";}',0);

INSERT INTO `ezcontentclass_classgroup` (`contentclass_id`, `contentclass_version`, `group_id`, `group_name`)
VALUES (1, 0, 1, 'Content'),
Expand Down
4 changes: 2 additions & 2 deletions data/postgresql/cleandata.sql
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ VALUES (1,'',2,0,0,0,0,255,0,0,0,'New article','','','','','ezstring',1,'title',
(0,'',4,0,0,0,0,7,10,0,0,'','^[^@]+$','','','','ezuser',12,'user_account',0,1,0,FALSE,3,NULL,NULL,'a:2:{s:6:"eng-GB";s:12:"User account";s:16:"always-available";s:6:"eng-GB";}',0),
(1,'',5,0,0,0,0,150,0,0,0,'','','','',NULL,'ezstring',116,'name',0,1,1,FALSE,1,NULL,NULL,'a:2:{s:6:"eng-GB";s:4:"Name";s:16:"always-available";s:6:"eng-GB";}',0),
(1,'',5,0,0,0,0,10,0,0,0,'','','','',NULL,'ezrichtext',117,'caption',0,0,1,FALSE,2,NULL,NULL,'a:2:{s:6:"eng-GB";s:7:"Caption";s:16:"always-available";s:6:"eng-GB";}',0),
(1,'',5,0,0,0,0,10,0,0,0,'','','','',NULL,'ezimage',118,'image',0,0,0,TRUE,3,NULL,NULL,'a:2:{s:6:"eng-GB";s:5:"Image";s:16:"always-available";s:6:"eng-GB";}',0),
(1,'',5,10.0,0,0,0,0,0,0,0,'MB','','','',NULL,'ezimage',118,'image',0,0,0,TRUE,3,NULL,NULL,'a:2:{s:6:"eng-GB";s:5:"Image";s:16:"always-available";s:6:"eng-GB";}',0),
(1,'',1,NULL,NULL,NULL,NULL,5,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'ezrichtext',119,'short_description',0,0,1,FALSE,3,'N;','a:0:{}','a:1:{s:6:"eng-GB";s:17:"Short description";}',0),
(1,'',2,0,0,0,0,10,0,0,0,'','','','','','ezrichtext',120,'intro',0,1,1,FALSE,4,NULL,NULL,'a:2:{s:6:"eng-GB";s:5:"Intro";s:16:"always-available";s:6:"eng-GB";}',0),
(1,'',2,0,0,0,0,20,0,0,0,'','','','','','ezrichtext',121,'body',0,0,1,FALSE,5,NULL,NULL,'a:2:{s:6:"eng-GB";s:4:"Body";s:16:"always-available";s:6:"eng-GB";}',0),
Expand All @@ -61,7 +61,7 @@ VALUES (1,'',2,0,0,0,0,255,0,0,0,'New article','','','','','ezstring',1,'title',
(1,'',1,NULL,NULL,NULL,NULL,100,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'ezstring',155,'short_name',0,0,1,FALSE,2,'N;','a:0:{}','a:1:{s:6:"eng-GB";s:10:"Short name";}',0),
(1,'',1,NULL,NULL,NULL,NULL,20,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'ezrichtext',156,'description',0,0,1,FALSE,4,'N;','a:0:{}','a:1:{s:6:"eng-GB";s:11:"Description";}',0),
(1,'',4,0,0,0,0,10,0,0,0,'','','','','','eztext',179,'signature',0,0,1,FALSE,4,NULL,NULL,'a:2:{s:6:"eng-GB";s:9:"Signature";s:16:"always-available";s:6:"eng-GB";}',0),
(1,'',4,0,0,0,0,10,0,0,0,'','','','','','ezimage',180,'image',0,0,0,TRUE,5,NULL,NULL,'a:2:{s:6:"eng-GB";s:5:"Image";s:16:"always-available";s:6:"eng-GB";}',0);
(1,'',4,10.0,0,0,0,0,0,0,0,'MB','','','','','ezimage',180,'image',0,0,0,TRUE,5,NULL,NULL,'a:2:{s:6:"eng-GB";s:5:"Image";s:16:"always-available";s:6:"eng-GB";}',0);

INSERT INTO "ezcontentclass_classgroup" ("contentclass_id", "contentclass_version", "group_id", "group_name")
VALUES (1,0,1,'Content'),
Expand Down
12 changes: 6 additions & 6 deletions src/lib/FieldType/Image/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Type extends FieldType implements TranslationContainerInterface
protected $validatorConfigurationSchema = [
'FileSizeValidator' => [
'maxFileSize' => [
'type' => 'int',
'type' => 'numeric',
'default' => null,
],
],
Expand Down Expand Up @@ -134,11 +134,11 @@ protected function checkValueStructure(BaseValue $value)
);
}

if (isset($value->fileSize) && (!is_int($value->fileSize) || $value->fileSize < 0)) {
if (isset($value->fileSize) && ((!is_int($value->fileSize) && !is_float($value->fileSize)) || $value->fileSize < 0)) {
throw new InvalidArgumentType(
'$value->fileSize',
'int',
$value->alternativeText
'numeric',
$value->fileSize
);
}

Expand Down Expand Up @@ -233,14 +233,14 @@ public function validateValidatorConfiguration($validatorConfiguration)
);
break;
}
if (!is_int($parameters['maxFileSize']) && $parameters['maxFileSize'] !== null) {
if (!is_numeric($parameters['maxFileSize']) && $parameters['maxFileSize'] !== null) {
$validationErrors[] = new ValidationError(
'Validator %validator% expects parameter %parameter% to be of %type%.',
null,
[
'%validator%' => $validatorIdentifier,
'%parameter%' => 'maxFileSize',
'%type%' => 'integer',
'%type%' => 'numeric',
],
"[$validatorIdentifier][maxFileSize]"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,9 @@ public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageField
{
$validators = $fieldDef->fieldTypeConstraints->validators;

$storageDef->dataInt1 = $validators['FileSizeValidator']['maxFileSize'] ?? 0;
$storageDef->dataFloat1 = $validators['FileSizeValidator']['maxFileSize'] ?? 0.0;
$storageDef->dataInt2 = (int)($validators['AlternativeTextValidator']['required'] ?? 0);
$storageDef->dataText1 = 'MB';
}

public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef): void
Expand All @@ -193,7 +194,7 @@ public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefin
[
'validators' => [
'FileSizeValidator' => [
'maxFileSize' => $storageDef->dataInt1 !== 0 ? $storageDef->dataInt1 : null,
'maxFileSize' => $storageDef->dataFloat1 !== 0.0 ? $storageDef->dataFloat1 : null,
],
'AlternativeTextValidator' => [
'required' => (bool)$storageDef->dataInt2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ public function getValidatorSchema()
return [
'FileSizeValidator' => [
'maxFileSize' => [
'type' => 'int',
'default' => false,
'type' => 'numeric',
'default' => null,
],
],
'AlternativeTextValidator' => [
Expand All @@ -134,7 +134,7 @@ public function getValidValidatorConfiguration()
{
return [
'FileSizeValidator' => [
'maxFileSize' => 2 * 1024 * 1024, // 2 MB
'maxFileSize' => 2.0,
],
'AlternativeTextValidator' => [
'required' => true,
Expand Down
Loading

0 comments on commit 1044c97

Please sign in to comment.