Skip to content

Commit

Permalink
feat: add files:cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardBaumrock committed Jan 18, 2024
1 parent 6e34d96 commit a242249
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions App/Commands/FilesCleanup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace RockShell;

use DirectoryIterator;
use Symfony\Component\Console\Input\InputOption;

class FilesCleanup extends Command
{

public function config()
{
$this
->setDescription("Cleanup orphaned directories")
->addOption("delete", "d", InputOption::VALUE_NONE, "Delete without confirmation")
->addOption("show", "s", InputOption::VALUE_NONE, "Show folders with matching page");
}

public function handle()
{
$path = $this->wire()->config->paths->files;
$dir = new DirectoryIterator($path);

$this->write($this->option("show"));

$delete =
$this->option("delete")
?: $this->confirm("Delete folders with no matching page? If you select <no>, folders will only be listed.");
$showExisting =
$this->option("show")
?: $this->confirm("Show folders with matching page?");

foreach ($dir as $d) {
if ($d->isDot()) continue;
if (!$d->isDir()) continue;
$id = $d->getFilename();
$folder = $d->getPath() . "/$id";
$files = $this->wire()->files->find($folder, [
'returnRelative' => true,
]);
$page = $this->wire()->pages->get($id);
if ($page->id) {
if (!$showExisting) continue;
$this->success($folder);
$this->write($files);
} else {
$this->error($folder);
if (count($files)) {
$this->write(" " . implode("\n ", $files));
}
if ($delete) {
$this->wire()->files->rmdir($folder, true);
$this->write("-- deleted --");
}
$this->write("");
}
}

return self::SUCCESS;
}
}

0 comments on commit a242249

Please sign in to comment.