|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\DAV\Command; |
| 11 | + |
| 12 | +use OCP\Files\AppData\IAppDataFactory; |
| 13 | +use OCP\Files\NotPermittedException; |
| 14 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 15 | +use Symfony\Component\Console\Command\Command; |
| 16 | +use Symfony\Component\Console\Helper\ProgressBar; |
| 17 | +use Symfony\Component\Console\Helper\QuestionHelper; |
| 18 | +use Symfony\Component\Console\Input\InputInterface; |
| 19 | +use Symfony\Component\Console\Output\OutputInterface; |
| 20 | +use Symfony\Component\Console\Question\ConfirmationQuestion; |
| 21 | + |
| 22 | +#[AsCommand( |
| 23 | + name: 'dav:clear-contacts-photo-cache', |
| 24 | + description: 'Clear cached contact photos', |
| 25 | + hidden: false, |
| 26 | +)] |
| 27 | +class ClearContactsPhotoCache extends Command { |
| 28 | + |
| 29 | + public function __construct( |
| 30 | + private IAppDataFactory $appDataFactory, |
| 31 | + ) { |
| 32 | + parent::__construct(); |
| 33 | + } |
| 34 | + |
| 35 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 36 | + $photoCacheAppData = $this->appDataFactory->get('dav-photocache'); |
| 37 | + |
| 38 | + $folders = $photoCacheAppData->getDirectoryListing(); |
| 39 | + $countFolders = count($folders); |
| 40 | + |
| 41 | + if ($countFolders === 0) { |
| 42 | + $output->writeln('No cached contact photos found.'); |
| 43 | + return self::SUCCESS; |
| 44 | + } |
| 45 | + |
| 46 | + $output->writeln('Found ' . count($folders) . ' cached contact photos.'); |
| 47 | + |
| 48 | + /** @var QuestionHelper $helper */ |
| 49 | + $helper = $this->getHelper('question'); |
| 50 | + $question = new ConfirmationQuestion('Please confirm to clear the contacts photo cache [y/n] ', true); |
| 51 | + |
| 52 | + if ($helper->ask($input, $output, $question) === false) { |
| 53 | + $output->writeln('Clearing the contacts photo cache aborted.'); |
| 54 | + return self::SUCCESS; |
| 55 | + } |
| 56 | + |
| 57 | + $progressBar = new ProgressBar($output, $countFolders); |
| 58 | + $progressBar->start(); |
| 59 | + |
| 60 | + foreach ($folders as $folder) { |
| 61 | + try { |
| 62 | + $folder->delete(); |
| 63 | + } catch (NotPermittedException) { |
| 64 | + } |
| 65 | + $progressBar->advance(); |
| 66 | + } |
| 67 | + |
| 68 | + $progressBar->finish(); |
| 69 | + |
| 70 | + $output->writeln(''); |
| 71 | + $output->writeln('Contacts photo cache cleared.'); |
| 72 | + |
| 73 | + return self::SUCCESS; |
| 74 | + } |
| 75 | +} |
0 commit comments