Skip to content

Commit

Permalink
refactor(s3): improve S3 bucket endpoint formatting
Browse files Browse the repository at this point in the history
- remove the bucket name from the DigitalOcean endpoint
- always add https in front if it is not http or already https
  • Loading branch information
peaklabs-dev committed Jan 25, 2025
1 parent 12c7ee2 commit d5504ea
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions app/Livewire/Storage/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Livewire\Storage;

use App\Models\S3Storage;
use Illuminate\Support\Uri;
use Livewire\Component;

class Create extends Component
Expand Down Expand Up @@ -45,9 +46,24 @@ class Create extends Component

public function updatedEndpoint($value)
{
if (! str($value)->startsWith('https://') && ! str($value)->startsWith('http://')) {
$this->endpoint = 'https://'.$value;
$value = $this->endpoint;
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://')) {
$value = 'https://'.$value;
}
$this->endpoint = $value;
}
}

Expand Down

0 comments on commit d5504ea

Please sign in to comment.