BookStack/app/Console/Commands/CleanupImagesCommand.php

77 lines
2.2 KiB
PHP
Raw Normal View History

<?php
namespace BookStack\Console\Commands;
use BookStack\Uploads\ImageService;
use Illuminate\Console\Command;
use Symfony\Component\Console\Output\OutputInterface;
class CleanupImagesCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'bookstack:cleanup-images
{--a|all : Also delete images that are only used in old revisions}
{--f|force : Actually run the deletions, Defaults to a dry-run}
';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Cleanup images and drawings';
/**
* Execute the console command.
*/
public function handle(ImageService $imageService): int
{
$checkRevisions = !$this->option('all');
$dryRun = !$this->option('force');
if (!$dryRun) {
2023-05-24 05:34:43 -04:00
$this->warn("This operation is destructive and is not guaranteed to be fully accurate.\nEnsure you have a backup of your images.\n");
$proceed = !$this->input->isInteractive() || $this->confirm("Are you sure you want to proceed?");
if (!$proceed) {
return 0;
}
}
$deleted = $imageService->deleteUnusedImages($checkRevisions, $dryRun);
$deleteCount = count($deleted);
if ($dryRun) {
2023-05-24 05:34:43 -04:00
$this->comment('Dry run, no images have been deleted');
$this->comment($deleteCount . ' image(s) found that would have been deleted');
$this->showDeletedImages($deleted);
$this->comment('Run with -f or --force to perform deletions');
2021-06-26 11:23:15 -04:00
return 0;
}
$this->showDeletedImages($deleted);
$this->comment("{$deleteCount} image(s) deleted");
return 0;
}
protected function showDeletedImages($paths): void
{
2018-09-21 13:48:47 -04:00
if ($this->getOutput()->getVerbosity() <= OutputInterface::VERBOSITY_NORMAL) {
return;
}
if (count($paths) > 0) {
$this->line('Image(s) to delete:');
}
foreach ($paths as $path) {
$this->line($path);
}
}
}