| @@ -5,9 +5,9 @@ | ||
| 5 | 5 | defined('ABSPATH') || exit; |
| 6 | 6 | |
| 7 | 7 | class FileUploader |
| 8 | 8 | { |
| 9 | - private const PHOTO_EXTENSIONS = ['jpg', 'png', 'webp', 'bmp', 'jfif', 'jpeg', 'avif']; | |
| 9 | + private const PHOTO_EXTENSIONS = ['jpg', 'png', 'webp', 'gif', 'bmp', 'jfif', 'jpeg', 'avif']; | |
| 10 | 10 | private const PHOTO_MAX_SIZE = 5 * 1024 * 1024; |
| 11 | 11 | |
| 12 | 12 | private const VIDEO_EXTENSIONS = ['mp4', 'mov', '3gp', 'm4v', 'mkv', 'flv', 'mpg', 'webm', 'mpeg', 'ts', 'avi', 'qt', 'm4a']; |
| 13 | 13 | private const VIDEO_MAX_SIZE = 120 * 1024 * 1024; |
| @@ -41,8 +41,9 @@ | ||
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | $pathToUpload = $preparedFile['path']; |
| 44 | 44 | $tmpFile = $preparedFile['tmpFile'] ?? null; |
| 45 | + $normalizedFile = null; | |
| 45 | 46 | |
| 46 | 47 | try { |
| 47 | 48 | if (!$this->checkFileSize($pathToUpload, $config['max_size'])) { |
| 48 | 49 | throw new \RuntimeException('حجم فایل ' . esc_html($config['error_label']) . ' بیش از حد مجاز است.'); |
| @@ -47,10 +48,21 @@ | ||
| 47 | 48 | if (!$this->checkFileSize($pathToUpload, $config['max_size'])) { |
| 48 | 49 | throw new \RuntimeException('حجم فایل ' . esc_html($config['error_label']) . ' بیش از حد مجاز است.'); |
| 49 | 50 | } |
| 50 | 51 | |
| 52 | + if ($config['file_type'] === 'product.photo') { | |
| 53 | + $normalizedFile = (new ImageFormatNormalizer())->normalize($pathToUpload, $config['max_size']); | |
| 54 | + if ($normalizedFile !== null) $pathToUpload = $normalizedFile; | |
| 55 | + } | |
| 56 | + | |
| 57 | + if (!$this->checkFileSize($pathToUpload, $config['max_size'])) { | |
| 58 | + throw new \RuntimeException('حجم فایل ' . esc_html($config['error_label']) . ' بیش از حد مجاز است.'); | |
| 59 | + } | |
| 60 | + | |
| 51 | 61 | $options = [ |
| 52 | - 'allowed_extensions' => $config['allowed_extensions'], | |
| 62 | + 'allowed_extensions' => $config['file_type'] === 'product.photo' | |
| 63 | + ? ImageFormatNormalizer::SUPPORTED_EXTENSIONS | |
| 64 | + : $config['allowed_extensions'], | |
| 53 | 65 | 'max_size' => $config['max_size'], |
| 54 | 66 | 'timeout' => $config['file_type'] === 'product.video' ? 600 : 120, |
| 55 | 67 | ]; |
| 56 | 68 | |
| @@ -61,8 +73,9 @@ | ||
| 61 | 73 | } |
| 62 | 74 | |
| 63 | 75 | return $mediaUploader->uploadPhoto($pathToUpload, $options); |
| 64 | 76 | } finally { |
| 77 | + if ($normalizedFile && file_exists($normalizedFile)) unlink($normalizedFile); | |
| 65 | 78 | if (!empty($preparedFile['isTemp']) && $tmpFile && file_exists($tmpFile)) unlink($tmpFile); |
| 66 | 79 | } |
| 67 | 80 | } |
| 68 | 81 | |