remove deleted files
This commit is contained in:
@@ -4,44 +4,47 @@ namespace Lucent\Commands;
|
|||||||
|
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Lucent\Channel\ChannelService;
|
use Lucent\Channel\ChannelService;
|
||||||
use Lucent\Database\Database;
|
|
||||||
use Lucent\File\FileService;
|
use Lucent\File\FileService;
|
||||||
|
use Lucent\Query\Query;
|
||||||
|
use Lucent\Record\Status;
|
||||||
use Lucent\Schema\FilesSchema;
|
use Lucent\Schema\FilesSchema;
|
||||||
use Lucent\Schema\Schema;
|
use Lucent\Schema\Schema;
|
||||||
|
|
||||||
class RemoveOrphanFiles extends Command
|
class RemoveOrphanFiles extends Command
|
||||||
{
|
{
|
||||||
|
|
||||||
protected $signature = 'lucent:remove-orphan-files {--force}';
|
protected $signature = 'lucent:remove-orphan-files {--force}';
|
||||||
|
|
||||||
protected $description = 'Searches and removes actual files that are not included in records';
|
protected $description = 'Searches and removes actual files that are not included in records';
|
||||||
|
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function handle(ChannelService $channelService, FileService $fileService, Query $query): void
|
||||||
public function handle(ChannelService $channelService, FileService $fileService): void
|
|
||||||
{
|
{
|
||||||
$schemas = $channelService->channel->schemas->filter(fn(Schema $schema) => get_class($schema) === FilesSchema::class)->values();
|
$schemas = $channelService->channel->schemas->filter(fn(Schema $schema) => get_class($schema) === FilesSchema::class)->values();
|
||||||
|
$filesToDelete = [];
|
||||||
foreach ($schemas as $schema){
|
foreach ($schemas as $schema) {
|
||||||
$disk = $fileService->loadDisk($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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$count = $filesToDelete->count();
|
||||||
dd($schemas);
|
if (!($this->option('force') || $this->confirm("$count files found. Delete?"))) {
|
||||||
$count = Database::make()->table("revisions")->whereNotIn("schema",$schemas)->count();
|
|
||||||
if (!($this->option('force') || $this->confirm("$count records found. Delete?"))) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$numOfRowsDeleted = Database::make()->table("records")->whereNotIn("schema",$schemas)->delete();
|
foreach ($filesToDelete as $op) {
|
||||||
Database::make()->table("revisions")->whereNotIn("schema",$schemas)->delete();
|
$op();
|
||||||
$this->info("$numOfRowsDeleted records were deleted");
|
}
|
||||||
|
$this->info("$count files were deleted");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ use Lucent\Record\QueryRecord;
|
|||||||
use Lucent\Schema\FilesSchema;
|
use Lucent\Schema\FilesSchema;
|
||||||
use Lucent\Schema\Schema;
|
use Lucent\Schema\Schema;
|
||||||
use Spatie\ImageOptimizer\OptimizerChainFactory;
|
use Spatie\ImageOptimizer\OptimizerChainFactory;
|
||||||
use Symfony\Component\Finder\Finder;
|
|
||||||
|
|
||||||
class FileService
|
class FileService
|
||||||
{
|
{
|
||||||
@@ -83,15 +82,13 @@ class FileService
|
|||||||
throw new LucentException("File $filename not uploaded");
|
throw new LucentException("File $filename not uploaded");
|
||||||
}
|
}
|
||||||
|
|
||||||
if($this->isImage($mimetype)){
|
if ($this->isImage($mimetype)) {
|
||||||
$this->createTemplates($disk, $path);
|
$this->createTemplates($disk, $path);
|
||||||
}
|
}
|
||||||
|
|
||||||
list($width, $height) = $this->isImage($mimetype) ? getimagesize($file) : [0, 0];
|
list($width, $height) = $this->isImage($mimetype) ? getimagesize($file) : [0, 0];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$recordFile = new RecordFile(
|
$recordFile = new RecordFile(
|
||||||
originalName: $originalFilename,
|
originalName: $originalFilename,
|
||||||
mime: $mimetype,
|
mime: $mimetype,
|
||||||
@@ -143,20 +140,20 @@ class FileService
|
|||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$originalImage = $this->imageManager->read($disk->get($path));
|
$originalImage = $this->imageManager->read($disk->get($path));
|
||||||
}catch (Exception $exception){
|
} catch (Exception $exception) {
|
||||||
$this->logger->error($exception->getMessage());
|
$this->logger->error($exception->getMessage());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (new DirectoryIterator(base_path(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();
|
||||||
$filterClass = $namespace.str_replace(
|
$filterClass = $namespace . str_replace(
|
||||||
['/', '.php'],
|
['/', '.php'],
|
||||||
['\\', ''],
|
['\\', ''],
|
||||||
Str::after($file->getRealPath(), realpath(app_path()).DIRECTORY_SEPARATOR)
|
Str::after($file->getRealPath(), realpath(app_path()) . DIRECTORY_SEPARATOR)
|
||||||
);
|
);
|
||||||
|
|
||||||
$image = $originalImage->modify(new $filterClass);
|
$image = $originalImage->modify(new $filterClass);
|
||||||
$templateUri = "/templates/" . (new $filterClass)->name . "/" . $path;
|
$templateUri = "/templates/" . (new $filterClass)->name . "/" . $path;
|
||||||
|
|||||||
Reference in New Issue
Block a user