PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.1
ووسلام – همگام سازی ووکامرس و باسلام v1.10.1
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.1, at includes/Services/FileUploader.php

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