3 namespace BookStack\Console\Commands;
5 use BookStack\Uploads\ImageService;
6 use Illuminate\Console\Command;
7 use Symfony\Component\Console\Output\OutputInterface;
9 class CleanupImagesCommand extends Command
12 * The name and signature of the console command.
16 protected $signature = 'bookstack:cleanup-images
17 {--a|all : Also delete images that are only used in old revisions}
18 {--f|force : Actually run the deletions, Defaults to a dry-run}
22 * The console command description.
26 protected $description = 'Cleanup images and drawings';
29 * Execute the console command.
31 public function handle(ImageService $imageService): int
33 $checkRevisions = !$this->option('all');
34 $dryRun = !$this->option('force');
37 $this->warn("This operation is destructive and is not guaranteed to be fully accurate.\nEnsure you have a backup of your images.\n");
38 $proceed = !$this->input->isInteractive() || $this->confirm("Are you sure you want to proceed?");
44 $deleted = $imageService->deleteUnusedImages($checkRevisions, $dryRun);
45 $deleteCount = count($deleted);
48 $this->comment('Dry run, no images have been deleted');
49 $this->comment($deleteCount . ' image(s) found that would have been deleted');
50 $this->showDeletedImages($deleted);
51 $this->comment('Run with -f or --force to perform deletions');
56 $this->showDeletedImages($deleted);
57 $this->comment("{$deleteCount} image(s) deleted");
62 protected function showDeletedImages($paths): void
64 if ($this->getOutput()->getVerbosity() <= OutputInterface::VERBOSITY_NORMAL) {
68 if (count($paths) > 0) {
69 $this->line('Image(s) to delete:');
72 foreach ($paths as $path) {