3 namespace BookStack\Console\Commands;
5 use BookStack\Services\ImageService;
6 use Illuminate\Console\Command;
7 use Symfony\Component\Console\Output\OutputInterface;
9 class CleanupImages extends Command
12 * The name and signature of the console command.
16 protected $signature = 'bookstack:cleanup-images
17 {--a|all : Include images that are used in page revisions}
18 {--f|force : Actually run the deletions}
22 * The console command description.
26 protected $description = 'Cleanup images and drawings';
29 protected $imageService;
32 * Create a new command instance.
33 * @param ImageService $imageService
35 public function __construct(ImageService $imageService)
37 $this->imageService = $imageService;
38 parent::__construct();
42 * Execute the console command.
46 public function handle()
48 $checkRevisions = $this->option('all') ? false : true;
49 $dryRun = $this->option('force') ? false : true;
52 $proceed = $this->confirm("This operation is destructive and is not guaranteed to be fully accurate.\nEnsure you have a backup of your images.\nAre you sure you want to proceed?");
58 $deleted = $this->imageService->deleteUnusedImages($checkRevisions, $dryRun);
59 $deleteCount = count($deleted);
62 $this->comment('Dry run, No images have been deleted');
63 $this->comment($deleteCount . ' images found that would have been deleted');
64 $this->showDeletedImages($deleted);
65 $this->comment('Run with -f or --force to perform deletions');
69 $this->showDeletedImages($deleted);
70 $this->comment($deleteCount . ' images deleted');
73 protected function showDeletedImages($paths)
75 if ($this->getOutput()->getVerbosity() <= OutputInterface::VERBOSITY_NORMAL) return;
76 if (count($paths) > 0) {
77 $this->line('Images to delete:');
79 foreach ($paths as $path) {