Skip to content

Commit

Permalink
add Cleaning module (LycheeOrg#2349)
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria authored Apr 6, 2024
1 parent e047198 commit 4aa9fa9
Show file tree
Hide file tree
Showing 22 changed files with 244 additions and 17 deletions.
114 changes: 114 additions & 0 deletions app/Livewire/Components/Modules/Maintenance/Cleaning.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace App\Livewire\Components\Modules\Maintenance;

use App\Models\Configs;
use App\Policies\SettingsPolicy;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Log;
use Illuminate\View\View;
use Livewire\Attributes\Locked;
use Livewire\Component;
use function Safe\rmdir;
use function Safe\unlink;

/**
* When an upload/extract job fail, they tend to leave files behind.
* This provides the ability to cleans this up.
*/
class Cleaning extends Component
{
#[Locked] public array $result = [];
#[Locked] public string $path = '';
private array $skip = ['.gitignore'];

/**
* Mount depending of the path.
*
* @param string $path to check/clean
*
* @return void
*/
public function mount(string $path): void
{
Gate::authorize(SettingsPolicy::CAN_UPDATE, Configs::class);

$this->path = $path;
}

/**
* Rendering of the front-end.
*
* @return View
*/
public function render(): View
{
return view('livewire.modules.maintenance.cleaning');
}

/**
* Clean the path from all files excluding $this->skip.
*
* @return void
*/
public function do(): void
{
Gate::authorize(SettingsPolicy::CAN_UPDATE, Configs::class);

if ($this->getNoFileFoundProperty()) {
return;
}

$dirs = [];
$files = [];
foreach (new \DirectoryIterator($this->path) as $fileInfo) {
if ($fileInfo->isDot()) {
continue;
}
if (in_array($fileInfo->getFilename(), $this->skip, true)) {
continue;
}
$this->result[] = sprintf(__('maintenance.cleaning.result'), $fileInfo->getFilename());

if ($fileInfo->isDir()) {
$dirs[] = $fileInfo->getRealPath();
rmdir($fileInfo->getRealPath());
continue;
}
$files[] = $fileInfo->getRealPath();
unlink($fileInfo->getRealPath());
}
}

/**
* Check whether there are files to be removed.
* If not, we will not display the module to reduce complexity.
*
* @return bool
*/
public function getNoFileFoundProperty(): bool
{
if (!is_dir($this->path)) {
Log::warning('directory ' . $this->path . ' not found!');

return true;
}

if (!(new \FilesystemIterator($this->path))->valid()) {
return true;
}

$files_found = false;
foreach (new \DirectoryIterator($this->path) as $fileInfo) {
if ($fileInfo->isDot()) {
continue;
}
if (in_array($fileInfo->getFilename(), $this->skip, true)) {
continue;
}
$files_found = true;
}

return $files_found === false;
}
}
6 changes: 6 additions & 0 deletions lang/cz/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
*/
'title' => 'Maintenance',
'description' => 'You will find on this page, all the required actions to keep your Lychee installation running smooth and nicely.',
'cleaning' => [
'title' => 'Cleaning %s',
'result' => '%s deleted.',
'description' => 'Remove all contents from <span class="font-mono">%s</span>',
'button' => 'Clean',
],
'optimize' => [
'title' => 'Optimize Database',
'description' => 'If you notice slowdown in your installation, it may be because your database does not
Expand Down
6 changes: 6 additions & 0 deletions lang/de/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
*/
'title' => 'Maintenance',
'description' => 'You will find on this page, all the required actions to keep your Lychee installation running smooth and nicely.',
'cleaning' => [
'title' => 'Cleaning %s',
'result' => '%s deleted.',
'description' => 'Remove all contents from <span class="font-mono">%s</span>',
'button' => 'Clean',
],
'optimize' => [
'title' => 'Optimize Database',
'description' => 'If you notice slowdown in your installation, it may be because your database does not
Expand Down
7 changes: 6 additions & 1 deletion lang/el/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
*/
'title' => 'Maintenance',
'description' => 'You will find on this page, all the required actions to keep your Lychee installation running smooth and nicely.',

'cleaning' => [
'title' => 'Cleaning %s',
'result' => '%s deleted.',
'description' => 'Remove all contents from <span class="font-mono">%s</span>',
'button' => 'Clean',
],
'optimize' => [
'title' => 'Optimize Database',
'description' => 'If you notice slowdown in your installation, it may be because your database does not
Expand Down
7 changes: 6 additions & 1 deletion lang/en/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
*/
'title' => 'Maintenance',
'description' => 'You will find on this page, all the required actions to keep your Lychee installation running smooth and nicely.',

'cleaning' => [
'title' => 'Cleaning %s',
'result' => '%s deleted.',
'description' => 'Remove all contents from <span class="font-mono">%s</span>',
'button' => 'Clean',
],
'optimize' => [
'title' => 'Optimize Database',
'description' => 'If you notice slowdown in your installation, it may be because your database does not
Expand Down
7 changes: 6 additions & 1 deletion lang/es/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
*/
'title' => 'Maintenance',
'description' => 'You will find on this page, all the required actions to keep your Lychee installation running smooth and nicely.',

'cleaning' => [
'title' => 'Cleaning %s',
'result' => '%s deleted.',
'description' => 'Remove all contents from <span class="font-mono">%s</span>',
'button' => 'Clean',
],
'optimize' => [
'title' => 'Optimize Database',
'description' => 'If you notice slowdown in your installation, it may be because your database does not
Expand Down
6 changes: 6 additions & 0 deletions lang/fr/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
*/
'title' => 'Maintenance',
'description' => 'Vous trouverez sur cette page toutes les actions necessaire au bon fonctionment de Lychee.',
'cleaning' => [
'title' => 'Nettoyer %s',
'result' => '%s supprimé.',
'description' => 'Supprimer le contenu de <span class="font-mono">%s</span>',
'button' => 'Nettoyer',
],
'optimize' => [
'title' => 'Optimisation de la base de donnée',
'description' => 'Si vous remarquez que votre installation est devenue lente, il est possible que votre base de donnée n’ai pas les index requis.',
Expand Down
7 changes: 6 additions & 1 deletion lang/hu/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
*/
'title' => 'Maintenance',
'description' => 'You will find on this page, all the required actions to keep your Lychee installation running smooth and nicely.',

'cleaning' => [
'title' => 'Cleaning %s',
'result' => '%s deleted.',
'description' => 'Remove all contents from <span class="font-mono">%s</span>',
'button' => 'Clean',
],
'optimize' => [
'title' => 'Optimize Database',
'description' => 'If you notice slowdown in your installation, it may be because your database does not
Expand Down
7 changes: 6 additions & 1 deletion lang/it/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
*/
'title' => 'Maintenance',
'description' => 'You will find on this page, all the required actions to keep your Lychee installation running smooth and nicely.',

'cleaning' => [
'title' => 'Cleaning %s',
'result' => '%s deleted.',
'description' => 'Remove all contents from <span class="font-mono">%s</span>',
'button' => 'Clean',
],
'optimize' => [
'title' => 'Optimize Database',
'description' => 'If you notice slowdown in your installation, it may be because your database does not
Expand Down
7 changes: 6 additions & 1 deletion lang/nl/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
*/
'title' => 'Maintenance',
'description' => 'You will find on this page, all the required actions to keep your Lychee installation running smooth and nicely.',

'cleaning' => [
'title' => 'Cleaning %s',
'result' => '%s deleted.',
'description' => 'Remove all contents from <span class="font-mono">%s</span>',
'button' => 'Clean',
],
'optimize' => [
'title' => 'Optimize Database',
'description' => 'If you notice slowdown in your installation, it may be because your database does not
Expand Down
7 changes: 6 additions & 1 deletion lang/no/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
*/
'title' => 'Maintenance',
'description' => 'You will find on this page, all the required actions to keep your Lychee installation running smooth and nicely.',

'cleaning' => [
'title' => 'Cleaning %s',
'result' => '%s deleted.',
'description' => 'Remove all contents from <span class="font-mono">%s</span>',
'button' => 'Clean',
],
'optimize' => [
'title' => 'Optimize Database',
'description' => 'If you notice slowdown in your installation, it may be because your database does not
Expand Down
7 changes: 6 additions & 1 deletion lang/pl/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
*/
'title' => 'Maintenance',
'description' => 'You will find on this page, all the required actions to keep your Lychee installation running smooth and nicely.',

'cleaning' => [
'title' => 'Cleaning %s',
'result' => '%s deleted.',
'description' => 'Remove all contents from <span class="font-mono">%s</span>',
'button' => 'Clean',
],
'optimize' => [
'title' => 'Optimize Database',
'description' => 'If you notice slowdown in your installation, it may be because your database does not
Expand Down
7 changes: 6 additions & 1 deletion lang/pt/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
*/
'title' => 'Maintenance',
'description' => 'You will find on this page, all the required actions to keep your Lychee installation running smooth and nicely.',

'cleaning' => [
'title' => 'Cleaning %s',
'result' => '%s deleted.',
'description' => 'Remove all contents from <span class="font-mono">%s</span>',
'button' => 'Clean',
],
'optimize' => [
'title' => 'Optimize Database',
'description' => 'If you notice slowdown in your installation, it may be because your database does not
Expand Down
7 changes: 6 additions & 1 deletion lang/ru/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
*/
'title' => 'Maintenance',
'description' => 'You will find on this page, all the required actions to keep your Lychee installation running smooth and nicely.',

'cleaning' => [
'title' => 'Cleaning %s',
'result' => '%s deleted.',
'description' => 'Remove all contents from <span class="font-mono">%s</span>',
'button' => 'Clean',
],
'optimize' => [
'title' => 'Optimize Database',
'description' => 'If you notice slowdown in your installation, it may be because your database does not
Expand Down
7 changes: 6 additions & 1 deletion lang/sk/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
*/
'title' => 'Maintenance',
'description' => 'You will find on this page, all the required actions to keep your Lychee installation running smooth and nicely.',

'cleaning' => [
'title' => 'Cleaning %s',
'result' => '%s deleted.',
'description' => 'Remove all contents from <span class="font-mono">%s</span>',
'button' => 'Clean',
],
'optimize' => [
'title' => 'Optimize Database',
'description' => 'If you notice slowdown in your installation, it may be because your database does not
Expand Down
7 changes: 6 additions & 1 deletion lang/sv/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
*/
'title' => 'Maintenance',
'description' => 'You will find on this page, all the required actions to keep your Lychee installation running smooth and nicely.',

'cleaning' => [
'title' => 'Cleaning %s',
'result' => '%s deleted.',
'description' => 'Remove all contents from <span class="font-mono">%s</span>',
'button' => 'Clean',
],
'optimize' => [
'title' => 'Optimize Database',
'description' => 'If you notice slowdown in your installation, it may be because your database does not
Expand Down
7 changes: 6 additions & 1 deletion lang/vi/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
*/
'title' => 'Maintenance',
'description' => 'You will find on this page, all the required actions to keep your Lychee installation running smooth and nicely.',

'cleaning' => [
'title' => 'Cleaning %s',
'result' => '%s deleted.',
'description' => 'Remove all contents from <span class="font-mono">%s</span>',
'button' => 'Clean',
],
'optimize' => [
'title' => 'Optimize Database',
'description' => 'If you notice slowdown in your installation, it may be because your database does not
Expand Down
7 changes: 6 additions & 1 deletion lang/zh_CN/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
*/
'title' => 'Maintenance',
'description' => 'You will find on this page, all the required actions to keep your Lychee installation running smooth and nicely.',

'cleaning' => [
'title' => 'Cleaning %s',
'result' => '%s deleted.',
'description' => 'Remove all contents from <span class="font-mono">%s</span>',
'button' => 'Clean',
],
'optimize' => [
'title' => 'Optimize Database',
'description' => 'If you notice slowdown in your installation, it may be because your database does not
Expand Down
7 changes: 6 additions & 1 deletion lang/zh_TW/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
*/
'title' => 'Maintenance',
'description' => 'You will find on this page, all the required actions to keep your Lychee installation running smooth and nicely.',

'cleaning' => [
'title' => 'Cleaning %s',
'result' => '%s deleted.',
'description' => 'Remove all contents from <span class="font-mono">%s</span>',
'button' => 'Clean',
],
'optimize' => [
'title' => 'Optimize Database',
'description' => 'If you notice slowdown in your installation, it may be because your database does not
Expand Down
17 changes: 17 additions & 0 deletions resources/views/livewire/modules/maintenance/cleaning.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<x-maintenance.card :disabled="$this->no_file_found">
@if (count($result) > 0)
<pre class="text-2xs m-4">
@foreach ($result as $resultLine)
{{ $resultLine }}
@endforeach
</pre>
@else
<x-maintenance.h1>{{ sprintf(__('maintenance.cleaning.title'), str_replace(storage_path() . '/', '', $path)) }}</x-maintenance.h1>
<x-maintenance.p>{!! sprintf(__('maintenance.cleaning.description'), str_replace(base_path() . '/', '', $path)) !!}</x-maintenance.p>
<x-forms.buttons.danger class="absolute mt-4 bottom-0 border-t border-t-bg-800 rounded-br-md rounded-bl-md w-full !block"
wire:click="do" wire:loading.class="!hidden">
{{ __('maintenance.cleaning.button') }}
</x-forms.buttons.danger>
<x-maintenance.loading />
@endif
</x-maintenance.card>
3 changes: 3 additions & 0 deletions resources/views/livewire/pages/maintenance.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
</div>
<div class=" mt-9 grid grid-cols-1 gap-4 sm:grid-cols-2 sm:items-stretch md:grid-cols-3 md:gap-8 w-full">
<livewire:modules.maintenance.optimize />
<livewire:modules.maintenance.cleaning :path="config('filesystems.disks.extract-jobs.root')" />
<livewire:modules.maintenance.cleaning :path="config('filesystems.disks.image-jobs.root')" />
<livewire:modules.maintenance.cleaning :path="config('filesystems.disks.livewire-upload.root')" />
</div>
</div>
</div>
Expand Down
Loading

0 comments on commit 4aa9fa9

Please sign in to comment.