refactor(s3): improve S3 bucket endpoint formatting

- remove the bucket name from the DigitalOcean endpoint
- always add https in front if it is not http or already https
This commit is contained in:
peaklabs-dev
2025-01-25 23:44:12 +01:00
parent 12c7ee2879
commit d5504ea546

View File

@@ -3,6 +3,7 @@
namespace App\Livewire\Storage; namespace App\Livewire\Storage;
use App\Models\S3Storage; use App\Models\S3Storage;
use Illuminate\Support\Uri;
use Livewire\Component; use Livewire\Component;
class Create extends Component class Create extends Component
@@ -45,9 +46,24 @@ class Create extends Component
public function updatedEndpoint($value) public function updatedEndpoint($value)
{ {
try {
if (empty($value)) {
return;
}
if (str($value)->contains('digitaloceanspaces.com')) {
$uri = Uri::of($value);
$host = $uri->host();
if (preg_match('/^(.+)\.([^.]+\.digitaloceanspaces\.com)$/', $host, $matches)) {
$host = $matches[2];
$value = "https://{$host}";
}
}
} finally {
if (! str($value)->startsWith('https://') && ! str($value)->startsWith('http://')) { if (! str($value)->startsWith('https://') && ! str($value)->startsWith('http://')) {
$this->endpoint = 'https://'.$value; $value = 'https://'.$value;
$value = $this->endpoint; }
$this->endpoint = $value;
} }
} }