2018-05-20 13:16:01 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Console\Commands;
|
|
|
|
|
2018-09-25 07:30:50 -04:00
|
|
|
use BookStack\Uploads\ImageService;
|
2018-05-20 13:16:01 -04:00
|
|
|
use Illuminate\Console\Command;
|
2018-05-27 09:33:50 -04:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2018-05-20 13:16:01 -04:00
|
|
|
|
|
|
|
class CleanupImages extends Command
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $signature = 'bookstack:cleanup-images
|
2020-12-18 17:54:53 -05: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-20 13:16:01 -04:00
|
|
|
';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Cleanup images and drawings';
|
|
|
|
|
|
|
|
protected $imageService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
2018-09-25 07:30:50 -04:00
|
|
|
* @param \BookStack\Uploads\ImageService $imageService
|
2018-05-20 13:16:01 -04:00
|
|
|
*/
|
|
|
|
public function __construct(ImageService $imageService)
|
|
|
|
{
|
|
|
|
$this->imageService = $imageService;
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$checkRevisions = $this->option('all') ? false : true;
|
|
|
|
$dryRun = $this->option('force') ? false : true;
|
|
|
|
|
|
|
|
if (!$dryRun) {
|
2023-05-24 05:34:43 -04:00
|
|
|
$this->warn("This operation is destructive and is not guaranteed to be fully accurate.\nEnsure you have a backup of your images.\n");
|
|
|
|
$proceed = $this->confirm("Are you sure you want to proceed?");
|
2018-05-20 13:16:01 -04:00
|
|
|
if (!$proceed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 14:40:07 -04:00
|
|
|
$deleted = $this->imageService->deleteUnusedImages($checkRevisions, $dryRun);
|
2018-05-27 09:33:50 -04:00
|
|
|
$deleteCount = count($deleted);
|
2018-05-20 13:16:01 -04:00
|
|
|
|
|
|
|
if ($dryRun) {
|
2023-05-24 05:34:43 -04:00
|
|
|
$this->comment('Dry run, no images have been deleted');
|
2018-05-20 13:16:01 -04:00
|
|
|
$this->comment($deleteCount . ' images found that would have been deleted');
|
2018-05-27 09:33:50 -04:00
|
|
|
$this->showDeletedImages($deleted);
|
2018-05-20 13:16:01 -04:00
|
|
|
$this->comment('Run with -f or --force to perform deletions');
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2018-05-20 13:16:01 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-27 09:33:50 -04:00
|
|
|
$this->showDeletedImages($deleted);
|
2018-05-20 13:16:01 -04:00
|
|
|
$this->comment($deleteCount . ' images deleted');
|
|
|
|
}
|
2018-05-27 09:33:50 -04:00
|
|
|
|
|
|
|
protected function showDeletedImages($paths)
|
|
|
|
{
|
2018-09-21 13:48:47 -04:00
|
|
|
if ($this->getOutput()->getVerbosity() <= OutputInterface::VERBOSITY_NORMAL) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-27 09:33:50 -04:00
|
|
|
if (count($paths) > 0) {
|
|
|
|
$this->line('Images to delete:');
|
|
|
|
}
|
|
|
|
foreach ($paths as $path) {
|
|
|
|
$this->line($path);
|
|
|
|
}
|
|
|
|
}
|
2018-05-20 13:16:01 -04:00
|
|
|
}
|