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

143 lines
5.1 KiB
PHP
Raw Normal View History

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