| 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 |
| 12 |
{ |
| 13 |
$fileValidation = $this->validateFile($filePath); |
| 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($url, ['headers' => $headers, 'body' => $payload]); |
| 28 |
|
| 29 |
return $this->handleResponse($response, $url); |
| 30 |
} |
| 31 |
|
| 32 |
private function validateFile(string $filePath): array |
| 33 |
{ |
| 34 |
if (!file_exists($filePath)) { |
| 35 |
return ['valid' => false, 'message' => 'فایل در دسترس نیست' . $filePath]; |
| 36 |
} |
| 37 |
|
| 38 |
$fileSize = filesize($filePath); |
| 39 |
if ($fileSize > 5 * 1024 * 1024) { |
| 40 |
return ['valid' => false, 'message' => 'حج� |
| 41 |
فایل بیش از حد � |
| 42 |
جاز است : ' . $filePath]; |
| 43 |
} |
| 44 |
|
| 45 |
$allowedExtensions = ['jpg', 'png', 'webp', 'bmp', 'jfif', 'jpeg', 'avif']; |
| 46 |
$extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION)); |
| 47 |
|
| 48 |
if (!in_array($extension, $allowedExtensions)) { |
| 49 |
return ['valid' => false, 'message' => 'فر� |
| 50 |
ت تصویر � |
| 51 |
تغیر نیست ، فر� |
| 52 |
ت های � |
| 53 |
عتبر : ' . implode(', ', $allowedExtensions)]; |
| 54 |
} |
| 55 |
|
| 56 |
return ['valid' => true, 'message' => 'فایل � |
| 57 |
تعبر است']; |
| 58 |
} |
| 59 |
|
| 60 |
private function handleResponse($response, string $url = ''): array |
| 61 |
{ |
| 62 |
if (is_wp_error($response)) { |
| 63 |
RequestStatusTracker::recordWpError($response, $url); |
| 64 |
|
| 65 |
return [ |
| 66 |
'body' => null, |
| 67 |
'status_code' => 500, |
| 68 |
'error' => $response->get_error_message() |
| 69 |
]; |
| 70 |
} |
| 71 |
|
| 72 |
$body = wp_remote_retrieve_body($response); |
| 73 |
$statusCode = wp_remote_retrieve_response_code($response); |
| 74 |
RequestStatusTracker::recordHttpStatus((int) $statusCode, $url); |
| 75 |
|
| 76 |
$decodedBody = json_decode($body, true); |
| 77 |
|
| 78 |
return [ |
| 79 |
'body' => $decodedBody, |
| 80 |
'status_code' => $statusCode, |
| 81 |
'error' => null |
| 82 |
]; |
| 83 |
} |
| 84 |
|
| 85 |
private function makePayloadupload(array $data, string $localFile, string $boundary): string |
| 86 |
{ |
| 87 |
$payload = ''; |
| 88 |
$eol = "\r\n"; |
| 89 |
|
| 90 |
foreach ($data as $name => $value) { |
| 91 |
$payload .= '--' . $boundary . $eol; |
| 92 |
$payload .= 'Content-Disposition: form-data; name="' . $name . '"' . $eol . $eol; |
| 93 |
$payload .= $value . $eol; |
| 94 |
} |
| 95 |
|
| 96 |
if ($localFile) { |
| 97 |
$filename = basename($localFile); |
| 98 |
$fileContent = file_get_contents($localFile); |
| 99 |
|
| 100 |
if ($fileContent === false) { |
| 101 |
Logger::error("خطا در خواندن فایل با استفاده از file_get_contents: " . $localFile); |
| 102 |
return ''; |
| 103 |
} |
| 104 |
|
| 105 |
$filetypeInfo = wp_check_filetype($localFile); |
| 106 |
$mimeType = $filetypeInfo['type'] ?? 'application/octet-stream'; |
| 107 |
|
| 108 |
$payload .= '--' . $boundary . $eol; |
| 109 |
$payload .= 'Content-Disposition: form-data; name="file"; filename="' . $filename . '"' . $eol; |
| 110 |
$payload .= 'Content-Type: ' . $mimeType . $eol; |
| 111 |
$payload .= 'Content-Transfer-Encoding: binary' . $eol . $eol; |
| 112 |
$payload .= $fileContent . $eol; |
| 113 |
} |
| 114 |
|
| 115 |
$payload .= '--' . $boundary . '--' . $eol; |
| 116 |
|
| 117 |
return $payload; |
| 118 |
} |
| 119 |
} |
| 120 |
|