From 30c82a52fb82aef8bcacec64fc72d6f285c2d5e6 Mon Sep 17 00:00:00 2001 From: Andy Footner Date: Thu, 25 Jul 2024 16:46:10 +0200 Subject: [PATCH] chore: try removing patch altogether Refs: OPS-10638 --- ...pal--3008292-image-upload-validators.patch | 156 ------------------ composer.patches.json | 1 - 2 files changed, 157 deletions(-) delete mode 100644 PATCHES/core--drupal--3008292-image-upload-validators.patch diff --git a/PATCHES/core--drupal--3008292-image-upload-validators.patch b/PATCHES/core--drupal--3008292-image-upload-validators.patch deleted file mode 100644 index 5fee9421d..000000000 --- a/PATCHES/core--drupal--3008292-image-upload-validators.patch +++ /dev/null @@ -1,156 +0,0 @@ -Subject: [PATCH] Issue #3008292 by phenaproxima, Spokje, ameymudras, darvanen, R_H-L, kishor_kolekar, stefan.korn, StryKaizer, yogeshmpawar, akalam, anushrikumari, scott_euser, supermoos, jaskaran.nagra: ImageItem::getUploadValidators() should be the source of truth for validating uploaded images ---- -Index: core/modules/image/src/Plugin/Field/FieldType/ImageItem.php -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== -diff --git a/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php b/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php ---- a/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php (revision 8d6cde7902d5711a5b7b6058b15eb358114897a5) -+++ b/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php (date 1720639627803) -@@ -517,6 +517,38 @@ - $form_state->setValueForElement($element, $value); - } - -+ /** -+ * {@inheritdoc} -+ */ -+ public function getUploadValidators(): array { -+ $upload_validators = parent::getUploadValidators(); -+ // Always validate that the uploaded file is an image. -+ $upload_validators['FileIsImage'] = []; -+ -+ // If the image's resolution is constrained by the field settings, validate -+ // that too. -+ $min_resolution = $this->getSetting('min_resolution') ?: 0; -+ $max_resolution = $this->getSetting('max_resolution') ?: 0; -+ if ($min_resolution || $max_resolution) { -+ $upload_validators['FileImageDimensions'] = [ -+ 'maxDimensions' => $max_resolution, -+ 'minDimensions' => $min_resolution, -+ ]; -+ } -+ -+ if (isset($upload_validators['file_validate_extensions'])) { -+ $extensions = $this->getSetting('file_extensions'); -+ $supported_extensions = \Drupal::service('image.factory')->getSupportedExtensions(); -+ -+ // If using custom extension validation, ensure that the extensions are -+ // supported by the current image toolkit. Otherwise, validate against all -+ // toolkit supported extensions. -+ $extensions = !empty($extensions) ? array_intersect(explode(' ', $extensions), $supported_extensions) : $supported_extensions; -+ $upload_validators['FileExtension']['extensions'] = implode(' ', $extensions); -+ } -+ return $upload_validators; -+ } -+ - /** - * {@inheritdoc} - */ -Index: core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== -diff --git a/core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php b/core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php ---- a/core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php (revision 8d6cde7902d5711a5b7b6058b15eb358114897a5) -+++ b/core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php (date 1720639675923) -@@ -140,29 +140,8 @@ - */ - public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { - $element = parent::formElement($items, $delta, $element, $form, $form_state); -- -+ $element['#upload_validators'] = $items[$delta]->getUploadValidators(); - $field_settings = $this->getFieldSettings(); -- -- // Add image validation. -- $element['#upload_validators']['FileIsImage'] = []; -- -- // Add upload dimensions validation. -- if ($field_settings['max_resolution'] || $field_settings['min_resolution']) { -- $element['#upload_validators']['FileImageDimensions'] = [ -- 'maxDimensions' => $field_settings['max_resolution'], -- 'minDimensions' => $field_settings['min_resolution'], -- ]; -- } -- -- $extensions = $field_settings['file_extensions']; -- $supported_extensions = $this->imageFactory->getSupportedExtensions(); -- -- // If using custom extension validation, ensure that the extensions are -- // supported by the current image toolkit. Otherwise, validate against all -- // toolkit supported extensions. -- $extensions = !empty($extensions) ? array_intersect(explode(' ', $extensions), $supported_extensions) : $supported_extensions; -- $element['#upload_validators']['FileExtension']['extensions'] = implode(' ', $extensions); -- - // Add mobile device image capture acceptance. - $element['#accept'] = 'image/*'; - -Index: core/modules/media_library/src/Form/FileUploadForm.php -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== -diff --git a/core/modules/media_library/src/Form/FileUploadForm.php b/core/modules/media_library/src/Form/FileUploadForm.php ---- a/core/modules/media_library/src/Form/FileUploadForm.php (revision 8d6cde7902d5711a5b7b6058b15eb358114897a5) -+++ b/core/modules/media_library/src/Form/FileUploadForm.php (date 1720639731101) -@@ -350,9 +350,10 @@ - * A created file item. - */ - protected function createFileItem(MediaTypeInterface $media_type) { -- $field_definition = $media_type->getSource()->getSourceFieldDefinition($media_type); -- $data_definition = FieldItemDataDefinition::create($field_definition); -- return new FileItem($data_definition); -+ $data_definition = $media_type->getSource()->getSourceFieldDefinition($media_type) -+ ->getItemDefinition(); -+ $class = $data_definition->getClass(); -+ return new $class($data_definition); - } - - /** -Index: core/modules/file/src/Plugin/rest/resource/FileUploadResource.php -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== -diff --git a/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php b/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php ---- a/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php (revision 8d6cde7902d5711a5b7b6058b15eb358114897a5) -+++ b/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php (date 1720639862180) -@@ -7,6 +7,7 @@ - use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait; - use Drupal\Core\Entity\EntityFieldManagerInterface; - use Drupal\Core\Entity\EntityTypeManagerInterface; -+use Drupal\Core\Field\FieldDefinitionInterface; - use Drupal\Core\File\Event\FileUploadSanitizeNameEvent; - use Drupal\Core\File\Exception\FileException; - use Drupal\Core\File\Exception\FileExistsException; -@@ -352,6 +353,29 @@ - return $field_definition; - } - -+ /** -+ * Retrieves the upload validators for a field definition. -+ * -+ * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition -+ * The field definition for which to get validators. -+ * -+ * @return array -+ * An array suitable for passing to file_save_upload() or the file field -+ * element's '#upload_validators' property. -+ */ -+ protected function getUploadValidators(FieldDefinitionInterface $field_definition): array { -+ $item_definition = $field_definition->getItemDefinition(); -+ $class = $item_definition->getClass(); -+ /** @var \Drupal\file\Plugin\Field\FieldType\FileItem $item */ -+ $item = new $class($item_definition); -+ -+ $validators = $item->getUploadValidators(); -+ // Add in our check of the file name length. -+ $validators['FileNameLength'] = []; -+ -+ return $validators; -+ } -+ - /** - * Prepares the filename to strip out any malicious extensions. - * diff --git a/composer.patches.json b/composer.patches.json index 012466c8f..8a9b4c7b2 100644 --- a/composer.patches.json +++ b/composer.patches.json @@ -4,7 +4,6 @@ "Cron fails on amazon_ses when not configured": "PATCHES/amazon_ses-3417090-cron-queue-14.patch" }, "drupal/core" : { - "https://www.drupal.org/project/drupal/issues/3008292": "PATCHES/core--drupal--3008292-image-upload-validators.patch", "https://www.drupal.org/project/drupal/issues/3143617": "PATCHES/core--drupal--3143617-pager-parameter.patch", "https://www.drupal.org/project/drupal/issues/3418098": "PATCHES/core--drupal--3418098-php-mailer.patch" },