exists($document->file_path)) { abort(404, 'Document file not found.'); } // Record the access before streaming, so the dashboard reflects it. // Only successful downloads reach this point, which matches how the // historical nginx rows were filtered (status 200 only). DocumentAccess::create([ 'document_id' => $document->id, 'accessed_at' => now(), 'ip_address' => $request->ip(), 'user_agent' => $request->userAgent(), 'source' => 'live', ]); // Stream the file to the browser return Storage::disk('public')->download( $document->file_path, $document->original_filename, [ 'Content-Type' => $document->mime_type, 'Content-Disposition' => 'inline; filename="' . $document->original_filename . '"', ] ); } }