]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/CleanupImages.php
Added command to clean-up old images, Unfinished
[bookstack] / app / Console / Commands / CleanupImages.php
1 <?php
2
3 namespace BookStack\Console\Commands;
4
5 use BookStack\Services\ImageService;
6 use Illuminate\Console\Command;
7
8 class CleanupImages extends Command
9 {
10     /**
11      * The name and signature of the console command.
12      *
13      * @var string
14      */
15     protected $signature = 'bookstack:cleanup-images
16                             {--a|all : Include images that are used in page revisions}
17                             {--f|force : Actually run the deletions}
18                             ';
19
20     /**
21      * The console command description.
22      *
23      * @var string
24      */
25     protected $description = 'Cleanup images and drawings';
26
27
28     protected $imageService;
29
30     /**
31      * Create a new command instance.
32      * @param ImageService $imageService
33      */
34     public function __construct(ImageService $imageService)
35     {
36         $this->imageService = $imageService;
37         parent::__construct();
38     }
39
40     /**
41      * Execute the console command.
42      *
43      * @return mixed
44      */
45     public function handle()
46     {
47         $checkRevisions = $this->option('all') ? false : true;
48         $dryRun = $this->option('force') ? false : true;
49
50         if (!$dryRun) {
51             $proceed = $this->confirm('This operation is destructive and is not guaranteed to be fully accurate. Ensure you have a backup of your images. Are you sure you want to proceed?');
52             if (!$proceed) {
53                 return;
54             }
55         }
56
57         $deleteCount = $this->imageService->deleteUnusedImages($checkRevisions, ['gallery', 'drawio'], $dryRun);
58
59         if ($dryRun) {
60             $this->comment('Dry run, No images have been deleted');
61             $this->comment($deleteCount . ' images found that would have been deleted');
62             $this->comment('Run with -f or --force to perform deletions');
63             return;
64         }
65
66         $this->comment($deleteCount . ' images deleted');
67     }
68 }