lasthourcloud/app/Livewire/Storage/Create.php

75 lines
2.3 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 Livewire\Component;
2023-08-07 13:31:42 +00:00
class Create extends Component
{
public string $name;
public string $description;
public string $region = 'us-east-1';
public string $key;
public string $secret;
public string $bucket;
public string $endpoint;
public S3Storage $storage;
protected $rules = [
'name' => 'required|min:3|max:255',
2023-08-07 13:31:42 +00:00
'description' => 'nullable|min:3|max:255',
'region' => 'required|max:255',
'key' => 'required|max:255',
'secret' => 'required|max:255',
'bucket' => 'required|max:255',
'endpoint' => 'required|url|max:255',
2023-08-07 13:31:42 +00:00
];
protected $validationAttributes = [
'name' => 'Name',
'description' => 'Description',
'region' => 'Region',
'key' => 'Key',
'secret' => "Secret",
'bucket' => 'Bucket',
'endpoint' => 'Endpoint',
];
public function mount()
{
if (isDev()) {
2023-08-07 13:31:42 +00:00
$this->name = 'Local MinIO';
$this->description = 'Local MinIO';
$this->key = 'minioadmin';
$this->secret = 'minioadmin';
$this->bucket = 'local';
$this->endpoint = 'http://coolify-minio:9000';
}
}
public function submit()
{
2023-08-07 13:31:42 +00:00
try {
$this->validate();
$this->storage = new S3Storage();
$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-04-26 12:09:54 +00:00
return redirect()->route('storage.show', $this->storage->uuid);
2023-09-11 15:36:30 +00:00
} 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
}
}
}