PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / trunk
ووسلام – همگام سازی ووکامرس و باسلام vtrunk
1.10.17 1.10.15 1.10.14 1.10.13 1.10.12 1.10.10 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.2 1.9.1 1.9.0 1.8.8 1.8.5 1.8.6 1.8.7 1.8.4 1.7.6 All 50 releases
sync-basalam / includes / Services / FileUploader.php

FileUploader.php in ووسلام – همگام سازی ووکامرس و باسلام trunk, at includes/Services/FileUploader.php

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