2023-09-22 19:31:47 +00:00
|
|
|
<?php
|
|
|
|
|
2023-12-07 18:06:32 +00:00
|
|
|
namespace App\Livewire\Project\Service;
|
2023-09-22 19:31:47 +00:00
|
|
|
|
2024-04-15 17:47:17 +00:00
|
|
|
use App\Models\Application;
|
2023-09-22 19:31:47 +00:00
|
|
|
use App\Models\LocalFileVolume;
|
2023-09-25 10:49:55 +00:00
|
|
|
use App\Models\ServiceApplication;
|
|
|
|
use App\Models\ServiceDatabase;
|
2024-04-10 13:00:46 +00:00
|
|
|
use App\Models\StandaloneClickhouse;
|
2023-09-22 19:31:47 +00:00
|
|
|
use Livewire\Component;
|
2023-09-25 10:49:55 +00:00
|
|
|
use Illuminate\Support\Str;
|
2023-09-22 19:31:47 +00:00
|
|
|
|
|
|
|
class FileStorage extends Component
|
|
|
|
{
|
|
|
|
public LocalFileVolume $fileStorage;
|
2024-04-15 17:47:17 +00:00
|
|
|
public ServiceApplication|ServiceDatabase|StandaloneClickhouse|Application $resource;
|
2023-09-25 10:49:55 +00:00
|
|
|
public string $fs_path;
|
2023-09-30 13:08:40 +00:00
|
|
|
public ?string $workdir = null;
|
2023-09-22 19:31:47 +00:00
|
|
|
|
|
|
|
protected $rules = [
|
2023-09-26 12:45:52 +00:00
|
|
|
'fileStorage.is_directory' => 'required',
|
2023-09-22 19:31:47 +00:00
|
|
|
'fileStorage.fs_path' => 'required',
|
|
|
|
'fileStorage.mount_path' => 'required',
|
|
|
|
'fileStorage.content' => 'nullable',
|
|
|
|
];
|
2023-09-27 10:45:53 +00:00
|
|
|
public function mount()
|
|
|
|
{
|
2024-04-10 13:00:46 +00:00
|
|
|
$this->resource = $this->fileStorage->service;
|
|
|
|
if (Str::of($this->fileStorage->fs_path)->startsWith('.')) {
|
|
|
|
$this->workdir = $this->resource->service->workdir();
|
2023-09-30 13:08:40 +00:00
|
|
|
$this->fs_path = Str::of($this->fileStorage->fs_path)->after('.');
|
2024-04-10 13:00:46 +00:00
|
|
|
} else {
|
2023-09-30 13:08:40 +00:00
|
|
|
$this->workdir = null;
|
|
|
|
$this->fs_path = $this->fileStorage->fs_path;
|
2024-04-10 13:00:46 +00:00
|
|
|
}
|
2023-09-25 10:49:55 +00:00
|
|
|
}
|
2024-04-15 17:47:17 +00:00
|
|
|
public function convertToDirectory() {
|
|
|
|
try {
|
|
|
|
$this->fileStorage->deleteStorageOnServer();
|
|
|
|
$this->fileStorage->is_directory = true;
|
|
|
|
$this->fileStorage->content = null;
|
|
|
|
$this->fileStorage->save();
|
|
|
|
$this->fileStorage->saveStorageOnServer();
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
return handleError($e, $this);
|
|
|
|
} finally {
|
|
|
|
$this->dispatch('storagesChanged');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public function convertToFile() {
|
|
|
|
try {
|
|
|
|
$this->fileStorage->deleteStorageOnServer();
|
|
|
|
$this->fileStorage->is_directory = false;
|
|
|
|
$this->fileStorage->content = null;
|
|
|
|
$this->fileStorage->save();
|
|
|
|
$this->fileStorage->saveStorageOnServer();
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
return handleError($e, $this);
|
|
|
|
} finally {
|
|
|
|
$this->dispatch('storagesChanged');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public function delete() {
|
|
|
|
try {
|
|
|
|
$this->fileStorage->deleteStorageOnServer();
|
|
|
|
$this->fileStorage->delete();
|
|
|
|
$this->dispatch('success', 'File deleted.');
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
return handleError($e, $this);
|
|
|
|
} finally {
|
|
|
|
$this->dispatch('storagesChanged');
|
|
|
|
}
|
|
|
|
}
|
2023-09-25 10:49:55 +00:00
|
|
|
public function submit()
|
|
|
|
{
|
2023-09-29 19:38:11 +00:00
|
|
|
$original = $this->fileStorage->getOriginal();
|
2023-09-25 10:49:55 +00:00
|
|
|
try {
|
|
|
|
$this->validate();
|
2023-09-29 19:38:11 +00:00
|
|
|
if ($this->fileStorage->is_directory) {
|
|
|
|
$this->fileStorage->content = null;
|
|
|
|
}
|
2023-09-25 10:49:55 +00:00
|
|
|
$this->fileStorage->save();
|
2024-02-14 14:00:24 +00:00
|
|
|
$this->fileStorage->saveStorageOnServer();
|
2024-02-22 13:53:42 +00:00
|
|
|
$this->dispatch('success', 'File updated.');
|
2023-09-25 10:49:55 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-09-29 19:38:11 +00:00
|
|
|
$this->fileStorage->setRawAttributes($original);
|
|
|
|
$this->fileStorage->save();
|
2023-09-25 10:49:55 +00:00
|
|
|
return handleError($e, $this);
|
|
|
|
}
|
|
|
|
}
|
2023-09-27 10:45:53 +00:00
|
|
|
public function instantSave()
|
|
|
|
{
|
2023-09-26 12:45:52 +00:00
|
|
|
$this->submit();
|
|
|
|
}
|
2023-09-22 19:31:47 +00:00
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
return view('livewire.project.service.file-storage');
|
|
|
|
}
|
|
|
|
}
|