WIP delete orphan files
This commit is contained in:
@@ -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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -148,7 +148,7 @@ class FileService
|
|||||||
return;
|
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;
|
if($file->isDot()) continue;
|
||||||
|
|
||||||
$namespace = app()->getNamespace();
|
$namespace = app()->getNamespace();
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ use Lucent\Commands\GenerateCollectionSchema;
|
|||||||
use Lucent\Commands\GenerateFileSchema;
|
use Lucent\Commands\GenerateFileSchema;
|
||||||
use Lucent\Commands\RebuildThumbnails;
|
use Lucent\Commands\RebuildThumbnails;
|
||||||
use Lucent\Commands\RemoveOrphanEdges;
|
use Lucent\Commands\RemoveOrphanEdges;
|
||||||
|
use Lucent\Commands\RemoveOrphanFiles;
|
||||||
use Lucent\Commands\RemoveOrphanRecords;
|
use Lucent\Commands\RemoveOrphanRecords;
|
||||||
use Lucent\Commands\Setup;
|
use Lucent\Commands\Setup;
|
||||||
use Lucent\Commands\SetupDatabase;
|
use Lucent\Commands\SetupDatabase;
|
||||||
@@ -95,6 +96,7 @@ class LucentServiceProvider extends ServiceProvider
|
|||||||
CompileSchemas::class,
|
CompileSchemas::class,
|
||||||
RebuildThumbnails::class,
|
RebuildThumbnails::class,
|
||||||
RemoveOrphanEdges::class,
|
RemoveOrphanEdges::class,
|
||||||
|
RemoveOrphanFiles::class,
|
||||||
RemoveOrphanRecords::class,
|
RemoveOrphanRecords::class,
|
||||||
SetupDatabase::class,
|
SetupDatabase::class,
|
||||||
GenerateCollectionSchema::class,
|
GenerateCollectionSchema::class,
|
||||||
|
|||||||
@@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
namespace Lucent\Record;
|
namespace Lucent\Record;
|
||||||
|
|
||||||
use Lucent\LucentException;
|
|
||||||
|
|
||||||
class QueryRecord
|
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
|
public static function fromRecord(Record $record): QueryRecord
|
||||||
{
|
{
|
||||||
return new QueryRecord(
|
return new QueryRecord(
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ readonly class RecordService
|
|||||||
public function createFromUrl(
|
public function createFromUrl(
|
||||||
string $url,
|
string $url,
|
||||||
RecordInputData $data,
|
RecordInputData $data,
|
||||||
array $edges
|
array $edges = []
|
||||||
): string
|
): string
|
||||||
{
|
{
|
||||||
$schema = $this->channelService->getSchema($data->schemaName)->get();
|
$schema = $this->channelService->getSchema($data->schemaName)->get();
|
||||||
@@ -70,7 +70,7 @@ readonly class RecordService
|
|||||||
public function createFromUploadedFile(
|
public function createFromUploadedFile(
|
||||||
UploadedFile $uploadedFile,
|
UploadedFile $uploadedFile,
|
||||||
RecordInputData $data,
|
RecordInputData $data,
|
||||||
array $edges
|
array $edges = []
|
||||||
): string
|
): string
|
||||||
{
|
{
|
||||||
$schema = $this->channelService->getSchema($data->schemaName)->get();
|
$schema = $this->channelService->getSchema($data->schemaName)->get();
|
||||||
@@ -87,7 +87,7 @@ readonly class RecordService
|
|||||||
public function createFromFileData(
|
public function createFromFileData(
|
||||||
FileData $fileData,
|
FileData $fileData,
|
||||||
RecordInputData $data,
|
RecordInputData $data,
|
||||||
array $edges
|
array $edges = []
|
||||||
): string
|
): string
|
||||||
{
|
{
|
||||||
$schema = $this->channelService->getSchema($data->schemaName)->get();
|
$schema = $this->channelService->getSchema($data->schemaName)->get();
|
||||||
@@ -281,12 +281,9 @@ readonly class RecordService
|
|||||||
string $schemaName,
|
string $schemaName,
|
||||||
): void
|
): void
|
||||||
{
|
{
|
||||||
|
|
||||||
$schema = $this->channelService->getSchema($schemaName)->get();
|
|
||||||
$this->recordRepo->deleteTrashedBySchema($schemaName);
|
$this->recordRepo->deleteTrashedBySchema($schemaName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws LucentException
|
* @throws LucentException
|
||||||
* @throws ValidatorException
|
* @throws ValidatorException
|
||||||
|
|||||||
Reference in New Issue
Block a user