From e9537862cada8f23951fec354a704065d47792c5 Mon Sep 17 00:00:00 2001 From: lexx Date: Wed, 9 Oct 2024 23:04:01 +0300 Subject: [PATCH] remove deleted files --- src/Commands/RemoveOrphanFiles.php | 33 ++++++++++++++++-------------- src/File/FileService.php | 19 ++++++++--------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/Commands/RemoveOrphanFiles.php b/src/Commands/RemoveOrphanFiles.php index 6b535e4..a077a01 100644 --- a/src/Commands/RemoveOrphanFiles.php +++ b/src/Commands/RemoveOrphanFiles.php @@ -4,44 +4,47 @@ namespace Lucent\Commands; use Illuminate\Console\Command; use Lucent\Channel\ChannelService; -use Lucent\Database\Database; use Lucent\File\FileService; +use Lucent\Query\Query; +use Lucent\Record\Status; use Lucent\Schema\FilesSchema; use Lucent\Schema\Schema; class RemoveOrphanFiles extends Command { - protected $signature = 'lucent:remove-orphan-files {--force}'; - protected $description = 'Searches and removes actual files that are not included in records'; - public function __construct() { parent::__construct(); } - - public function handle(ChannelService $channelService, FileService $fileService): void + public function handle(ChannelService $channelService, FileService $fileService, Query $query): void { $schemas = $channelService->channel->schemas->filter(fn(Schema $schema) => get_class($schema) === FilesSchema::class)->values(); - - foreach ($schemas as $schema){ + $filesToDelete = []; + foreach ($schemas as $schema) { $disk = $fileService->loadDisk($schema); + $filesToDelete = collect($disk->files($schema->path)) + ->filter( + fn(string $file) => $query->filter(["schema" => $schema->name, "_file.path" => $file])->status([Status::DRAFT, Status::PUBLISHED, Status::TRASHED])->run()->records->isEmpty() + ) + ->map(fn(string $file) => fn() => $disk->delete($file)) + ->values() + ->merge($filesToDelete); } - - dd($schemas); - $count = Database::make()->table("revisions")->whereNotIn("schema",$schemas)->count(); - if (!($this->option('force') || $this->confirm("$count records found. Delete?"))) { + $count = $filesToDelete->count(); + if (!($this->option('force') || $this->confirm("$count files found. Delete?"))) { return; } - $numOfRowsDeleted = Database::make()->table("records")->whereNotIn("schema",$schemas)->delete(); - Database::make()->table("revisions")->whereNotIn("schema",$schemas)->delete(); - $this->info("$numOfRowsDeleted records were deleted"); + foreach ($filesToDelete as $op) { + $op(); + } + $this->info("$count files were deleted"); } } diff --git a/src/File/FileService.php b/src/File/FileService.php index 19cec15..9f22825 100644 --- a/src/File/FileService.php +++ b/src/File/FileService.php @@ -19,7 +19,6 @@ use Lucent\Record\QueryRecord; use Lucent\Schema\FilesSchema; use Lucent\Schema\Schema; use Spatie\ImageOptimizer\OptimizerChainFactory; -use Symfony\Component\Finder\Finder; class FileService { @@ -83,15 +82,13 @@ class FileService throw new LucentException("File $filename not uploaded"); } - if($this->isImage($mimetype)){ + if ($this->isImage($mimetype)) { $this->createTemplates($disk, $path); } list($width, $height) = $this->isImage($mimetype) ? getimagesize($file) : [0, 0]; - - $recordFile = new RecordFile( originalName: $originalFilename, mime: $mimetype, @@ -143,20 +140,20 @@ class FileService { try { $originalImage = $this->imageManager->read($disk->get($path)); - }catch (Exception $exception){ + } catch (Exception $exception) { $this->logger->error($exception->getMessage()); return; } foreach (new DirectoryIterator(base_path(config("lucent.image_filters_path"))) as $file) { - if($file->isDot()) continue; + if ($file->isDot()) continue; $namespace = app()->getNamespace(); - $filterClass = $namespace.str_replace( - ['/', '.php'], - ['\\', ''], - Str::after($file->getRealPath(), realpath(app_path()).DIRECTORY_SEPARATOR) - ); + $filterClass = $namespace . str_replace( + ['/', '.php'], + ['\\', ''], + Str::after($file->getRealPath(), realpath(app_path()) . DIRECTORY_SEPARATOR) + ); $image = $originalImage->modify(new $filterClass); $templateUri = "/templates/" . (new $filterClass)->name . "/" . $path;