]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/CleanupImages.php
Allow book, shelf, settings & profile form input validation to skip image
[bookstack] / app / Console / Commands / CleanupImages.php
1 <?php
2
3 namespace BookStack\Console\Commands;
4
5 use BookStack\Uploads\ImageService;
6 use Illuminate\Console\Command;
7 use Symfony\Component\Console\Output\OutputInterface;
8
9 class CleanupImages extends Command
10 {
11     /**
12      * The name and signature of the console command.
13      *
14      * @var string
15      */
16     protected $signature = 'bookstack:cleanup-images
17                             {--a|all : Include images that are used in page revisions}
18                             {--f|force : Actually run the deletions}
19                             ';
20
21     /**
22      * The console command description.
23      *
24      * @var string
25      */
26     protected $description = 'Cleanup images and drawings';
27
28
29     protected $imageService;
30
31     /**
32      * Create a new command instance.
33      * @param \BookStack\Uploads\ImageService $imageService
34      */
35     public function __construct(ImageService $imageService)
36     {
37         $this->imageService = $imageService;
38         parent::__construct();
39     }
40
41     /**
42      * Execute the console command.
43      *
44      * @return mixed
45      */
46     public function handle()
47     {
48         $checkRevisions = $this->option('all') ? false : true;
49         $dryRun = $this->option('force') ? false : true;
50
51         if (!$dryRun) {
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?");
53             if (!$proceed) {
54                 return;
55             }
56         }
57
58         $deleted = $this->imageService->deleteUnusedImages($checkRevisions, $dryRun);
59         $deleteCount = count($deleted);
60
61         if ($dryRun) {
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');
66             return;
67         }
68
69         $this->showDeletedImages($deleted);
70         $this->comment($deleteCount . ' images deleted');
71     }
72
73     protected function showDeletedImages($paths)
74     {
75         if ($this->getOutput()->getVerbosity() <= OutputInterface::VERBOSITY_NORMAL) {
76             return;
77         }
78         if (count($paths) > 0) {
79             $this->line('Images to delete:');
80         }
81         foreach ($paths as $path) {
82             $this->line($path);
83         }
84     }
85 }