Files
coolify/app/Livewire/Project/Service/FileStorage.php
T

216 lines
7.0 KiB
PHP
Raw Normal View History

2023-09-22 21:31:47 +02:00
<?php
2023-12-07 19:06:32 +01:00
namespace App\Livewire\Project\Service;
2023-09-22 21:31:47 +02:00
use App\Models\Application;
2023-09-22 21:31:47 +02:00
use App\Models\LocalFileVolume;
2023-09-25 12:49:55 +02:00
use App\Models\ServiceApplication;
use App\Models\ServiceDatabase;
2024-04-10 15:00:46 +02:00
use App\Models\StandaloneClickhouse;
2024-05-27 14:14:44 +02: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;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Livewire\Attributes\Validate;
2024-09-23 19:51:31 +02:00
use Livewire\Component;
2023-09-22 21:31:47 +02:00
class FileStorage extends Component
{
use AuthorizesRequests;
2023-09-22 21:31:47 +02:00
public LocalFileVolume $fileStorage;
2024-06-10 20:43:34 +00:00
2024-05-27 14:14:44 +02: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 12:49:55 +02:00
public string $fs_path;
2024-06-10 20:43:34 +00:00
2023-09-30 15:08:40 +02:00
public ?string $workdir = null;
2023-09-22 21:31:47 +02:00
public bool $permanently_delete = true;
public bool $isReadOnly = false;
#[Validate(['nullable'])]
public ?string $content = null;
#[Validate(['required', 'boolean'])]
public bool $isBasedOnGit = false;
#[Validate(['required', 'boolean'])]
public bool $isPreviewSuffixEnabled = true;
2023-09-22 21:31:47 +02:00
protected $rules = [
2023-09-26 14:45:52 +02:00
'fileStorage.is_directory' => 'required',
2023-09-22 21:31:47 +02:00
'fileStorage.fs_path' => 'required',
'fileStorage.mount_path' => 'required',
'content' => 'nullable',
'isBasedOnGit' => 'required|boolean',
'isPreviewSuffixEnabled' => 'required|boolean',
2023-09-22 21:31:47 +02:00
];
2024-06-10 20:43:34 +00:00
2023-09-27 12:45:53 +02:00
public function mount()
{
2024-04-10 15:00:46 +02:00
$this->resource = $this->fileStorage->service;
if (str($this->fileStorage->fs_path)->startsWith('.')) {
2024-05-27 14:14:44 +02:00
$this->workdir = $this->resource->service?->workdir();
$this->fs_path = str($this->fileStorage->fs_path)->after('.');
2024-04-10 15:00:46 +02:00
} else {
2023-09-30 15:08:40 +02:00
$this->workdir = null;
$this->fs_path = $this->fileStorage->fs_path;
2024-04-10 15:00:46 +02:00
}
$this->isReadOnly = $this->fileStorage->shouldBeReadOnlyInUI();
$this->syncData();
}
public function syncData(bool $toModel = false): void
{
if ($toModel) {
$this->validate();
// Sync to model
$this->fileStorage->content = $this->content;
$this->fileStorage->is_based_on_git = $this->isBasedOnGit;
$this->fileStorage->is_preview_suffix_enabled = $this->isPreviewSuffixEnabled;
$this->fileStorage->save();
} else {
// Sync from model
$this->content = $this->fileStorage->content;
$this->isBasedOnGit = $this->fileStorage->is_based_on_git;
$this->isPreviewSuffixEnabled = $this->fileStorage->is_preview_suffix_enabled ?? true;
}
2023-09-25 12:49:55 +02:00
}
2024-06-10 20:43:34 +00:00
public function convertToDirectory()
{
try {
$this->authorize('update', $this->resource);
$this->fileStorage->deleteStorageOnServer();
$this->fileStorage->is_directory = true;
$this->fileStorage->content = null;
2024-08-21 14:31:17 +02:00
$this->fileStorage->is_based_on_git = false;
$this->fileStorage->save();
$this->fileStorage->saveStorageOnServer();
2025-01-07 15:31:43 +01:00
} catch (\Throwable $e) {
return handleError($e, $this);
} finally {
$this->dispatch('refreshStorages');
}
}
2024-06-10 20:43:34 +00:00
public function loadStorageOnServer()
{
try {
// Loading content is a read operation, so we use 'view' permission
$this->authorize('view', $this->resource);
$this->fileStorage->loadStorageOnServer();
$this->syncData();
$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()
{
try {
$this->authorize('update', $this->resource);
$this->fileStorage->deleteStorageOnServer();
$this->fileStorage->is_directory = false;
$this->fileStorage->content = null;
if (data_get($this->resource, 'settings.is_preserve_repository_enabled')) {
$this->fileStorage->is_based_on_git = true;
}
$this->fileStorage->save();
$this->fileStorage->saveStorageOnServer();
2025-01-07 15:31:43 +01:00
} catch (\Throwable $e) {
return handleError($e, $this);
} finally {
$this->dispatch('refreshStorages');
}
}
2024-06-10 20:43:34 +00:00
public function delete($password, $selectedActions = [])
2024-06-10 20:43:34 +00:00
{
$this->authorize('update', $this->resource);
if (! verifyPasswordConfirmation($password, $this)) {
return 'The provided password is incorrect.';
2024-09-05 17:54:32 +02:00
}
try {
$message = 'File deleted.';
if ($this->fileStorage->is_directory) {
$message = 'Directory deleted.';
}
if ($this->permanently_delete) {
$message = 'Directory deleted from the server.';
$this->fileStorage->deleteStorageOnServer();
}
$this->fileStorage->delete();
$this->dispatch('success', $message);
2025-01-07 15:31:43 +01:00
} catch (\Throwable $e) {
return handleError($e, $this);
} finally {
$this->dispatch('refreshStorages');
}
return true;
}
2024-06-10 20:43:34 +00:00
2023-09-25 12:49:55 +02:00
public function submit()
{
$this->authorize('update', $this->resource);
$original = $this->fileStorage->getOriginal();
2023-09-25 12:49:55 +02:00
try {
$this->validate();
if ($this->fileStorage->is_directory) {
$this->content = null;
}
// Sync component properties to model
$this->fileStorage->content = $this->content;
$this->fileStorage->is_based_on_git = $this->isBasedOnGit;
$this->fileStorage->is_preview_suffix_enabled = $this->isPreviewSuffixEnabled;
2023-09-25 12:49:55 +02:00
$this->fileStorage->save();
2024-02-14 15:00:24 +01:00
$this->fileStorage->saveStorageOnServer();
2024-02-22 14:53:42 +01:00
$this->dispatch('success', 'File updated.');
2025-01-07 15:31:43 +01:00
} catch (\Throwable $e) {
$this->fileStorage->setRawAttributes($original);
$this->fileStorage->save();
$this->syncData();
2024-06-10 20:43:34 +00:00
2023-09-25 12:49:55 +02:00
return handleError($e, $this);
}
}
2024-06-10 20:43:34 +00:00
public function instantSave(): void
2023-09-27 12:45:53 +02:00
{
$this->authorize('update', $this->resource);
$this->syncData(true);
$this->dispatch('success', 'File updated.');
2023-09-26 14:45:52 +02:00
}
2024-06-10 20:43:34 +00:00
2024-09-23 19:51:31 +02:00
public function render()
2023-09-22 21:31:47 +02:00
{
2024-09-05 17:54:32 +02: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 19:51:31 +02:00
],
2024-09-05 17:54:32 +02:00
]);
2023-09-22 21:31:47 +02:00
}
}