3 namespace BookStack\Console\Commands;
5 use BookStack\Uploads\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 : 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';
28 protected $imageService;
31 * Create a new command instance.
33 * @param \BookStack\Uploads\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 $this->warn("This operation is destructive and is not guaranteed to be fully accurate.\nEnsure you have a backup of your images.\n");
53 $proceed = $this->confirm("Are you sure you want to proceed?");
59 $deleted = $this->imageService->deleteUnusedImages($checkRevisions, $dryRun);
60 $deleteCount = count($deleted);
63 $this->comment('Dry run, no images have been deleted');
64 $this->comment($deleteCount . ' images found that would have been deleted');
65 $this->showDeletedImages($deleted);
66 $this->comment('Run with -f or --force to perform deletions');
71 $this->showDeletedImages($deleted);
72 $this->comment($deleteCount . ' images deleted');
75 protected function showDeletedImages($paths)
77 if ($this->getOutput()->getVerbosity() <= OutputInterface::VERBOSITY_NORMAL) {
80 if (count($paths) > 0) {
81 $this->line('Images to delete:');
83 foreach ($paths as $path) {