forked from LycheeOrg/Lychee
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Cleaning module (LycheeOrg#2349)
- Loading branch information
Showing
22 changed files
with
244 additions
and
17 deletions.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
app/Livewire/Components/Modules/Maintenance/Cleaning.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
resources/views/livewire/modules/maintenance/cleaning.blade.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.