PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.3
ووسلام – همگام سازی ووکامرس و باسلام v1.10.3
1.10.19 1.10.20 1.10.18 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 All 53 releases
sync-basalam / includes / Services / FileUploader.php

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

136 lines 4.6 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', '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
47 try {
48 if (!$this->checkFileSize($pathToUpload, $config['max_size'])) {
49 throw new \RuntimeException('حج�
50 فایل ' . esc_html($config['error_label']) . ' بیش از حد �
51 جاز است.');
52 }
53
54 $options = [
55 'allowed_extensions' => $config['allowed_extensions'],
56 'max_size' => $config['max_size'],
57 'timeout' => $config['file_type'] === 'product.video' ? 600 : 120,
58 ];
59
60 $mediaUploader = new MediaUploadService();
61
62 if ($config['file_type'] === 'product.video') {
63 return $mediaUploader->uploadVideo($pathToUpload, $options);
64 }
65
66 return $mediaUploader->uploadPhoto($pathToUpload, $options);
67 } finally {
68 if (!empty($preparedFile['isTemp']) && $tmpFile && file_exists($tmpFile)) unlink($tmpFile);
69 }
70 }
71
72 private function prepare($filePath, array $allowedExtensions, int $maxSize)
73 {
74 if (filter_var($filePath, FILTER_VALIDATE_URL)) {
75 $parsedUrl = parse_url($filePath);
76 $path = $parsedUrl['path'] ?? $filePath;
77 $extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
78
79 if (!in_array($extension, $allowedExtensions, true)) return false;
80
81 $tmpFile = sys_get_temp_dir() . '/' . uniqid('upload_', true) . '.' . $extension;
82 $response = wp_safe_remote_get($filePath, [
83 'timeout' => 600,
84 'stream' => true,
85 'filename' => $tmpFile,
86 'limit_response_size' => $maxSize + 1,
87 ]);
88
89 if (is_wp_error($response)) {
90 if (file_exists($tmpFile)) unlink($tmpFile);
91 throw new \RuntimeException('دانلود فایل از آدرس �
92 بدا نا�
93 وفق بود: ' . esc_html($response->get_error_message()));
94 }
95
96 $statusCode = (int) wp_remote_retrieve_response_code($response);
97 if ($statusCode < 200 || $statusCode >= 300 || !file_exists($tmpFile)) {
98 if (file_exists($tmpFile)) unlink($tmpFile);
99 throw new \RuntimeException('دانلود فایل از آدرس �
100 بدا نا�
101 وفق بود.');
102 }
103
104 return [
105 'path' => $tmpFile,
106 'isTemp' => true,
107 'tmpFile' => $tmpFile
108 ];
109 }
110
111 if (!file_exists($filePath)) return false;
112
113 $extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
114 if (!in_array($extension, $allowedExtensions, true)) return false;
115
116 return [
117 'path' => $filePath,
118 'isTemp' => false,
119 'tmpFile' => null
120 ];
121 }
122
123 public function checkFileSize($path, int $maxSize = self::PHOTO_MAX_SIZE): bool
124 {
125 if (!file_exists($path)) return false;
126
127 $fileSize = filesize($path);
128 return $fileSize !== false && $fileSize <= $maxSize;
129 }
130
131 public function FileExtensionValidator($extension)
132 {
133 return in_array($extension, self::PHOTO_EXTENSIONS, true);
134 }
135 }
136