| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Services\Api; |
| 4 |
|
| 5 |
use SyncBasalam\Logger\Logger; |
| 6 |
|
| 7 |
defined('ABSPATH') || exit; |
| 8 |
|
| 9 |
class FileUploadApiService |
| 10 |
{ |
| 11 |
public function upload(string $url, string $filePath, array $data = [], array $headers = [], array $options = []): array |
| 12 |
{ |
| 13 |
$fileValidation = $this->validateFile($filePath, $options); |
| 14 |
if (!$fileValidation['valid']) { |
| 15 |
return [ |
| 16 |
'body' => null, |
| 17 |
'status_code' => 400, |
| 18 |
'error' => $fileValidation['message'] |
| 19 |
]; |
| 20 |
} |
| 21 |
|
| 22 |
$boundary = wp_generate_password(24); |
| 23 |
$payload = $this->makePayloadupload($data, $filePath, $boundary); |
| 24 |
|
| 25 |
$headers = array_merge($headers, ['content-type' => 'multipart/form-data; boundary=' . $boundary]); |
| 26 |
|
| 27 |
$response = wp_remote_post( |
| 28 |
$url, |
| 29 |
['headers' => $headers, 'body' => $payload, 'timeout' => 10,], |
| 30 |
); |
| 31 |
|
| 32 |
return $this->handleResponse($response, $url); |
| 33 |
} |
| 34 |
|
| 35 |
private function validateFile(string $filePath, array $options = []): array |
| 36 |
{ |
| 37 |
if (!file_exists($filePath)) { |
| 38 |
return ['valid' => false, 'message' => 'فایل در دسترس نیست' . $filePath]; |
| 39 |
} |
| 40 |
|
| 41 |
$maxSize = isset($options['max_size']) ? (int) $options['max_size'] : 5 * 1024 * 1024; |
| 42 |
|
| 43 |
$fileSize = filesize($filePath); |
| 44 |
if ($fileSize > $maxSize) { |
| 45 |
return ['valid' => false, 'message' => 'حج� |
| 46 |
فایل بیش از حد � |
| 47 |
جاز است : ' . $filePath]; |
| 48 |
} |
| 49 |
|
| 50 |
$allowedExtensions = isset($options['allowed_extensions']) && is_array($options['allowed_extensions']) |
| 51 |
? $options['allowed_extensions'] |
| 52 |
: ['jpg', 'png', 'webp', 'bmp', 'jfif', 'jpeg', 'avif']; |
| 53 |
|
| 54 |
$extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION)); |
| 55 |
|
| 56 |
if (!in_array($extension, $allowedExtensions, true)) { |
| 57 |
return ['valid' => false, 'message' => 'فر� |
| 58 |
ت فایل � |
| 59 |
عتبر نیست ، فر� |
| 60 |
ت های � |
| 61 |
عتبر : ' . implode(', ', $allowedExtensions)]; |
| 62 |
} |
| 63 |
|
| 64 |
return ['valid' => true, 'message' => 'فایل � |
| 65 |
عتبر است']; |
| 66 |
} |
| 67 |
|
| 68 |
private function handleResponse($response, string $url = ''): array |
| 69 |
{ |
| 70 |
if (is_wp_error($response)) { |
| 71 |
$category = RequestStatusTracker::recordWpError($response, $url); |
| 72 |
$errorData = $response->get_error_data(); |
| 73 |
$errorCodes = $response->get_error_codes(); |
| 74 |
$errorMessages = []; |
| 75 |
|
| 76 |
foreach ($errorCodes as $code) { |
| 77 |
$messages = $response->get_error_messages($code); |
| 78 |
if (!empty($messages)) $errorMessages[$code] = $messages; |
| 79 |
} |
| 80 |
|
| 81 |
Logger::error('خطا در فرایند آپلود فایل در باسلا� |
| 82 |
. ' . RequestStatusTracker::describeCategoryFa($category), [ |
| 83 |
'url' => $url, |
| 84 |
'category' => $category, |
| 85 |
'reason' => RequestStatusTracker::describeCategoryFa($category), |
| 86 |
'error_codes' => $errorCodes, |
| 87 |
'error_messages' => $errorMessages, |
| 88 |
'error_data' => $errorData, |
| 89 |
]); |
| 90 |
|
| 91 |
return [ |
| 92 |
'body' => null, |
| 93 |
'status_code' => $this->resolveWpErrorStatusCode($response), |
| 94 |
'error' => $this->resolveWpErrorMessage($response) |
| 95 |
]; |
| 96 |
} |
| 97 |
|
| 98 |
$body = wp_remote_retrieve_body($response); |
| 99 |
$statusCode = wp_remote_retrieve_response_code($response); |
| 100 |
RequestStatusTracker::recordHttpStatus((int) $statusCode, $url); |
| 101 |
|
| 102 |
$decodedBody = json_decode($body, true); |
| 103 |
|
| 104 |
if ((int) $statusCode >= 400) { |
| 105 |
$reason = RequestStatusTracker::describeHttpStatusFa((int) $statusCode); |
| 106 |
Logger::error('آپلود فایل به باسلا� |
| 107 |
نا� |
| 108 |
وفق بود. ' . $reason, [ |
| 109 |
'url' => $url, |
| 110 |
'status_code' => (int) $statusCode, |
| 111 |
'reason' => $reason, |
| 112 |
'response_body' => $decodedBody ?? $body, |
| 113 |
]); |
| 114 |
} |
| 115 |
|
| 116 |
return [ |
| 117 |
'body' => $decodedBody, |
| 118 |
'status_code' => $statusCode, |
| 119 |
'error' => (int) $statusCode >= 400 ? $this->extractErrorMessage($decodedBody, $body) : null |
| 120 |
]; |
| 121 |
} |
| 122 |
|
| 123 |
private function makePayloadupload(array $data, string $localFile, string $boundary): string |
| 124 |
{ |
| 125 |
$payload = ''; |
| 126 |
$eol = "\r\n"; |
| 127 |
|
| 128 |
foreach ($data as $name => $value) { |
| 129 |
$payload .= '--' . $boundary . $eol; |
| 130 |
$payload .= 'Content-Disposition: form-data; name="' . $name . '"' . $eol . $eol; |
| 131 |
$payload .= $value . $eol; |
| 132 |
} |
| 133 |
|
| 134 |
if ($localFile) { |
| 135 |
$filename = basename($localFile); |
| 136 |
$fileContent = file_get_contents($localFile); |
| 137 |
|
| 138 |
if ($fileContent === false) { |
| 139 |
Logger::error('خطا در خواندن فایل با استفاده از file_get_contents: ' . $localFile); |
| 140 |
return ''; |
| 141 |
} |
| 142 |
|
| 143 |
$filetypeInfo = wp_check_filetype($localFile); |
| 144 |
$mimeType = $filetypeInfo['type'] ?? 'application/octet-stream'; |
| 145 |
|
| 146 |
$payload .= '--' . $boundary . $eol; |
| 147 |
$payload .= 'Content-Disposition: form-data; name="file"; filename="' . $filename . '"' . $eol; |
| 148 |
$payload .= 'Content-Type: ' . $mimeType . $eol; |
| 149 |
$payload .= 'Content-Transfer-Encoding: binary' . $eol . $eol; |
| 150 |
$payload .= $fileContent . $eol; |
| 151 |
} |
| 152 |
|
| 153 |
$payload .= '--' . $boundary . '--' . $eol; |
| 154 |
|
| 155 |
return $payload; |
| 156 |
} |
| 157 |
|
| 158 |
private function resolveWpErrorMessage(\WP_Error $response): string |
| 159 |
{ |
| 160 |
$errorData = $response->get_error_data(); |
| 161 |
$host = is_array($errorData) ? ($errorData['host'] ?? '') : ''; |
| 162 |
$message = $response->get_error_message(); |
| 163 |
|
| 164 |
if ($response->get_error_code() === 'http_request_blocked') { |
| 165 |
return 'درخواست آپلود تصویر به باسلا� |
| 166 |
� |
| 167 |
سدود شد' . ($host ? ': ' . $host : ''); |
| 168 |
} |
| 169 |
|
| 170 |
return $message ?: 'خطای نا� |
| 171 |
شخص در آپلود فایل'; |
| 172 |
} |
| 173 |
|
| 174 |
private function resolveWpErrorStatusCode(\WP_Error $response): int |
| 175 |
{ |
| 176 |
$errorData = $response->get_error_data(); |
| 177 |
|
| 178 |
if (is_array($errorData) && isset($errorData['status']) && is_numeric($errorData['status'])) { |
| 179 |
return (int) $errorData['status']; |
| 180 |
} |
| 181 |
|
| 182 |
return 500; |
| 183 |
} |
| 184 |
|
| 185 |
private function extractErrorMessage($decodedBody, string $rawBody): string |
| 186 |
{ |
| 187 |
if (is_array($decodedBody)) { |
| 188 |
if (!empty($decodedBody['messages'][0]['message'])) return (string) $decodedBody['messages'][0]['message']; |
| 189 |
if (!empty($decodedBody['errors'][0]['message'])) return (string) $decodedBody['errors'][0]['message']; |
| 190 |
if (!empty($decodedBody['error'])) return (string) $decodedBody['error']; |
| 191 |
if (!empty($decodedBody['message'])) return (string) $decodedBody['message']; |
| 192 |
} |
| 193 |
|
| 194 |
return $rawBody !== '' ? $rawBody : 'خطای نا� |
| 195 |
شخص در آپلود فایل'; |
| 196 |
} |
| 197 |
} |
| 198 |
|