remove deleted files
This commit is contained in:
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user