feat: full dashboard-ticketing scaffold with data model, controllers, Vue pages
This commit is contained in:
63
src/Http/Controllers/TicketAttachmentController.php
Normal file
63
src/Http/Controllers/TicketAttachmentController.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace Dashboard\Ticketing\Http\Controllers;
|
||||
|
||||
use Dashboard\Ticketing\Models\Ticket;
|
||||
use Dashboard\Ticketing\Models\TicketAttachment;
|
||||
use Dashboard\Ticketing\Models\TicketingAgentAccess;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Controller;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class TicketAttachmentController extends Controller
|
||||
{
|
||||
private const MAX_SIZE_MB = 20;
|
||||
|
||||
private function canAccessTicket(Ticket $ticket): bool
|
||||
{
|
||||
$user = Auth::user();
|
||||
$isAgent = TicketingAgentAccess::where('user_id', $user->id)
|
||||
->where('group_id', $ticket->group_id)
|
||||
->exists();
|
||||
return $isAgent || $ticket->submitter_id === $user->id;
|
||||
}
|
||||
|
||||
public function store(Request $request, Ticket $ticket)
|
||||
{
|
||||
if (!$this->canAccessTicket($ticket)) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$request->validate([
|
||||
'file' => 'required|file|max:' . (self::MAX_SIZE_MB * 1024),
|
||||
'message_id' => 'nullable|exists:ticket_messages,id',
|
||||
]);
|
||||
|
||||
$file = $request->file('file');
|
||||
$disk = config('ticketing.storage_disk', env('TICKETING_STORAGE_DISK', 'local'));
|
||||
$path = $file->store('ticketing/attachments/' . $ticket->id, $disk);
|
||||
|
||||
TicketAttachment::create([
|
||||
'ticket_id' => $ticket->id,
|
||||
'message_id' => $request->message_id,
|
||||
'filename' => $file->getClientOriginalName(),
|
||||
'path' => $path,
|
||||
'mime_type' => $file->getMimeType(),
|
||||
'size' => $file->getSize(),
|
||||
]);
|
||||
|
||||
return back()->with('success', 'Attachment uploaded.');
|
||||
}
|
||||
|
||||
public function show(TicketAttachment $attachment)
|
||||
{
|
||||
$ticket = $attachment->ticket;
|
||||
if (!$this->canAccessTicket($ticket)) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$disk = config('ticketing.storage_disk', env('TICKETING_STORAGE_DISK', 'local'));
|
||||
return Storage::disk($disk)->download($attachment->path, $attachment->filename);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user