<?php

namespace App\Http\Controllers;

use App\Support\MediaUrl;
use Illuminate\Support\Facades\Storage;

class MediaController extends Controller
{
    public function show(string $path)
    {
        $path = MediaUrl::normalize(rawurldecode($path));

        abort_if($path === '' || str_contains($path, '..'), 404);

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

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

        return response()->file($disk->path($path), [
            'Cache-Control' => 'public, max-age=31536000',
        ]);
    }
}
