48 lines
1.4 KiB
PHP
48 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Modules\Helpdesk\Services;
|
|
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Modules\Helpdesk\Models\Attachment;
|
|
use Modules\Helpdesk\Models\Comment;
|
|
use Modules\Helpdesk\Models\Ticket;
|
|
|
|
final class AttachmentService
|
|
{
|
|
/**
|
|
* Store uploaded files against a ticket and optional comment.
|
|
*
|
|
* @param array<int, UploadedFile> $files
|
|
*/
|
|
public function storeMany(Ticket $ticket, array $files, int $userId, ?Comment $comment = null): void
|
|
{
|
|
foreach ($files as $file) {
|
|
$disk = (string) config('helpdesk.disk');
|
|
$path = $file->store('tickets/'.$ticket->id, $disk);
|
|
|
|
Attachment::query()->create([
|
|
'ticket_id' => $ticket->id,
|
|
'comment_id' => $comment?->id,
|
|
'user_id' => $userId,
|
|
'disk' => $disk,
|
|
'path' => $path,
|
|
'original_name' => $file->getClientOriginalName(),
|
|
'mime_type' => $file->getClientMimeType(),
|
|
'size' => $file->getSize() ?: 0,
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Delete a stored attachment and its backing file.
|
|
*/
|
|
public function delete(Attachment $attachment): void
|
|
{
|
|
Storage::disk($attachment->disk)->delete($attachment->path);
|
|
$attachment->delete();
|
|
}
|
|
}
|