2020-12-14 21:19:19 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Wallabag\CoreBundle\Command;
|
|
|
|
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
2020-12-16 15:54:17 +00:00
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
2020-12-14 21:19:19 +00:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
|
|
|
use Symfony\Component\Finder\Finder;
|
2022-04-24 15:48:59 +00:00
|
|
|
use Wallabag\CoreBundle\Helper\DownloadImages;
|
2020-12-14 21:19:19 +00:00
|
|
|
|
|
|
|
class CleanDownloadedImagesCommand extends ContainerAwareCommand
|
|
|
|
{
|
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->setName('wallabag:clean-downloaded-images')
|
|
|
|
->setDescription('Cleans downloaded images which are no more associated to an entry')
|
2020-12-16 15:54:17 +00:00
|
|
|
->addOption(
|
|
|
|
'dry-run',
|
|
|
|
null,
|
|
|
|
InputOption::VALUE_NONE,
|
|
|
|
'Do not remove images, just dump counters'
|
2020-12-14 21:19:19 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
2020-12-16 16:45:28 +00:00
|
|
|
$io = new SymfonyStyle($input, $output);
|
2020-12-14 21:19:19 +00:00
|
|
|
|
2020-12-16 15:54:17 +00:00
|
|
|
$dryRun = (bool) $input->getOption('dry-run');
|
2020-12-14 21:19:19 +00:00
|
|
|
|
2020-12-16 15:54:17 +00:00
|
|
|
if ($dryRun) {
|
2020-12-16 16:45:28 +00:00
|
|
|
$io->text('Dry run mode <info>enabled</info> (no images will be removed)');
|
2020-12-16 15:54:17 +00:00
|
|
|
}
|
2020-12-14 21:19:19 +00:00
|
|
|
|
2022-04-24 15:48:59 +00:00
|
|
|
$downloadImages = $this->getContainer()->get(DownloadImages::class);
|
2020-12-16 16:45:28 +00:00
|
|
|
$baseFolder = $downloadImages->getBaseFolder();
|
|
|
|
|
|
|
|
$io->text('Retrieve existing images');
|
2020-12-16 16:26:13 +00:00
|
|
|
|
|
|
|
// retrieve _existing_ folders in the image folder
|
|
|
|
$finder = new Finder();
|
|
|
|
$finder
|
|
|
|
->directories()
|
|
|
|
->ignoreDotFiles(true)
|
|
|
|
->depth(2)
|
2020-12-16 16:45:28 +00:00
|
|
|
->in($baseFolder);
|
2020-12-16 16:26:13 +00:00
|
|
|
|
|
|
|
$existingPaths = [];
|
|
|
|
foreach ($finder as $file) {
|
|
|
|
$existingPaths[] = $file->getFilename();
|
|
|
|
}
|
|
|
|
|
2020-12-16 16:45:28 +00:00
|
|
|
$io->text(sprintf(' -> <info>%d</info> images found', \count($existingPaths)));
|
2020-12-14 21:19:19 +00:00
|
|
|
|
2020-12-16 16:45:28 +00:00
|
|
|
$io->text('Retrieve valid folders attached to a user');
|
2020-12-16 15:54:17 +00:00
|
|
|
|
2020-12-16 16:45:28 +00:00
|
|
|
$entries = $this->getContainer()->get('wallabag_core.entry_repository')->findAllEntriesIdByUserId();
|
2020-12-14 21:19:19 +00:00
|
|
|
|
2020-12-16 16:26:13 +00:00
|
|
|
// retrieve _valid_ folders from existing entries
|
2020-12-14 21:19:19 +00:00
|
|
|
$validPaths = [];
|
|
|
|
foreach ($entries as $entry) {
|
2020-12-16 16:45:28 +00:00
|
|
|
$path = $downloadImages->getRelativePath($entry['id']);
|
2020-12-14 21:19:19 +00:00
|
|
|
|
|
|
|
if (!file_exists($baseFolder . '/' . $path)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// only store the hash, not the full path
|
2020-12-16 16:26:13 +00:00
|
|
|
$validPaths[] = explode('/', $path)[2];
|
2020-12-14 21:19:19 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 16:45:28 +00:00
|
|
|
$io->text(sprintf(' -> <info>%d</info> folders found', \count($validPaths)));
|
|
|
|
|
|
|
|
$deletedCount = 0;
|
|
|
|
|
|
|
|
$io->text('Remove images');
|
|
|
|
|
2020-12-14 21:19:19 +00:00
|
|
|
// check if existing path are valid, if not, remove all images and the folder
|
|
|
|
foreach ($existingPaths as $existingPath) {
|
|
|
|
if (!\in_array($existingPath, $validPaths, true)) {
|
|
|
|
$fullPath = $baseFolder . '/' . $existingPath[0] . '/' . $existingPath[1] . '/' . $existingPath;
|
2020-12-16 15:54:17 +00:00
|
|
|
$files = glob($fullPath . '/*.*');
|
2020-12-14 21:19:19 +00:00
|
|
|
|
2020-12-16 15:54:17 +00:00
|
|
|
if (!$dryRun) {
|
|
|
|
array_map('unlink', $files);
|
|
|
|
rmdir($fullPath);
|
|
|
|
}
|
2020-12-14 21:19:19 +00:00
|
|
|
|
2020-12-16 15:54:17 +00:00
|
|
|
$deletedCount += \count($files);
|
2020-12-14 21:19:19 +00:00
|
|
|
|
2020-12-16 16:45:28 +00:00
|
|
|
$io->text(sprintf('Deleted images in <info>%s</info>: <info>%d</info>', $existingPath, \count($files)));
|
2020-12-14 21:19:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 16:45:28 +00:00
|
|
|
$io->success(sprintf('Finished cleaning. %d deleted images', $deletedCount));
|
2020-12-14 21:19:19 +00:00
|
|
|
|
2020-12-16 16:45:28 +00:00
|
|
|
return 0;
|
2020-12-14 21:19:19 +00:00
|
|
|
}
|
|
|
|
}
|