coolify/app/Livewire/Project/Shared/Storages/Add.php

156 lines
5.1 KiB
PHP
Raw Normal View History

2023-05-05 12:08:38 +02:00
<?php
2023-12-07 19:06:32 +01:00
namespace App\Livewire\Project\Shared\Storages;
2023-05-05 12:08:38 +02:00
2023-12-18 14:01:25 +01:00
use App\Models\Application;
use App\Models\LocalFileVolume;
2023-05-05 12:08:38 +02:00
use Livewire\Component;
class Add extends Component
{
public $resource;
2024-06-10 22:43:34 +02:00
2023-10-02 09:08:41 +02:00
public $uuid;
2024-06-10 22:43:34 +02:00
2023-05-05 12:08:38 +02:00
public $parameters;
2024-06-10 22:43:34 +02:00
2023-12-18 14:01:25 +01:00
public $isSwarm = false;
2024-06-10 22:43:34 +02:00
2023-05-05 12:08:38 +02:00
public string $name;
2024-06-10 22:43:34 +02:00
2023-05-05 12:08:38 +02:00
public string $mount_path;
2024-06-10 22:43:34 +02:00
2023-12-18 14:01:25 +01:00
public ?string $host_path = null;
2024-06-10 22:43:34 +02:00
public string $file_storage_path;
2024-06-10 22:43:34 +02:00
2024-05-28 13:07:07 +02:00
public ?string $file_storage_content = null;
2024-06-10 22:43:34 +02:00
public string $file_storage_directory_source;
2024-06-10 22:43:34 +02:00
public string $file_storage_directory_destination;
2023-05-05 14:20:10 +02:00
2023-12-18 14:01:25 +01:00
public $rules = [
2023-05-05 12:08:38 +02:00
'name' => 'required|string',
'mount_path' => 'required|string',
'host_path' => 'string|nullable',
'file_storage_path' => 'string',
2024-05-28 13:07:07 +02:00
'file_storage_content' => 'nullable|string',
'file_storage_directory_source' => 'string',
'file_storage_directory_destination' => 'string',
2023-05-05 12:08:38 +02:00
];
2023-12-18 14:01:25 +01:00
protected $listeners = ['clearAddStorage' => 'clear'];
2023-06-16 12:35:40 +02:00
protected $validationAttributes = [
'name' => 'name',
'mount_path' => 'mount',
'host_path' => 'host',
'file_storage_path' => 'file storage path',
'file_storage_content' => 'file storage content',
'file_storage_directory_source' => 'file storage directory source',
'file_storage_directory_destination' => 'file storage directory destination',
2023-06-16 12:35:40 +02:00
];
2023-05-05 12:08:38 +02:00
public function mount()
{
2024-06-10 22:43:34 +02:00
$this->file_storage_directory_source = application_configuration_dir()."/{$this->resource->uuid}";
$this->uuid = $this->resource->uuid;
$this->parameters = get_route_parameters();
2023-12-20 16:19:48 +01:00
if (data_get($this->parameters, 'application_uuid')) {
$applicationUuid = $this->parameters['application_uuid'];
$application = Application::where('uuid', $applicationUuid)->first();
2024-06-10 22:43:34 +02:00
if (! $application) {
2023-12-20 16:19:48 +01:00
abort(404);
}
if ($application->destination->server->isSwarm()) {
$this->isSwarm = true;
$this->rules['host_path'] = 'required|string';
}
2023-12-18 14:01:25 +01:00
}
2023-05-05 12:08:38 +02:00
}
2024-06-10 22:43:34 +02:00
public function submitFileStorage()
{
try {
2024-05-28 13:07:07 +02:00
$this->validate([
'file_storage_path' => 'string',
'file_storage_content' => 'nullable|string',
]);
$this->file_storage_path = trim($this->file_storage_path);
$this->file_storage_path = str($this->file_storage_path)->start('/')->value();
if ($this->resource->getMorphClass() === 'App\Models\Application') {
2024-06-10 22:43:34 +02:00
$fs_path = application_configuration_dir().'/'.$this->resource->uuid.$this->file_storage_path;
}
LocalFileVolume::create(
[
'fs_path' => $fs_path,
'mount_path' => $this->file_storage_path,
'content' => $this->file_storage_content,
'is_directory' => false,
'resource_id' => $this->resource->id,
2024-06-10 22:43:34 +02:00
'resource_type' => get_class($this->resource),
],
);
$this->dispatch('refresh_storages');
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
2024-06-10 22:43:34 +02:00
public function submitFileStorageDirectory()
{
try {
2024-05-28 13:07:07 +02:00
$this->validate([
'file_storage_directory_source' => 'string',
'file_storage_directory_destination' => 'string',
]);
$this->file_storage_directory_source = trim($this->file_storage_directory_source);
$this->file_storage_directory_source = str($this->file_storage_directory_source)->start('/')->value();
$this->file_storage_directory_destination = trim($this->file_storage_directory_destination);
$this->file_storage_directory_destination = str($this->file_storage_directory_destination)->start('/')->value();
LocalFileVolume::create(
[
'fs_path' => $this->file_storage_directory_source,
'mount_path' => $this->file_storage_directory_destination,
'is_directory' => true,
'resource_id' => $this->resource->id,
2024-06-10 22:43:34 +02:00
'resource_type' => get_class($this->resource),
],
);
$this->dispatch('refresh_storages');
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
2024-06-10 22:43:34 +02:00
public function submitPersistentVolume()
2023-05-05 12:08:38 +02:00
{
2023-12-18 14:01:25 +01:00
try {
2024-05-28 13:07:07 +02:00
$this->validate([
'name' => 'required|string',
'mount_path' => 'required|string',
'host_path' => 'string|nullable',
]);
2024-06-10 22:43:34 +02:00
$name = $this->uuid.'-'.$this->name;
2023-12-18 14:01:25 +01:00
$this->dispatch('addNewVolume', [
'name' => $name,
'mount_path' => $this->mount_path,
'host_path' => $this->host_path,
]);
2023-12-18 14:01:25 +01:00
} catch (\Throwable $e) {
return handleError($e, $this);
}
2023-05-05 14:20:10 +02:00
}
2023-05-05 14:20:10 +02:00
public function clear()
{
$this->name = '';
$this->mount_path = '';
$this->host_path = null;
2023-05-05 12:08:38 +02:00
}
}