Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/ghostbust dead symlinks #579

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions app/Console/Commands/Ghostbuster.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -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 = [];
Expand Down Expand Up @@ -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);
Expand All @@ -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('');
Expand All @@ -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.');
Expand Down