attachable; abort_unless($message !== null, 404); $conversation = $message->conversation; abort_unless($conversation !== null, 404); abort_unless($profile->belongsToConversation($conversation), 403); $disk = Storage::disk(WireChat::storageDisk()); abort_unless($disk->exists($attachment->file_path), 404); $absolutePath = $disk->path($attachment->file_path); $mimeType = $attachment->mime_type; $fileSize = filesize($absolutePath); // Use StreamedResponse so we can flush any stray OB content before sending binary data. // BinaryFileResponse writes to php://output which respects active output buffers, // so any pre-existing OB content (e.g. from middleware) would prepend to the binary file. return response()->stream(function () use ($absolutePath) { while (ob_get_level() > 0) { ob_end_clean(); } readfile($absolutePath); }, 200, [ 'Content-Type' => $mimeType, 'Content-Length' => $fileSize, 'Cache-Control' => 'private, max-age=3600', 'Accept-Ranges' => 'none', ]); } }