<?php

namespace App\Http\Controllers\Public;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class MediaController extends Controller
{
    public function show(string $path): BinaryFileResponse
    {
        abort_if(str_contains($path, '..'), 404);

        $disk = Storage::disk('public');

        abort_unless($disk->exists($path), 404);

        $headers = [
            'Cache-Control' => 'public, max-age=604800',
            'Content-Type' => $disk->mimeType($path) ?: 'application/octet-stream',
        ];

        return response()->file($disk->path($path), $headers);
    }
}
