32 lines
961 B
PHP
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',
|
|
]);
|
|
}
|
|
}
|