| @@ -1,17 +1,13 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace SyncBasalam\Services; |
| 4 | 4 | |
| 5 | -use SyncBasalam\Admin\Settings\SettingsConfig; | |
| 6 | -use SyncBasalam\Config\Endpoints; | |
| 7 | -use SyncBasalam\Logger\Logger; | |
| 8 | - | |
| 9 | 5 | defined('ABSPATH') || exit; |
| 10 | 6 | |
| 11 | 7 | class FileUploader |
| 12 | 8 | { |
| 13 | - private const PHOTO_EXTENSIONS = ['jpg', 'png', 'webp', 'bmp', 'jfif', 'jpeg', 'avif']; | |
| 9 | + private const PHOTO_EXTENSIONS = ['jpg', 'png', 'webp', 'gif', 'bmp', 'jfif', 'jpeg', 'avif']; | |
| 14 | 10 | private const PHOTO_MAX_SIZE = 5 * 1024 * 1024; |
| 15 | 11 | |
| 16 | 12 | private const VIDEO_EXTENSIONS = ['mp4', 'mov', '3gp', 'm4v', 'mkv', 'flv', 'mpg', 'webm', 'mpeg', 'ts', 'avi', 'qt', 'm4a']; |
| 17 | 13 | private const VIDEO_MAX_SIZE = 120 * 1024 * 1024; |
| @@ -17,9 +13,9 @@ | ||
| 17 | 13 | private const VIDEO_MAX_SIZE = 120 * 1024 * 1024; |
| 18 | 14 | |
| 19 | 15 | public function upload($filePath) |
| 20 | 16 | { |
| 21 | - return $this->doUpload($filePath, [ | |
| 17 | + return $this->uploadMedia($filePath, [ | |
| 22 | 18 | 'file_type' => 'product.photo', |
| 23 | 19 | 'allowed_extensions' => self::PHOTO_EXTENSIONS, |
| 24 | 20 | 'max_size' => self::PHOTO_MAX_SIZE, |
| 25 | 21 | 'error_label' => 'تصویر', |
| @@ -27,9 +23,9 @@ | ||
| 27 | 23 | } |
| 28 | 24 | |
| 29 | 25 | public function uploadVideo($filePath) |
| 30 | 26 | { |
| 31 | - return $this->doUpload($filePath, [ | |
| 27 | + return $this->uploadMedia($filePath, [ | |
| 32 | 28 | 'file_type' => 'product.video', |
| 33 | 29 | 'allowed_extensions' => self::VIDEO_EXTENSIONS, |
| 34 | 30 | 'max_size' => self::VIDEO_MAX_SIZE, |
| 35 | 31 | 'error_label' => 'ویدیو', |
| @@ -35,11 +31,11 @@ | ||
| 35 | 31 | 'error_label' => 'ویدیو', |
| 36 | 32 | ]); |
| 37 | 33 | } |
| 38 | 34 | |
| 39 | - private function doUpload(string $filePath, array $config): array | |
| 35 | + private function uploadMedia(string $filePath, array $config): array | |
| 40 | 36 | { |
| 41 | - $preparedFile = $this->prepare($filePath, $config['allowed_extensions']); | |
| 37 | + $preparedFile = $this->prepare($filePath, $config['allowed_extensions'], $config['max_size']); | |
| 42 | 38 | |
| 43 | 39 | if ($preparedFile === false) { |
| 44 | 40 | throw new \RuntimeException('فایل ' . esc_html($config['error_label']) . ' برای آپلود معتبر یا در دسترس نیست.'); |
| 45 | 41 | } |
| @@ -44,33 +40,47 @@ | ||
| 44 | 40 | throw new \RuntimeException('فایل ' . esc_html($config['error_label']) . ' برای آپلود معتبر یا در دسترس نیست.'); |
| 45 | 41 | } |
| 46 | 42 | |
| 47 | 43 | $pathToUpload = $preparedFile['path']; |
| 48 | - $isTemp = $preparedFile['isTemp']; | |
| 49 | 44 | $tmpFile = $preparedFile['tmpFile'] ?? null; |
| 45 | + $normalizedFile = null; | |
| 50 | 46 | |
| 51 | - if (!$this->checkFileSize($pathToUpload, $config['max_size'])) { | |
| 52 | - if ($isTemp && $tmpFile) unlink($tmpFile); | |
| 53 | - throw new \RuntimeException('حجم فایل ' . esc_html($config['error_label']) . ' بیش از حد مجاز است.'); | |
| 54 | - } | |
| 47 | + try { | |
| 48 | + if (!$this->checkFileSize($pathToUpload, $config['max_size'])) { | |
| 49 | + throw new \RuntimeException('حجم فایل ' . esc_html($config['error_label']) . ' بیش از حد مجاز است.'); | |
| 50 | + } | |
| 55 | 51 | |
| 56 | - $response = $this->uploadFileToBasalam($pathToUpload, $config); | |
| 52 | + if ($config['file_type'] === 'product.photo') { | |
| 53 | + $normalizedFile = (new ImageFormatNormalizer())->normalize($pathToUpload, $config['max_size']); | |
| 54 | + if ($normalizedFile !== null) $pathToUpload = $normalizedFile; | |
| 55 | + } | |
| 57 | 56 | |
| 58 | - if ($isTemp && $tmpFile) unlink($tmpFile); | |
| 57 | + if (!$this->checkFileSize($pathToUpload, $config['max_size'])) { | |
| 58 | + throw new \RuntimeException('حجم فایل ' . esc_html($config['error_label']) . ' بیش از حد مجاز است.'); | |
| 59 | + } | |
| 59 | 60 | |
| 60 | - if ($response && $response['status_code'] == 200 && $response['body']) { | |
| 61 | - return [ | |
| 62 | - 'file_id' => $response['body']['id'], | |
| 63 | - 'url' => $response['body']['urls']['primary'] ?? null, | |
| 61 | + $options = [ | |
| 62 | + 'allowed_extensions' => $config['file_type'] === 'product.photo' | |
| 63 | + ? ImageFormatNormalizer::SUPPORTED_EXTENSIONS | |
| 64 | + : $config['allowed_extensions'], | |
| 65 | + 'max_size' => $config['max_size'], | |
| 66 | + 'timeout' => $config['file_type'] === 'product.video' ? 600 : 120, | |
| 64 | 67 | ]; |
| 65 | - } | |
| 66 | 68 | |
| 67 | - $errorMessage = $response['error'] ?? 'آپلود ' . $config['error_label'] . ' به باسلام ناموفق بود.'; | |
| 69 | + $mediaUploader = new MediaUploadService(); | |
| 68 | 70 | |
| 69 | - throw new \RuntimeException(esc_html($errorMessage)); | |
| 71 | + if ($config['file_type'] === 'product.video') { | |
| 72 | + return $mediaUploader->uploadVideo($pathToUpload, $options); | |
| 73 | + } | |
| 74 | + | |
| 75 | + return $mediaUploader->uploadPhoto($pathToUpload, $options); | |
| 76 | + } finally { | |
| 77 | + if ($normalizedFile && file_exists($normalizedFile)) unlink($normalizedFile); | |
| 78 | + if (!empty($preparedFile['isTemp']) && $tmpFile && file_exists($tmpFile)) unlink($tmpFile); | |
| 79 | + } | |
| 70 | 80 | } |
| 71 | 81 | |
| 72 | - private function prepare($filePath, array $allowedExtensions) | |
| 82 | + private function prepare($filePath, array $allowedExtensions, int $maxSize) | |
| 73 | 83 | { |
| 74 | 84 | if (filter_var($filePath, FILTER_VALIDATE_URL)) { |
| 75 | 85 | $parsedUrl = parse_url($filePath); |
| 76 | 86 | $path = $parsedUrl['path'] ?? $filePath; |
| @@ -78,17 +88,26 @@ | ||
| 78 | 88 | |
| 79 | 89 | if (!in_array($extension, $allowedExtensions, true)) return false; |
| 80 | 90 | |
| 81 | 91 | $tmpFile = sys_get_temp_dir() . '/' . uniqid('upload_', true) . '.' . $extension; |
| 92 | + $response = wp_safe_remote_get($filePath, [ | |
| 93 | + 'timeout' => 600, | |
| 94 | + 'stream' => true, | |
| 95 | + 'filename' => $tmpFile, | |
| 96 | + 'limit_response_size' => $maxSize + 1, | |
| 97 | + ]); | |
| 82 | 98 | |
| 83 | - $fileContents = file_get_contents($filePath); | |
| 99 | + if (is_wp_error($response)) { | |
| 100 | + if (file_exists($tmpFile)) unlink($tmpFile); | |
| 101 | + throw new \RuntimeException('دانلود فایل از آدرس مبدا ناموفق بود: ' . esc_html($response->get_error_message())); | |
| 102 | + } | |
| 84 | 103 | |
| 85 | - if ($fileContents === false) { | |
| 104 | + $statusCode = (int) wp_remote_retrieve_response_code($response); | |
| 105 | + if ($statusCode < 200 || $statusCode >= 300 || !file_exists($tmpFile)) { | |
| 106 | + if (file_exists($tmpFile)) unlink($tmpFile); | |
| 86 | 107 | throw new \RuntimeException('دانلود فایل از آدرس مبدا ناموفق بود.'); |
| 87 | 108 | } |
| 88 | 109 | |
| 89 | - file_put_contents($tmpFile, $fileContents); | |
| 90 | - | |
| 91 | 110 | return [ |
| 92 | 111 | 'path' => $tmpFile, |
| 93 | 112 | 'isTemp' => true, |
| 94 | 113 | 'tmpFile' => $tmpFile |
| @@ -97,9 +116,8 @@ | ||
| 97 | 116 | |
| 98 | 117 | if (!file_exists($filePath)) return false; |
| 99 | 118 | |
| 100 | 119 | $extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION)); |
| 101 | - | |
| 102 | 120 | if (!in_array($extension, $allowedExtensions, true)) return false; |
| 103 | 121 | |
| 104 | 122 | return [ |
| 105 | 123 | 'path' => $filePath, |
| @@ -112,38 +130,12 @@ | ||
| 112 | 130 | { |
| 113 | 131 | if (!file_exists($path)) return false; |
| 114 | 132 | |
| 115 | 133 | $fileSize = filesize($path); |
| 116 | - | |
| 117 | - if ($fileSize > $maxSize) return false; | |
| 118 | - | |
| 119 | - return true; | |
| 134 | + return $fileSize !== false && $fileSize <= $maxSize; | |
| 120 | 135 | } |
| 121 | 136 | |
| 122 | 137 | public function FileExtensionValidator($extension) |
| 123 | 138 | { |
| 124 | 139 | return in_array($extension, self::PHOTO_EXTENSIONS, true); |
| 125 | - } | |
| 126 | - | |
| 127 | - public function uploadFileToBasalam($filePath, array $config = []) | |
| 128 | - { | |
| 129 | - $apiService = syncBasalamContainer()->get(ApiServiceManager::class); | |
| 130 | - | |
| 131 | - $url = Endpoints::FILE_UPLOAD; | |
| 132 | - | |
| 133 | - $fileType = $config['file_type'] ?? 'product.photo'; | |
| 134 | - $allowedExtensions = $config['allowed_extensions'] ?? self::PHOTO_EXTENSIONS; | |
| 135 | - $maxSize = $config['max_size'] ?? self::PHOTO_MAX_SIZE; | |
| 136 | - | |
| 137 | - $data = ['file_type' => $fileType]; | |
| 138 | - $token = syncBasalamSettings()->getSettings(SettingsConfig::TOKEN); | |
| 139 | - | |
| 140 | - $headers = ['Authorization' => 'Bearer ' . $token]; | |
| 141 | - | |
| 142 | - $options = [ | |
| 143 | - 'allowed_extensions' => $allowedExtensions, | |
| 144 | - 'max_size' => $maxSize, | |
| 145 | - ]; | |
| 146 | - | |
| 147 | - return $apiService->upload($url, $filePath, $data, $headers, $options); | |
| 148 | 140 | } |
| 149 | 141 | } |