| 
									
										
										
										
											2018-05-21 01:16:01 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\Console\Commands; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-25 19:30:50 +08:00
										 |  |  | use BookStack\Uploads\ImageService; | 
					
						
							| 
									
										
										
										
											2018-05-21 01:16:01 +08:00
										 |  |  | use Illuminate\Console\Command; | 
					
						
							| 
									
										
										
										
											2018-05-27 21:33:50 +08:00
										 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
					
						
							| 
									
										
										
										
											2018-05-21 01:16:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-24 20:21:46 +08:00
										 |  |  | class CleanupImagesCommand extends Command | 
					
						
							| 
									
										
										
										
											2018-05-21 01:16:01 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * The name and signature of the console command. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @var string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected $signature = 'bookstack:cleanup-images | 
					
						
							| 
									
										
										
										
											2020-12-19 06:54:53 +08:00
										 |  |  |                             {--a|all : Also delete images that are only used in old revisions} | 
					
						
							|  |  |  |                             {--f|force : Actually run the deletions, Defaults to a dry-run} | 
					
						
							| 
									
										
										
										
											2018-05-21 01:16:01 +08:00
										 |  |  |                             '; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * The console command description. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @var string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected $description = 'Cleanup images and drawings'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Execute the console command. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-05-24 19:59:50 +08:00
										 |  |  |     public function handle(ImageService $imageService): int | 
					
						
							| 
									
										
										
										
											2018-05-21 01:16:01 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-05-24 19:59:50 +08:00
										 |  |  |         $checkRevisions = !$this->option('all'); | 
					
						
							|  |  |  |         $dryRun = !$this->option('force'); | 
					
						
							| 
									
										
										
										
											2018-05-21 01:16:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (!$dryRun) { | 
					
						
							| 
									
										
										
										
											2023-05-24 17:34:43 +08:00
										 |  |  |             $this->warn("This operation is destructive and is not guaranteed to be fully accurate.\nEnsure you have a backup of your images.\n"); | 
					
						
							| 
									
										
										
										
											2023-09-14 21:17:20 +08:00
										 |  |  |             $proceed = !$this->input->isInteractive() || $this->confirm("Are you sure you want to proceed?"); | 
					
						
							| 
									
										
										
										
											2018-05-21 01:16:01 +08:00
										 |  |  |             if (!$proceed) { | 
					
						
							| 
									
										
										
										
											2023-05-24 19:59:50 +08:00
										 |  |  |                 return 0; | 
					
						
							| 
									
										
										
										
											2018-05-21 01:16:01 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-24 19:59:50 +08:00
										 |  |  |         $deleted = $imageService->deleteUnusedImages($checkRevisions, $dryRun); | 
					
						
							| 
									
										
										
										
											2018-05-27 21:33:50 +08:00
										 |  |  |         $deleteCount = count($deleted); | 
					
						
							| 
									
										
										
										
											2018-05-21 01:16:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if ($dryRun) { | 
					
						
							| 
									
										
										
										
											2023-05-24 17:34:43 +08:00
										 |  |  |             $this->comment('Dry run, no images have been deleted'); | 
					
						
							| 
									
										
										
										
											2023-09-14 21:17:20 +08:00
										 |  |  |             $this->comment($deleteCount . ' image(s) found that would have been deleted'); | 
					
						
							| 
									
										
										
										
											2018-05-27 21:33:50 +08:00
										 |  |  |             $this->showDeletedImages($deleted); | 
					
						
							| 
									
										
										
										
											2018-05-21 01:16:01 +08:00
										 |  |  |             $this->comment('Run with -f or --force to perform deletions'); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-24 19:59:50 +08:00
										 |  |  |             return 0; | 
					
						
							| 
									
										
										
										
											2018-05-21 01:16:01 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-27 21:33:50 +08:00
										 |  |  |         $this->showDeletedImages($deleted); | 
					
						
							| 
									
										
										
										
											2023-09-14 21:17:20 +08:00
										 |  |  |         $this->comment("{$deleteCount} image(s) deleted"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-24 19:59:50 +08:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2018-05-21 01:16:01 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-05-27 21:33:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-24 19:59:50 +08:00
										 |  |  |     protected function showDeletedImages($paths): void | 
					
						
							| 
									
										
										
										
											2018-05-27 21:33:50 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-09-22 01:48:47 +08:00
										 |  |  |         if ($this->getOutput()->getVerbosity() <= OutputInterface::VERBOSITY_NORMAL) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-05-24 19:59:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-27 21:33:50 +08:00
										 |  |  |         if (count($paths) > 0) { | 
					
						
							| 
									
										
										
										
											2023-09-14 21:17:20 +08:00
										 |  |  |             $this->line('Image(s) to delete:'); | 
					
						
							| 
									
										
										
										
											2018-05-27 21:33:50 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-05-24 19:59:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-27 21:33:50 +08:00
										 |  |  |         foreach ($paths as $path) { | 
					
						
							|  |  |  |             $this->line($path); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-05-21 01:16:01 +08:00
										 |  |  | } |