remove deleted files

This commit is contained in:
2024-10-09 23:04:01 +03:00
parent fda4bced92
commit e9537862ca
2 changed files with 26 additions and 26 deletions
+17 -14
View File
@@ -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();
$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");
}
}
-3
View File
@@ -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
{
@@ -90,8 +89,6 @@ class FileService
list($width, $height) = $this->isImage($mimetype) ? getimagesize($file) : [0, 0];
$recordFile = new RecordFile(
originalName: $originalFilename,
mime: $mimetype,