52 lines
1.7 KiB
PHP
52 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Namu\WireChat\Facades\WireChat;
|
|
use Namu\WireChat\Models\Attachment;
|
|
|
|
class WireChatAttachmentController extends Controller
|
|
{
|
|
public function serve(Request $request, Attachment $attachment)
|
|
{
|
|
$profile = getActiveProfile();
|
|
|
|
abort_unless($profile !== null, 403);
|
|
|
|
// Verify the active profile is a participant in the conversation this attachment belongs to
|
|
$message = $attachment->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',
|
|
]);
|
|
}
|
|
}
|