Updated PublicS3Disk to accept S3 public endpoint in constructor. Enhanced publicUrlOf method to generate public URLs using the adapter.
45 lines
1.2 KiB
PHP
45 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Lib;
|
|
|
|
use Illuminate\Filesystem\AwsS3V3Adapter;
|
|
use Illuminate\Support\Facades\Config;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Uwil\Storage\Lib\ValueObjects\DiskName;
|
|
|
|
final readonly class PublicS3Disk
|
|
{
|
|
public const DISK_NAME = 's3-public';
|
|
|
|
public DiskName $disk;
|
|
|
|
public string $urlEndpoint;
|
|
|
|
private AwsS3V3Adapter $adapter;
|
|
|
|
public function __construct(
|
|
private readonly string $s3PublicEndpoint,
|
|
) {
|
|
$this->disk = new DiskName(self::DISK_NAME, 's3');
|
|
$this->urlEndpoint = $this->s3PublicEndpoint;
|
|
$adapter = Storage::disk($this->disk->value);
|
|
|
|
if (! ($adapter instanceof AwsS3V3Adapter)) {
|
|
throw new \UnexpectedValueException("Expected {$this->disk->value} to be a S3 disk");
|
|
}
|
|
|
|
$this->adapter = $adapter;
|
|
}
|
|
|
|
public function publicUrlOf(string $path): string
|
|
{
|
|
$prefix = Config::string('storage-service.prefix', 'media');
|
|
|
|
$trimmedPath = ltrim($path, '/');
|
|
$fullPath = ltrim(trim($prefix, '/') . "/{$trimmedPath}", '/');
|
|
|
|
return $this->adapter->publicUrl($fullPath);
|
|
}
|
|
}
|