mad-lawsuit/app/Http/Controllers/Admin/DocumentController.php

73 lines
2.4 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\DocketEntry;
use App\Models\Document;
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
class DocumentController extends Controller
{
/**
* Store a newly uploaded document.
*/
public function store(Request $request, DocketEntry $docketEntry): RedirectResponse
{
$validated = $request->validate([
'file' => 'required|file|mimes:pdf|max:512000', // 500MB max
'title' => 'required|string|max:255',
'summary' => 'nullable|string',
]);
// Get the uploaded file
$file = $request->file('file');
// Generate unique filename
$storedFilename = Str::uuid() . '.pdf';
// Store file in storage/app/public/documents
$filePath = $file->storeAs('documents', $storedFilename, 'public');
// Get the highest display order for this entry
$maxOrder = $docketEntry->documents()->max('display_order') ?? -1;
// Create document record
Document::create([
'docket_entry_id' => $docketEntry->id,
'title' => $validated['title'],
'original_filename' => $file->getClientOriginalName(),
'stored_filename' => $storedFilename,
'file_path' => $filePath,
'file_size' => $file->getSize(),
'mime_type' => $file->getMimeType(),
'summary' => $validated['summary'] ?? null,
'display_order' => $maxOrder + 1,
]);
return redirect()->route('admin.docket-entries.show', $docketEntry->id)
->with('success', 'Document uploaded successfully.');
}
/**
* Remove the specified document.
*/
public function destroy(Document $document): RedirectResponse
{
$docketEntryId = $document->docket_entry_id;
// Delete the file from storage
if (Storage::disk('public')->exists($document->file_path)) {
Storage::disk('public')->delete($document->file_path);
}
// Delete the database record
$document->delete();
return redirect()->route('admin.docket-entries.show', $docketEntryId)
->with('success', 'Document deleted successfully.');
}
}