diff --git a/app/Console/Commands/Ghostbuster.php b/app/Console/Commands/Ghostbuster.php index f934d80737..b0255049bf 100644 --- a/app/Console/Commands/Ghostbuster.php +++ b/app/Console/Commands/Ghostbuster.php @@ -27,7 +27,7 @@ class Ghostbuster extends Command * * @var string */ - protected $signature = 'lychee:ghostbuster {dryrun=1 : Dry Run default is True}'; + protected $signature = 'lychee:ghostbuster {removeDeadSymLinks=0 : Remove Photos with dead symlinks} {dryrun=1 : Dry Run default is True}'; /** * The console command description. @@ -59,7 +59,17 @@ public function __construct(PhotoFunctions $photoFunctions, Colorize $colorize) public function handle() { $this->line(''); + $removeDeadSymLinks = (bool) $this->argument('removeDeadSymLinks'); $dryrun = (bool) $this->argument('dryrun'); + if ($removeDeadSymLinks) { + $this->line('Also parsing database for pictures where the url does not point to an existing file.'); + $this->line($this->col->yellow('This may modify the database.')); + $this->line(''); + } + if (!$dryrun) { + $this->line($this->col->red("This is not a drill! Let's delete those files!")); + $this->line(''); + } $path = Storage::path('big'); $files = array_slice(scandir($path), 2); @@ -69,8 +79,11 @@ public function handle() if ($url == 'index.html') { continue; } - $c = Photo::where('url', '=', $url)->count(); - if ($c == 0) { + + $isDeadSymlink = is_link($path . '/' . $url) && !file_exists(readlink($path . '/' . $url)); + $photos = Photo::where('url', '=', $url)->get(); + + if (count($photos) === 0 || ($isDeadSymlink && $removeDeadSymLinks)) { $photoName = explode('.', $url); $to_delete = []; @@ -102,7 +115,7 @@ public function handle() if ($delete > 0) { $total++; if ($dryrun) { - $this->line(str_pad($del, 50) . $this->col->red(' will be removed') . '.'); + $this->line(str_pad($del, 50) . $this->col->red(' file will be removed') . '.'); } else { if ($delete == 1) { Storage::delete($del); @@ -114,6 +127,22 @@ public function handle() } } } + + if ($isDeadSymlink && $removeDeadSymLinks) { + foreach ($photos as $photo) { + if ($dryrun) { + $this->line(str_pad($photo->url, 50) . $this->col->red(' photo will be removed') . '.'); + } else { + // Laravel apparently doesn't think dead symlinks 'exist', so manually remove the original here. + unlink($path . '/' . $url); + + $photo->predelete(); + $photo->delete(); + + $this->line($this->col->red('removed photo: ') . $photo->url); + } + } + } } } $this->line(''); @@ -124,7 +153,7 @@ public function handle() if ($total > 0 && $dryrun) { $this->line($total . ' pictures will be deleted.'); $this->line(''); - $this->line("Rerun the command '" . $this->col->yellow('php artisan lychee:ghostbuster 0') . "' to effectively remove the files."); + $this->line("Rerun the command '" . $this->col->yellow('php artisan lychee:ghostbuster ' . ($removeDeadSymLinks ? '1' : '0') . ' 0') . "' to effectively remove the files."); } if ($total > 0 && !$dryrun) { $this->line($total . ' pictures have been deleted.');