WIP delete orphan files

This commit is contained in:
2024-10-09 16:57:19 +03:00
parent c3e6f9ff64
commit fda4bced92
5 changed files with 68 additions and 9 deletions
+47
View File
@@ -0,0 +1,47 @@
<?php
namespace Lucent\Commands;
use Illuminate\Console\Command;
use Lucent\Channel\ChannelService;
use Lucent\Database\Database;
use Lucent\File\FileService;
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
{
$schemas = $channelService->channel->schemas->filter(fn(Schema $schema) => get_class($schema) === FilesSchema::class)->values();
foreach ($schemas as $schema){
$disk = $fileService->loadDisk($schema);
}
dd($schemas);
$count = Database::make()->table("revisions")->whereNotIn("schema",$schemas)->count();
if (!($this->option('force') || $this->confirm("$count records 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");
}
}
+1 -1
View File
@@ -148,7 +148,7 @@ class FileService
return;
}
foreach (new DirectoryIterator(config("lucent.image_filters_path")) as $file) {
foreach (new DirectoryIterator(base_path(config("lucent.image_filters_path"))) as $file) {
if($file->isDot()) continue;
$namespace = app()->getNamespace();
+2
View File
@@ -17,6 +17,7 @@ use Lucent\Commands\GenerateCollectionSchema;
use Lucent\Commands\GenerateFileSchema;
use Lucent\Commands\RebuildThumbnails;
use Lucent\Commands\RemoveOrphanEdges;
use Lucent\Commands\RemoveOrphanFiles;
use Lucent\Commands\RemoveOrphanRecords;
use Lucent\Commands\Setup;
use Lucent\Commands\SetupDatabase;
@@ -95,6 +96,7 @@ class LucentServiceProvider extends ServiceProvider
CompileSchemas::class,
RebuildThumbnails::class,
RemoveOrphanEdges::class,
RemoveOrphanFiles::class,
RemoveOrphanRecords::class,
SetupDatabase::class,
GenerateCollectionSchema::class,
+15 -2
View File
@@ -2,8 +2,6 @@
namespace Lucent\Record;
use Lucent\LucentException;
class QueryRecord
{
@@ -21,6 +19,21 @@ class QueryRecord
{
}
public function getChildren(string $field): array
{
return $this->_children[$field];
}
public function getFirstChild(string $field): ?QueryRecord
{
return $this->_children[$field][0] ?? null;
}
public function hasChildren(string $field): bool
{
return isset($this->_children[$field]) && count($this->_children[$field]) > 0;
}
public static function fromRecord(Record $record): QueryRecord
{
return new QueryRecord(
+3 -6
View File
@@ -53,7 +53,7 @@ readonly class RecordService
public function createFromUrl(
string $url,
RecordInputData $data,
array $edges
array $edges = []
): string
{
$schema = $this->channelService->getSchema($data->schemaName)->get();
@@ -70,7 +70,7 @@ readonly class RecordService
public function createFromUploadedFile(
UploadedFile $uploadedFile,
RecordInputData $data,
array $edges
array $edges = []
): string
{
$schema = $this->channelService->getSchema($data->schemaName)->get();
@@ -87,7 +87,7 @@ readonly class RecordService
public function createFromFileData(
FileData $fileData,
RecordInputData $data,
array $edges
array $edges = []
): string
{
$schema = $this->channelService->getSchema($data->schemaName)->get();
@@ -281,12 +281,9 @@ readonly class RecordService
string $schemaName,
): void
{
$schema = $this->channelService->getSchema($schemaName)->get();
$this->recordRepo->deleteTrashedBySchema($schemaName);
}
/**
* @throws LucentException
* @throws ValidatorException