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;
|
2024-10-22 10:29:48 +00:00
|
|
|
use App\Models\InstanceSettings;
|
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;
|
2024-05-27 12:14:44 +00:00
|
|
|
use App\Models\StandaloneDragonfly;
|
|
|
|
|
use App\Models\StandaloneKeydb;
|
|
|
|
|
use App\Models\StandaloneMariadb;
|
|
|
|
|
use App\Models\StandaloneMongodb;
|
|
|
|
|
use App\Models\StandaloneMysql;
|
|
|
|
|
use App\Models\StandalonePostgresql;
|
|
|
|
|
use App\Models\StandaloneRedis;
|
2025-08-26 08:27:31 +00:00
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
2024-09-05 15:54:32 +00:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2024-09-23 17:51:31 +00:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
2025-11-04 08:18:05 +00:00
|
|
|
use Livewire\Attributes\Validate;
|
2024-09-23 17:51:31 +00:00
|
|
|
use Livewire\Component;
|
2023-09-22 19:31:47 +00:00
|
|
|
|
|
|
|
|
class FileStorage extends Component
|
|
|
|
|
{
|
2025-11-04 08:18:05 +00:00
|
|
|
use AuthorizesRequests;
|
2025-08-26 08:27:31 +00:00
|
|
|
|
2023-09-22 19:31:47 +00:00
|
|
|
public LocalFileVolume $fileStorage;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-05-27 12:14:44 +00:00
|
|
|
public ServiceApplication|StandaloneRedis|StandalonePostgresql|StandaloneMongodb|StandaloneMysql|StandaloneMariadb|StandaloneKeydb|StandaloneDragonfly|StandaloneClickhouse|ServiceDatabase|Application $resource;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-25 10:49:55 +00:00
|
|
|
public string $fs_path;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-30 13:08:40 +00:00
|
|
|
public ?string $workdir = null;
|
2023-09-22 19:31:47 +00:00
|
|
|
|
2024-08-05 18:00:57 +00:00
|
|
|
public bool $permanently_delete = true;
|
|
|
|
|
|
2025-10-03 14:39:57 +00:00
|
|
|
public bool $isReadOnly = false;
|
|
|
|
|
|
2025-11-04 08:18:05 +00:00
|
|
|
#[Validate(['nullable'])]
|
2025-10-13 13:38:59 +00:00
|
|
|
public ?string $content = null;
|
|
|
|
|
|
2025-11-04 08:18:05 +00:00
|
|
|
#[Validate(['required', 'boolean'])]
|
2025-10-13 13:38:59 +00:00
|
|
|
public bool $isBasedOnGit = false;
|
|
|
|
|
|
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',
|
2025-10-13 13:38:59 +00:00
|
|
|
'content' => 'nullable',
|
|
|
|
|
'isBasedOnGit' => 'required|boolean',
|
2023-09-22 19:31:47 +00:00
|
|
|
];
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-27 10:45:53 +00:00
|
|
|
public function mount()
|
|
|
|
|
{
|
2024-04-10 13:00:46 +00:00
|
|
|
$this->resource = $this->fileStorage->service;
|
2024-06-25 08:37:10 +00:00
|
|
|
if (str($this->fileStorage->fs_path)->startsWith('.')) {
|
2024-05-27 12:14:44 +00:00
|
|
|
$this->workdir = $this->resource->service?->workdir();
|
2024-06-25 08:37:10 +00:00
|
|
|
$this->fs_path = str($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
|
|
|
}
|
2025-10-03 14:39:57 +00:00
|
|
|
|
|
|
|
|
$this->isReadOnly = $this->fileStorage->isReadOnlyVolume();
|
2025-11-04 08:18:05 +00:00
|
|
|
$this->syncData();
|
2025-10-13 13:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-04 08:18:05 +00:00
|
|
|
public function syncData(bool $toModel = false): void
|
2025-10-13 13:38:59 +00:00
|
|
|
{
|
2025-11-04 08:18:05 +00:00
|
|
|
if ($toModel) {
|
|
|
|
|
$this->validate();
|
|
|
|
|
|
|
|
|
|
// Sync to model
|
|
|
|
|
$this->fileStorage->content = $this->content;
|
|
|
|
|
$this->fileStorage->is_based_on_git = $this->isBasedOnGit;
|
|
|
|
|
|
|
|
|
|
$this->fileStorage->save();
|
|
|
|
|
} else {
|
|
|
|
|
// Sync from model
|
|
|
|
|
$this->content = $this->fileStorage->content;
|
|
|
|
|
$this->isBasedOnGit = $this->fileStorage->is_based_on_git;
|
|
|
|
|
}
|
2023-09-25 10:49:55 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
|
|
|
|
public function convertToDirectory()
|
|
|
|
|
{
|
2024-04-15 17:47:17 +00:00
|
|
|
try {
|
2025-08-26 08:27:31 +00:00
|
|
|
$this->authorize('update', $this->resource);
|
|
|
|
|
|
2024-04-15 17:47:17 +00:00
|
|
|
$this->fileStorage->deleteStorageOnServer();
|
|
|
|
|
$this->fileStorage->is_directory = true;
|
|
|
|
|
$this->fileStorage->content = null;
|
2024-08-21 12:31:17 +00:00
|
|
|
$this->fileStorage->is_based_on_git = false;
|
2024-04-15 17:47:17 +00:00
|
|
|
$this->fileStorage->save();
|
|
|
|
|
$this->fileStorage->saveStorageOnServer();
|
2025-01-07 14:31:43 +00:00
|
|
|
} catch (\Throwable $e) {
|
2024-04-15 17:47:17 +00:00
|
|
|
return handleError($e, $this);
|
|
|
|
|
} finally {
|
2024-08-05 18:00:57 +00:00
|
|
|
$this->dispatch('refreshStorages');
|
2024-04-15 17:47:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2025-03-28 20:05:34 +00:00
|
|
|
public function loadStorageOnServer()
|
|
|
|
|
{
|
|
|
|
|
try {
|
2025-12-11 13:18:58 +00:00
|
|
|
// Loading content is a read operation, so we use 'view' permission
|
|
|
|
|
$this->authorize('view', $this->resource);
|
2025-08-26 08:27:31 +00:00
|
|
|
|
2025-03-28 20:05:34 +00:00
|
|
|
$this->fileStorage->loadStorageOnServer();
|
2025-11-04 08:18:05 +00:00
|
|
|
$this->syncData();
|
2025-03-28 20:05:34 +00:00
|
|
|
$this->dispatch('success', 'File storage loaded from server.');
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
return handleError($e, $this);
|
|
|
|
|
} finally {
|
|
|
|
|
$this->dispatch('refreshStorages');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-10 20:43:34 +00:00
|
|
|
public function convertToFile()
|
|
|
|
|
{
|
2024-04-15 17:47:17 +00:00
|
|
|
try {
|
2025-08-26 08:27:31 +00:00
|
|
|
$this->authorize('update', $this->resource);
|
|
|
|
|
|
2024-04-15 17:47:17 +00:00
|
|
|
$this->fileStorage->deleteStorageOnServer();
|
|
|
|
|
$this->fileStorage->is_directory = false;
|
|
|
|
|
$this->fileStorage->content = null;
|
2024-08-12 14:06:24 +00:00
|
|
|
if (data_get($this->resource, 'settings.is_preserve_repository_enabled')) {
|
|
|
|
|
$this->fileStorage->is_based_on_git = true;
|
|
|
|
|
}
|
2024-04-15 17:47:17 +00:00
|
|
|
$this->fileStorage->save();
|
|
|
|
|
$this->fileStorage->saveStorageOnServer();
|
2025-01-07 14:31:43 +00:00
|
|
|
} catch (\Throwable $e) {
|
2024-04-15 17:47:17 +00:00
|
|
|
return handleError($e, $this);
|
|
|
|
|
} finally {
|
2024-08-05 18:00:57 +00:00
|
|
|
$this->dispatch('refreshStorages');
|
2024-04-15 17:47:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-09-05 15:54:32 +00:00
|
|
|
public function delete($password)
|
2024-06-10 20:43:34 +00:00
|
|
|
{
|
2025-08-26 08:27:31 +00:00
|
|
|
$this->authorize('update', $this->resource);
|
|
|
|
|
|
2025-01-07 14:31:43 +00:00
|
|
|
if (! data_get(InstanceSettings::get(), 'disable_two_step_confirmation')) {
|
|
|
|
|
if (! Hash::check($password, Auth::user()->password)) {
|
|
|
|
|
$this->addError('password', 'The provided password is incorrect.');
|
2024-09-23 17:51:31 +00:00
|
|
|
|
2025-01-07 14:31:43 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2024-09-05 15:54:32 +00:00
|
|
|
}
|
|
|
|
|
|
2024-04-15 17:47:17 +00:00
|
|
|
try {
|
2024-08-05 18:00:57 +00:00
|
|
|
$message = 'File deleted.';
|
|
|
|
|
if ($this->fileStorage->is_directory) {
|
|
|
|
|
$message = 'Directory deleted.';
|
|
|
|
|
}
|
|
|
|
|
if ($this->permanently_delete) {
|
|
|
|
|
$message = 'Directory deleted from the server.';
|
|
|
|
|
$this->fileStorage->deleteStorageOnServer();
|
|
|
|
|
}
|
2024-04-15 17:47:17 +00:00
|
|
|
$this->fileStorage->delete();
|
2024-08-05 18:00:57 +00:00
|
|
|
$this->dispatch('success', $message);
|
2025-01-07 14:31:43 +00:00
|
|
|
} catch (\Throwable $e) {
|
2024-04-15 17:47:17 +00:00
|
|
|
return handleError($e, $this);
|
|
|
|
|
} finally {
|
2024-08-05 18:00:57 +00:00
|
|
|
$this->dispatch('refreshStorages');
|
2024-04-15 17:47:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-25 10:49:55 +00:00
|
|
|
public function submit()
|
|
|
|
|
{
|
2025-08-26 08:27:31 +00:00
|
|
|
$this->authorize('update', $this->resource);
|
|
|
|
|
|
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) {
|
2025-10-13 13:38:59 +00:00
|
|
|
$this->content = null;
|
2023-09-29 19:38:11 +00:00
|
|
|
}
|
2025-11-04 08:18:05 +00:00
|
|
|
// Sync component properties to model
|
|
|
|
|
$this->fileStorage->content = $this->content;
|
|
|
|
|
$this->fileStorage->is_based_on_git = $this->isBasedOnGit;
|
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.');
|
2025-01-07 14:31:43 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-09-29 19:38:11 +00:00
|
|
|
$this->fileStorage->setRawAttributes($original);
|
|
|
|
|
$this->fileStorage->save();
|
2025-11-04 08:18:05 +00:00
|
|
|
$this->syncData();
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-25 10:49:55 +00:00
|
|
|
return handleError($e, $this);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-27 10:45:53 +00:00
|
|
|
public function instantSave()
|
|
|
|
|
{
|
2023-09-26 12:45:52 +00:00
|
|
|
$this->submit();
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-09-23 17:51:31 +00:00
|
|
|
public function render()
|
2023-09-22 19:31:47 +00:00
|
|
|
{
|
2024-09-05 15:54:32 +00:00
|
|
|
return view('livewire.project.service.file-storage', [
|
|
|
|
|
'directoryDeletionCheckboxes' => [
|
|
|
|
|
['id' => 'permanently_delete', 'label' => 'The selected directory and all its contents will be permantely deleted form the server.'],
|
|
|
|
|
],
|
|
|
|
|
'fileDeletionCheckboxes' => [
|
|
|
|
|
['id' => 'permanently_delete', 'label' => 'The selected file will be permanently deleted form the server.'],
|
2024-09-23 17:51:31 +00:00
|
|
|
],
|
2024-09-05 15:54:32 +00:00
|
|
|
]);
|
2023-09-22 19:31:47 +00:00
|
|
|
}
|
|
|
|
|
}
|