Compare full list to full list

This commit is contained in:
Jeremy Benoist 2020-12-16 17:45:28 +01:00
parent 5437f0e3da
commit fb4d1fd10b
No known key found for this signature in database
GPG key ID: BCA73962457ACC3C

View file

@ -8,17 +8,9 @@ use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\Finder;
use Wallabag\CoreBundle\Helper\DownloadImages;
use Wallabag\UserBundle\Entity\User;
class CleanDownloadedImagesCommand extends ContainerAwareCommand class CleanDownloadedImagesCommand extends ContainerAwareCommand
{ {
/** @var SymfonyStyle */
protected $io;
protected $deleted = 0;
/** @var DownloadImages */
protected $downloadImages;
protected function configure() protected function configure()
{ {
$this $this
@ -34,19 +26,18 @@ class CleanDownloadedImagesCommand extends ContainerAwareCommand
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
$this->io = new SymfonyStyle($input, $output); $io = new SymfonyStyle($input, $output);
$dryRun = (bool) $input->getOption('dry-run'); $dryRun = (bool) $input->getOption('dry-run');
$users = $this->getContainer()->get('wallabag_user.user_repository')->findAll();
$this->io->text(sprintf('Cleaning through <info>%d</info> user accounts', \count($users)));
if ($dryRun) { if ($dryRun) {
$this->io->text('Dry run mode <info>enabled</info> (no images will be removed)'); $io->text('Dry run mode <info>enabled</info> (no images will be removed)');
} }
$this->downloadImages = $this->getContainer()->get('wallabag_core.entry.download_images'); $downloadImages = $this->getContainer()->get('wallabag_core.entry.download_images');
$baseFolder = $downloadImages->getBaseFolder();
$io->text('Retrieve existing images');
// retrieve _existing_ folders in the image folder // retrieve _existing_ folders in the image folder
$finder = new Finder(); $finder = new Finder();
@ -54,36 +45,23 @@ class CleanDownloadedImagesCommand extends ContainerAwareCommand
->directories() ->directories()
->ignoreDotFiles(true) ->ignoreDotFiles(true)
->depth(2) ->depth(2)
->in($this->downloadImages->getBaseFolder()); ->in($baseFolder);
$existingPaths = []; $existingPaths = [];
foreach ($finder as $file) { foreach ($finder as $file) {
$existingPaths[] = $file->getFilename(); $existingPaths[] = $file->getFilename();
} }
foreach ($users as $user) { $io->text(sprintf(' -> <info>%d</info> images found', \count($existingPaths)));
$this->clean($user, $existingPaths, $dryRun);
}
$this->io->success(sprintf('Finished cleaning. %d deleted images', $this->deleted)); $io->text('Retrieve valid folders attached to a user');
return 0; $entries = $this->getContainer()->get('wallabag_core.entry_repository')->findAllEntriesIdByUserId();
}
private function clean(User $user, array $existingPaths, bool $dryRun)
{
$this->io->text(sprintf('Processing user <info>%s</info>', $user->getUsername()));
$repo = $this->getContainer()->get('wallabag_core.entry_repository');
$baseFolder = $this->downloadImages->getBaseFolder();
$entries = $repo->findAllEntriesIdByUserId($user->getId());
$deletedCount = 0;
// retrieve _valid_ folders from existing entries // retrieve _valid_ folders from existing entries
$validPaths = []; $validPaths = [];
foreach ($entries as $entry) { foreach ($entries as $entry) {
$path = $this->downloadImages->getRelativePath($entry['id']); $path = $downloadImages->getRelativePath($entry['id']);
if (!file_exists($baseFolder . '/' . $path)) { if (!file_exists($baseFolder . '/' . $path)) {
continue; continue;
@ -93,6 +71,12 @@ class CleanDownloadedImagesCommand extends ContainerAwareCommand
$validPaths[] = explode('/', $path)[2]; $validPaths[] = explode('/', $path)[2];
} }
$io->text(sprintf(' -> <info>%d</info> folders found', \count($validPaths)));
$deletedCount = 0;
$io->text('Remove images');
// check if existing path are valid, if not, remove all images and the folder // check if existing path are valid, if not, remove all images and the folder
foreach ($existingPaths as $existingPath) { foreach ($existingPaths as $existingPath) {
if (!\in_array($existingPath, $validPaths, true)) { if (!\in_array($existingPath, $validPaths, true)) {
@ -106,12 +90,12 @@ class CleanDownloadedImagesCommand extends ContainerAwareCommand
$deletedCount += \count($files); $deletedCount += \count($files);
$this->io->text(sprintf('Deleted images in <info>%s</info>: <info>%d</info>', $existingPath, \count($files))); $io->text(sprintf('Deleted images in <info>%s</info>: <info>%d</info>', $existingPath, \count($files)));
} }
} }
$this->deleted += $deletedCount; $io->success(sprintf('Finished cleaning. %d deleted images', $deletedCount));
$this->io->text(sprintf('Deleted <info>%d</info> images for user <info>%s</info>', $deletedCount, $user->getUserName())); return 0;
} }
} }