coolify/app/Livewire/Storage/Create.php

126 lines
4 KiB
PHP
Raw Normal View History

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;
use App\Support\ValidationPatterns;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Uri;
use Livewire\Component;
2023-08-07 13:31:42 +00:00
class Create extends Component
{
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
2023-08-07 13:31:42 +00:00
public string $endpoint;
2024-06-10 20:43:34 +00:00
2023-08-07 13:31:42 +00:00
public S3Storage $storage;
2024-06-10 20:43:34 +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',
'bucket' => 'required|max:255',
'endpoint' => 'required|url|max:255',
];
}
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.',
'bucket.max' => 'The Bucket may not be greater than 255 characters.',
'endpoint.required' => 'The Endpoint field is required.',
'endpoint.url' => 'The Endpoint must be a valid URL.',
'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',
];
2024-10-02 08:25:45 +00:00
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://')) {
$value = 'https://'.$value;
}
$this->endpoint = $value;
2024-10-02 08:25:45 +00:00
}
2023-08-07 13:31:42 +00:00
}
public function submit()
{
2023-08-07 13:31:42 +00:00
try {
$this->authorize('create', S3Storage::class);
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;
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
2024-04-26 12:09:54 +00:00
return redirect()->route('storage.show', $this->storage->uuid);
} catch (\Throwable $e) {
2024-01-31 15:14:12 +00:00
$this->dispatch('error', 'Failed to create storage.', $e->getMessage());
// return handleError($e, $this);
2023-08-07 13:31:42 +00:00
}
}
}