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

112 lines 3.0 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
8 defined('ABSPATH') || exit;
9 class FileUploader
10 {
11 public function upload($filePath)
12 {
13 $preparedFile = $this->prepare($filePath);
14
15 if ($preparedFile === false) {
16 return false;
17 }
18
19 $pathToUpload = $preparedFile['path'];
20 $isTemp = $preparedFile['isTemp'];
21 $tmpFile = $preparedFile['tmpFile'] ?? null;
22
23 if (!$this->checkFileSize($pathToUpload)) {
24 if ($isTemp && $tmpFile) unlink($tmpFile);
25 return false;
26 }
27
28 $response = $this->uploadFileToBasalam($pathToUpload);
29
30 if ($isTemp && $tmpFile) unlink($tmpFile);
31
32 if ($response && $response['status_code'] == 200 && $response['body']) {
33 return [
34 'file_id' => $response['body']['id'],
35 'url' => $response['body']['urls']['primary'],
36 ];
37 }
38
39 return false;
40 }
41
42 private function prepare($filePath)
43 {
44 if (filter_var($filePath, FILTER_VALIDATE_URL)) {
45 $parsedUrl = parse_url($filePath);
46 $path = $parsedUrl['path'] ?? $filePath;
47 $extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
48
49 if (!$this->FileExtensionValidator($extension)) return false;
50
51 $tmpFile = sys_get_temp_dir() . '/' . uniqid('upload_', true) . '.' . $extension;
52
53 $fileContents = file_get_contents($filePath);
54
55 if ($fileContents === false) return false;
56
57 file_put_contents($tmpFile, $fileContents);
58
59 return [
60 'path' => $tmpFile,
61 'isTemp' => true,
62 'tmpFile' => $tmpFile
63 ];
64 } else {
65 if (!file_exists($filePath)) return false;
66
67 $extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
68
69 if (!$this->FileExtensionValidator($extension)) return false;
70
71 return [
72 'path' => $filePath,
73 'isTemp' => false,
74 'tmpFile' => null
75 ];
76 }
77 }
78
79 public function checkFileSize($path)
80 {
81 if (!file_exists($path)) return false;
82
83 $fileSize = filesize($path);
84
85 if ($fileSize > 5 * 1024 * 1024) return false;
86
87 return true;
88 }
89
90 public function FileExtensionValidator($extension)
91 {
92 $allowedExtensions = ['jpg', 'png', 'webp', 'bmp', 'jfif', 'jpeg', 'avif'];
93 return in_array($extension, $allowedExtensions);
94 }
95
96 public function uploadFileToBasalam($filePath)
97 {
98 $apiService = syncBasalamContainer()->get(ApiServiceManager::class);
99
100 $url = Endpoints::FILE_UPLOAD;
101
102 $data = ['file_type' => 'product.photo'];
103 $token = syncBasalamSettings()->getSettings(SettingsConfig::TOKEN);
104
105 $headers = ['Authorization' => 'Bearer ' . $token];
106
107 $response = $apiService->upload($url, $filePath, $data, $headers);
108
109 return $response;
110 }
111 }
112