| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Actions\Controller\TicketActions; |
| 4 |
|
| 5 |
use SyncBasalam\Actions\Controller\ActionController; |
| 6 |
use SyncBasalam\Services\TicketServiceManager; |
| 7 |
|
| 8 |
defined('ABSPATH') || exit; |
| 9 |
|
| 10 |
class UploadTicketMediaAjax extends ActionController |
| 11 |
{ |
| 12 |
public function __invoke() |
| 13 |
{ |
| 14 |
if (empty($_FILES['file']['tmp_name'])) { |
| 15 |
wp_send_json_error(['message' => 'فایلی انتخاب نشده است.']); |
| 16 |
return; |
| 17 |
} |
| 18 |
|
| 19 |
$originalName = $_FILES['file']['name'] ?? 'unknown'; |
| 20 |
$fileSize = $_FILES['file']['size'] ?? 0; |
| 21 |
|
| 22 |
$moveResult = wp_handle_upload($_FILES['file'], ['test_form' => false]); |
| 23 |
|
| 24 |
if (isset($moveResult['error'])) { |
| 25 |
wp_send_json_error(['message' => $moveResult['error']]); |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
$filePath = $moveResult['file']; |
| 30 |
|
| 31 |
$ticketManager = new TicketServiceManager(); |
| 32 |
$uploadResult = $ticketManager->uploadTicketMedia($filePath); |
| 33 |
|
| 34 |
@unlink($filePath); |
| 35 |
|
| 36 |
if (empty($uploadResult['body']['data']['id'])) { |
| 37 |
wp_send_json_error(['message' => 'خطا در آپلود فایل به سرور ه� |
| 38 |
سلا� |
| 39 |
.']); |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
$fileId = $uploadResult['body']['data']['id']; |
| 44 |
|
| 45 |
wp_send_json_success(['file_id' => $fileId]); |
| 46 |
} |
| 47 |
} |
| 48 |
|