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\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;
use Wallabag\CoreBundle\Helper\DownloadImages;
use Wallabag\UserBundle\Entity\User;
class CleanDownloadedImagesCommand extends ContainerAwareCommand
{
/** @var SymfonyStyle */
protected $io;
protected $deleted = 0;
/** @var DownloadImages */
protected $downloadImages;
protected function configure()
{
$this
@ -34,19 +26,18 @@ class CleanDownloadedImagesCommand extends ContainerAwareCommand
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->io = new SymfonyStyle($input, $output);
$io = new SymfonyStyle($input, $output);
$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) {
$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
$finder = new Finder();
@ -54,36 +45,23 @@ class CleanDownloadedImagesCommand extends ContainerAwareCommand
->directories()
->ignoreDotFiles(true)
->depth(2)
->in($this->downloadImages->getBaseFolder());
->in($baseFolder);
$existingPaths = [];
foreach ($finder as $file) {
$existingPaths[] = $file->getFilename();
}
foreach ($users as $user) {
$this->clean($user, $existingPaths, $dryRun);
}
$io->text(sprintf(' -> <info>%d</info> images found', \count($existingPaths)));
$this->io->success(sprintf('Finished cleaning. %d deleted images', $this->deleted));
$io->text('Retrieve valid folders attached to a user');
return 0;
}
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;
$entries = $this->getContainer()->get('wallabag_core.entry_repository')->findAllEntriesIdByUserId();
// retrieve _valid_ folders from existing entries
$validPaths = [];
foreach ($entries as $entry) {
$path = $this->downloadImages->getRelativePath($entry['id']);
$path = $downloadImages->getRelativePath($entry['id']);
if (!file_exists($baseFolder . '/' . $path)) {
continue;
@ -93,6 +71,12 @@ class CleanDownloadedImagesCommand extends ContainerAwareCommand
$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
foreach ($existingPaths as $existingPath) {
if (!\in_array($existingPath, $validPaths, true)) {
@ -106,12 +90,12 @@ class CleanDownloadedImagesCommand extends ContainerAwareCommand
$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;
}
}