Build images list outside of clean

This commit is contained in:
Jeremy Benoist 2020-12-16 17:26:13 +01:00
parent 478c20d3a4
commit 5437f0e3da
No known key found for this signature in database
GPG Key ID: BCA73962457ACC3C

View File

@ -8,14 +8,16 @@ 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; use Wallabag\UserBundle\Entity\User;
class CleanDownloadedImagesCommand extends ContainerAwareCommand class CleanDownloadedImagesCommand extends ContainerAwareCommand
{ {
/** @var SymfonyStyle */ /** @var SymfonyStyle */
protected $io; protected $io;
protected $deleted = 0; protected $deleted = 0;
/** @var DownloadImages */
protected $downloadImages;
protected function configure() protected function configure()
{ {
@ -44,8 +46,23 @@ class CleanDownloadedImagesCommand extends ContainerAwareCommand
$this->io->text('Dry run mode <info>enabled</info> (no images will be removed)'); $this->io->text('Dry run mode <info>enabled</info> (no images will be removed)');
} }
$this->downloadImages = $this->getContainer()->get('wallabag_core.entry.download_images');
// retrieve _existing_ folders in the image folder
$finder = new Finder();
$finder
->directories()
->ignoreDotFiles(true)
->depth(2)
->in($this->downloadImages->getBaseFolder());
$existingPaths = [];
foreach ($finder as $file) {
$existingPaths[] = $file->getFilename();
}
foreach ($users as $user) { foreach ($users as $user) {
$this->clean($user, $dryRun); $this->clean($user, $existingPaths, $dryRun);
} }
$this->io->success(sprintf('Finished cleaning. %d deleted images', $this->deleted)); $this->io->success(sprintf('Finished cleaning. %d deleted images', $this->deleted));
@ -53,45 +70,27 @@ class CleanDownloadedImagesCommand extends ContainerAwareCommand
return 0; return 0;
} }
private function clean(User $user, bool $dryRun) private function clean(User $user, array $existingPaths, bool $dryRun)
{ {
$this->io->text(sprintf('Processing user <info>%s</info>', $user->getUsername())); $this->io->text(sprintf('Processing user <info>%s</info>', $user->getUsername()));
$repo = $this->getContainer()->get('wallabag_core.entry_repository'); $repo = $this->getContainer()->get('wallabag_core.entry_repository');
$downloadImages = $this->getContainer()->get('wallabag_core.entry.download_images'); $baseFolder = $this->downloadImages->getBaseFolder();
$baseFolder = $downloadImages->getBaseFolder();
$entries = $repo->findAllEntriesIdByUserId($user->getId()); $entries = $repo->findAllEntriesIdByUserId($user->getId());
$deletedCount = 0; $deletedCount = 0;
// first retrieve _valid_ folders from existing entries // retrieve _valid_ folders from existing entries
$hashToId = [];
$validPaths = []; $validPaths = [];
foreach ($entries as $entry) { foreach ($entries as $entry) {
$path = $downloadImages->getRelativePath($entry['id']); $path = $this->downloadImages->getRelativePath($entry['id']);
if (!file_exists($baseFolder . '/' . $path)) { if (!file_exists($baseFolder . '/' . $path)) {
continue; continue;
} }
// only store the hash, not the full path // only store the hash, not the full path
$hash = explode('/', $path)[2]; $validPaths[] = explode('/', $path)[2];
$validPaths[] = $hash;
$hashToId[$hash] = $entry['id'];
}
// then retrieve _existing_ folders in the image folder
$finder = new Finder();
$finder
->directories()
->ignoreDotFiles(true)
->depth(2)
->in($baseFolder);
$existingPaths = [];
foreach ($finder as $file) {
$existingPaths[] = $file->getFilename();
} }
// 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