ldc-ovoteam/app/Http/Controllers/MediaController.php
Jérémy Munsch 1e670eb1c2
fix(MediaController): improve S3 URL generation logic
Ensure proper handling of S3 disk type and URL construction.
2026-11-05 07:57:14 +01:00

32 lines
961 B
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers;
use App\Lib\PublicS3Disk;
use Illuminate\Filesystem\AwsS3V3Adapter;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Storage;
class MediaController extends Controller
{
public function __invoke(string $path, PublicS3Disk $s3): RedirectResponse
{
$disk = Storage::disk($s3->disk->value);
$prefix = Config::string('storage-service.prefix', 'media');
if (! ($disk instanceof AwsS3V3Adapter)) {
throw new \UnexpectedValueException("Expected {$s3->disk->value} to be an S3 disk");
}
$trimmedPath = ltrim($path, '/');
$fullPath = ltrim(trim($prefix, '/') . "/{$trimmedPath}", '/');
$s3Url = $disk->url($fullPath);
return redirect()->away($s3Url, 301, [
'Cache-Control' => 'public, max-age=31536000, immutable',
]);
}
}