diff --git a/src/Wallabag/CoreBundle/Command/CleanDownloadedImagesCommand.php b/src/Wallabag/CoreBundle/Command/CleanDownloadedImagesCommand.php index de5cfc509..d81becdc3 100644 --- a/src/Wallabag/CoreBundle/Command/CleanDownloadedImagesCommand.php +++ b/src/Wallabag/CoreBundle/Command/CleanDownloadedImagesCommand.php @@ -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 %d user accounts', \count($users))); - if ($dryRun) { - $this->io->text('Dry run mode enabled (no images will be removed)'); + $io->text('Dry run mode enabled (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(' -> %d 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 %s', $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(' -> %d 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 %s: %d', $existingPath, \count($files))); + $io->text(sprintf('Deleted images in %s: %d', $existingPath, \count($files))); } } - $this->deleted += $deletedCount; + $io->success(sprintf('Finished cleaning. %d deleted images', $deletedCount)); - $this->io->text(sprintf('Deleted %d images for user %s', $deletedCount, $user->getUserName())); + return 0; } }