2023-08-07 13:31:42 +00:00
|
|
|
<?php
|
|
|
|
|
|
2024-04-26 12:09:54 +00:00
|
|
|
namespace App\Livewire\Storage;
|
2023-08-07 13:31:42 +00:00
|
|
|
|
|
|
|
|
use App\Models\S3Storage;
|
2026-04-20 09:50:19 +00:00
|
|
|
use App\Rules\SafeWebhookUrl;
|
2026-07-02 12:43:56 +00:00
|
|
|
use App\Rules\ValidS3BucketName;
|
2026-08-17 14:05:21 +00:00
|
|
|
use App\Support\DomainUrlParts;
|
2025-08-19 12:15:31 +00:00
|
|
|
use App\Support\ValidationPatterns;
|
2025-08-26 08:27:31 +00:00
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
2025-01-25 22:44:12 +00:00
|
|
|
use Illuminate\Support\Uri;
|
2023-08-08 09:51:36 +00:00
|
|
|
use Livewire\Component;
|
2023-08-07 13:31:42 +00:00
|
|
|
|
|
|
|
|
class Create extends Component
|
|
|
|
|
{
|
2025-08-26 08:27:31 +00:00
|
|
|
use AuthorizesRequests;
|
|
|
|
|
|
2023-08-07 13:31:42 +00:00
|
|
|
public string $name;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-08-07 13:31:42 +00:00
|
|
|
public string $description;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-08-07 13:31:42 +00:00
|
|
|
public string $region = 'us-east-1';
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-08-07 13:31:42 +00:00
|
|
|
public string $key;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-08-07 13:31:42 +00:00
|
|
|
public string $secret;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-08-07 13:31:42 +00:00
|
|
|
public string $bucket;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2026-08-15 09:46:49 +00:00
|
|
|
public string $endpoint = '';
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2026-08-17 14:05:21 +00:00
|
|
|
public array $endpointParts = ['scheme' => 'https', 'host' => '', 'port' => '', 'path' => ''];
|
|
|
|
|
|
|
|
|
|
public bool $endpointPartsChanged = false;
|
|
|
|
|
|
2023-08-07 13:31:42 +00:00
|
|
|
public S3Storage $storage;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2025-08-19 12:15:31 +00:00
|
|
|
protected function rules(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'name' => ValidationPatterns::nameRules(),
|
|
|
|
|
'description' => ValidationPatterns::descriptionRules(),
|
|
|
|
|
'region' => 'required|max:255',
|
|
|
|
|
'key' => 'required|max:255',
|
|
|
|
|
'secret' => 'required|max:255',
|
2026-07-02 12:43:56 +00:00
|
|
|
'bucket' => ['required', new ValidS3BucketName],
|
2026-04-20 09:50:19 +00:00
|
|
|
'endpoint' => ['required', 'max:255', new SafeWebhookUrl],
|
2025-08-19 12:15:31 +00:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function messages(): array
|
|
|
|
|
{
|
|
|
|
|
return array_merge(
|
|
|
|
|
ValidationPatterns::combinedMessages(),
|
|
|
|
|
[
|
|
|
|
|
'region.required' => 'The Region field is required.',
|
|
|
|
|
'region.max' => 'The Region may not be greater than 255 characters.',
|
|
|
|
|
'key.required' => 'The Access Key field is required.',
|
|
|
|
|
'key.max' => 'The Access Key may not be greater than 255 characters.',
|
|
|
|
|
'secret.required' => 'The Secret Key field is required.',
|
|
|
|
|
'secret.max' => 'The Secret Key may not be greater than 255 characters.',
|
|
|
|
|
'bucket.required' => 'The Bucket field is required.',
|
|
|
|
|
'endpoint.required' => 'The Endpoint field is required.',
|
|
|
|
|
'endpoint.max' => 'The Endpoint may not be greater than 255 characters.',
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-08-07 13:31:42 +00:00
|
|
|
protected $validationAttributes = [
|
|
|
|
|
'name' => 'Name',
|
|
|
|
|
'description' => 'Description',
|
|
|
|
|
'region' => 'Region',
|
|
|
|
|
'key' => 'Key',
|
2024-06-10 20:43:34 +00:00
|
|
|
'secret' => 'Secret',
|
2023-08-07 13:31:42 +00:00
|
|
|
'bucket' => 'Bucket',
|
|
|
|
|
'endpoint' => 'Endpoint',
|
|
|
|
|
];
|
2023-08-08 09:51:36 +00:00
|
|
|
|
|
|
|
|
public function submit()
|
|
|
|
|
{
|
2023-08-07 13:31:42 +00:00
|
|
|
try {
|
2025-08-26 08:27:31 +00:00
|
|
|
$this->authorize('create', S3Storage::class);
|
|
|
|
|
|
2026-08-17 14:05:21 +00:00
|
|
|
if ($this->endpointPartsChanged) {
|
|
|
|
|
$this->endpoint = DomainUrlParts::compose(...$this->endpointParts);
|
|
|
|
|
}
|
2026-08-14 16:48:34 +00:00
|
|
|
$this->endpoint = $this->normalizeEndpoint($this->endpoint);
|
2023-08-07 13:31:42 +00:00
|
|
|
$this->validate();
|
2024-07-24 19:11:12 +00:00
|
|
|
$this->storage = new S3Storage;
|
2023-08-07 13:31:42 +00:00
|
|
|
$this->storage->name = $this->name;
|
2023-08-07 14:28:07 +00:00
|
|
|
$this->storage->description = $this->description ?? null;
|
2023-08-07 13:31:42 +00:00
|
|
|
$this->storage->region = $this->region;
|
|
|
|
|
$this->storage->key = $this->key;
|
|
|
|
|
$this->storage->secret = $this->secret;
|
|
|
|
|
$this->storage->bucket = $this->bucket;
|
2025-01-07 14:31:43 +00:00
|
|
|
if (empty($this->endpoint)) {
|
|
|
|
|
$this->storage->endpoint = "https://s3.{$this->region}.amazonaws.com";
|
|
|
|
|
} else {
|
|
|
|
|
$this->storage->endpoint = $this->endpoint;
|
|
|
|
|
}
|
2023-08-22 15:44:49 +00:00
|
|
|
$this->storage->team_id = currentTeam()->id;
|
2023-08-07 13:31:42 +00:00
|
|
|
$this->storage->testConnection();
|
|
|
|
|
$this->storage->save();
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2025-12-26 12:29:53 +00:00
|
|
|
return redirectRoute($this, 'storage.show', [$this->storage->uuid]);
|
2025-01-07 14:31:43 +00:00
|
|
|
} catch (\Throwable $e) {
|
2026-08-14 16:48:34 +00:00
|
|
|
$this->dispatch('error', 'Failed to create storage.', $this->connectionErrorDescription($e));
|
2024-01-31 15:14:12 +00:00
|
|
|
// return handleError($e, $this);
|
2023-08-07 13:31:42 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-08-14 16:48:34 +00:00
|
|
|
|
2026-08-17 14:05:21 +00:00
|
|
|
public function updatedEndpointParts(): void
|
|
|
|
|
{
|
|
|
|
|
$this->endpointPartsChanged = true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-14 16:48:34 +00:00
|
|
|
private function connectionErrorDescription(\Throwable $exception): string
|
|
|
|
|
{
|
|
|
|
|
$settingsUrl = route('settings.advanced').'#endpoint-section';
|
|
|
|
|
$description = e($exception->getMessage());
|
|
|
|
|
|
|
|
|
|
if (! str_contains($exception->getMessage(), $settingsUrl)) {
|
|
|
|
|
return $description;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$link = '<a class="font-medium underline" href="'.e($settingsUrl).'">Set them here.</a>';
|
|
|
|
|
|
|
|
|
|
return str_replace(e($settingsUrl), $link, $description);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function normalizeEndpoint(string $endpoint): string
|
|
|
|
|
{
|
|
|
|
|
$endpoint = trim($endpoint);
|
|
|
|
|
|
|
|
|
|
$hasScheme = preg_match('/^(?:https?:|[a-z][a-z0-9+.-]*:\/\/)/i', $endpoint) === 1;
|
|
|
|
|
if (! $hasScheme) {
|
|
|
|
|
$endpoint = 'https://'.$endpoint;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (str($endpoint)->contains('digitaloceanspaces.com')) {
|
|
|
|
|
$host = Uri::of($endpoint)->host();
|
|
|
|
|
|
|
|
|
|
if (preg_match('/^(.+)\.([^.]+\.digitaloceanspaces\.com)$/', $host, $matches)) {
|
|
|
|
|
return "https://{$matches[2]}";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $endpoint;
|
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|