| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Attachment; |
| 6 |
use FluentSupport\App\Models\Ticket; |
| 7 |
use FluentSupport\App\Services\EmailNotification\Settings; |
| 8 |
use FluentSupport\App\Services\Helper; |
| 9 |
use FluentSupport\App\Services\Includes\FileSystem; |
| 10 |
use FluentSupport\Framework\Request\Request; |
| 11 |
|
| 12 |
class UploaderController extends Controller |
| 13 |
{ |
| 14 |
public function uploadTicketFiles(Request $request) |
| 15 |
{ |
| 16 |
$settings = (new Settings())->globalBusinessSettings(); |
| 17 |
$maxFileSize = absint($settings['max_file_size']); |
| 18 |
$mimeHeadings = Helper::getAcceptedMimeHeadings(); |
| 19 |
|
| 20 |
$maxSizeBytes = $maxFileSize * 1024; |
| 21 |
|
| 22 |
$files = $this->validate($this->request->files(), [ |
| 23 |
'file' => 'max:' . $maxSizeBytes . '|mimetypes:' . implode(',', Helper::ticketAcceptedFileMiles()) |
| 24 |
], [ |
| 25 |
'file.mimetypes' => sprintf(__('Only %s files are allowed.', 'fluent-support'), implode(', ', $mimeHeadings)), |
| 26 |
'file.max' => sprintf(__('The file can not be more than %dMB. Please upload somewhere like dropbox/google drive and paste the link in the response', 'fluent-support'), $maxFileSize) |
| 27 |
]); |
| 28 |
|
| 29 |
$ticketId = $request->get('ticket_id'); |
| 30 |
|
| 31 |
if ($ticketId == 'undefined') { |
| 32 |
$ticketId = NULL; |
| 33 |
} |
| 34 |
|
| 35 |
if ($ticketId && $request->get('intended_ticket_hash') && Helper::isPublicSignedTicketEnabled()) { |
| 36 |
$ticket = Ticket::with(['customer'])->findOrFail($ticketId); |
| 37 |
$person = $ticket->customer; |
| 38 |
} else { |
| 39 |
$person = Helper::getCurrentPerson(); |
| 40 |
} |
| 41 |
|
| 42 |
if ($person->person_type == 'customer') { |
| 43 |
$disabledFields = apply_filters('fluent_support/disabled_ticket_fields', []); |
| 44 |
if (in_array('file_upload', $disabledFields)) { |
| 45 |
return $this->sendError([ |
| 46 |
'message' => __('You do not have permission to upload a file', 'fluent-support') |
| 47 |
]); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
$uploadedFiles = FileSystem::setSubDir('ticket_' . $ticketId)->put($files); |
| 52 |
|
| 53 |
$attachments = []; |
| 54 |
foreach ($uploadedFiles as $file) { |
| 55 |
|
| 56 |
$fileData = [ |
| 57 |
'ticket_id' => $ticketId, |
| 58 |
'person_id' => $person->id, |
| 59 |
'file_type' => $file['type'], |
| 60 |
'file_path' => $file['file_path'], |
| 61 |
'full_url' => $file['url'], |
| 62 |
'title' => $file['name'], |
| 63 |
'driver' => 'local', |
| 64 |
'status' => 'in-active' |
| 65 |
]; |
| 66 |
|
| 67 |
$attachment = Attachment::create($fileData); |
| 68 |
$attachments[] = $attachment->file_hash; |
| 69 |
} |
| 70 |
|
| 71 |
return [ |
| 72 |
'attachments' => $attachments |
| 73 |
]; |
| 74 |
|
| 75 |
} |
| 76 |
} |
| 77 |
|