Skip to content
Open
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
21 changes: 21 additions & 0 deletions Plugin/ImageProcessorRestrictExtensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ class ImageProcessorRestrictExtensions
{
private const ALLOWED_EXTENSIONS = ['jpg', 'jpeg', 'gif', 'png'];

private const MIME_EXTENSION_MAP = [
'image/jpg' => 'jpg',
'image/jpeg' => 'jpg',
'image/gif' => 'gif',
'image/png' => 'png',
];

/**
* @var Uploader
*/
Expand All @@ -34,6 +41,13 @@ public function __construct(Uploader $uploader)
/**
* Before processImageContent, lock the uploader to image-only extensions.
*
* Magento core stores ImageContentInterface::NAME as the filename without
* extension when images are added programmatically (e.g. CLI import via
* Product::addImageToMediaGallery). In this case the Uploader would reject
* the file because an empty extension is not in the allowlist. We derive the
* extension from the MIME type — which Magento has already validated — so that
* both programmatic and form-based uploads pass the extension check.
*
* @param ImageProcessor $subject
* @param string $entityType
* @param ImageContentInterface $imageContent
Expand All @@ -46,6 +60,13 @@ public function beforeProcessImageContent(
$entityType,
$imageContent
) {
if (!pathinfo($imageContent->getName(), PATHINFO_EXTENSION)) {
$extension = self::MIME_EXTENSION_MAP[$imageContent->getType()] ?? null;
if ($extension) {
$imageContent->setName($imageContent->getName() . '.' . $extension);
}
}

$this->uploader->setAllowedExtensions(self::ALLOWED_EXTENSIONS);
return null;
}
Expand Down