<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class UploadProxyController extends Controller
{
    public function show(Request $request, string $path)
    {
        if (str_contains($path, '..')) {
            abort(404);
        }

        $disk = Storage::disk('public');
        if (! $disk->exists($path)) {
            abort(404);
        }

        $absolute = $disk->path($path);
        $mime     = $disk->mimeType($path) ?: 'application/octet-stream';

        return response()->file($absolute, [
            'Content-Type'                  => $mime,
            'Cache-Control'                 => 'public, max-age=86400',
            'Access-Control-Allow-Origin'   => '*',
            'Cross-Origin-Resource-Policy'  => 'cross-origin',
        ]);
    }
}
