Files
2026-09-15 10:35:56 +08:00

37 lines
1.0 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\ImageManager\Plugin\MediaGallery;
use Esun\ImageManager\Model\MediaUploader;
use Magento\Framework\App\Request\Http;
use Magento\MediaGalleryUi\Model\UploadImage;
class SanitizePngUploadPlugin
{
public function __construct(
private readonly Http $request,
private readonly MediaUploader $mediaUploader
) {
}
/**
* Sanitize the temporary file before Magento Media Gallery creates its
* uploader and invokes the environment-specific GD/libpng validator.
*
* @return array{0: string, 1: string}
*/
public function beforeExecute(UploadImage $subject, string $targetFolder, string $type): array
{
$file = $this->request->getFiles('image');
if (is_array($file)
&& !empty($file['tmp_name'])
&& (int) ($file['error'] ?? UPLOAD_ERR_NO_FILE) === UPLOAD_ERR_OK
) {
$this->mediaUploader->sanitizePngUpload((string) $file['tmp_name']);
}
return [$targetFolder, $type];
}
}