Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions apps/dav/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

use OCA\DAV\Capabilities;
use OCA\DAV\CardDAV\ContactsManager;
use OCA\DAV\CardDAV\PhotoCache;
use OCA\DAV\CardDAV\SyncService;
use OCA\DAV\Events\AddressBookCreatedEvent;
use OCA\DAV\Events\AddressBookDeletedEvent;
Expand Down Expand Up @@ -103,12 +102,6 @@ public function __construct() {

public function register(IRegistrationContext $context): void {
$context->registerServiceAlias('CardDAVSyncService', SyncService::class);
$context->registerService(PhotoCache::class, function (ContainerInterface $c) {
return new PhotoCache(
$c->get(IAppDataFactory::class)->get('dav-photocache'),
$c->get(LoggerInterface::class)
);
});
$context->registerService(AppCalendarPlugin::class, function (ContainerInterface $c) {
return new AppCalendarPlugin(
$c->get(ICalendarManager::class),
Expand Down
17 changes: 13 additions & 4 deletions apps/dav/lib/CardDAV/PhotoCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\DAV\CardDAV;

use OCP\Files\AppData\IAppDataFactory;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
Expand All @@ -19,6 +21,7 @@
use Sabre\VObject\Reader;

class PhotoCache {
private ?IAppData $photoCacheAppData = null;

/** @var array */
public const ALLOWED_CONTENT_TYPES = [
Expand Down Expand Up @@ -142,13 +145,12 @@ private function getFile(ISimpleFolder $folder, $size): ISimpleFile {
private function getFolder(int $addressBookId, string $cardUri, bool $createIfNotExists = true): ISimpleFolder {
$hash = md5($addressBookId . ' ' . $cardUri);
try {
return $this->appData->getFolder($hash);
return $this->getPhotoCacheAppData()->getFolder($hash);
} catch (NotFoundException $e) {
if ($createIfNotExists) {
return $this->appData->newFolder($hash);
} else {
throw $e;
return $this->getPhotoCacheAppData()->newFolder($hash);
}
throw $e;
}
}

Expand Down Expand Up @@ -265,4 +267,11 @@ public function delete($addressBookId, $cardUri) {
// that's OK, nothing to do
}
}

private function getPhotoCacheAppData(): IAppData {
if ($this->photoCacheAppData === null) {
$this->photoCacheAppData = $this->appDataFactory->get('dav-photocache');
}
return $this->photoCacheAppData;
}
}
75 changes: 75 additions & 0 deletions apps/dav/lib/Command/ClearContactsPhotoCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\DAV\Command;

use OCP\Files\AppData\IAppDataFactory;
use OCP\Files\NotPermittedException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;

#[AsCommand(
name: 'dav:clear-contacts-photo-cache',
description: 'Clear cached contact photos',
hidden: false,
)]
class ClearContactsPhotoCache extends Command {

public function __construct(
private IAppDataFactory $appDataFactory,
) {
parent::__construct();
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$photoCacheAppData = $this->appDataFactory->get('dav-photocache');

$folders = $photoCacheAppData->getDirectoryListing();
$countFolders = count($folders);

if ($countFolders === 0) {
$output->writeln('No cached contact photos found.');
return self::SUCCESS;
}

$output->writeln('Found ' . count($folders) . ' cached contact photos.');

/** @var QuestionHelper $helper */
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion('Please confirm to clear the contacts photo cache [y/n] ', true);

if ($helper->ask($input, $output, $question) === false) {
$output->writeln('Clearing the contacts photo cache aborted.');
return self::SUCCESS;
}

$progressBar = new ProgressBar($output, $countFolders);
$progressBar->start();

foreach ($folders as $folder) {
try {
$folder->delete();
} catch (NotPermittedException) {
}
$progressBar->advance();
}

$progressBar->finish();

$output->writeln('');
$output->writeln('Contacts photo cache cleared.');

return self::SUCCESS;
}
}
2 changes: 1 addition & 1 deletion lib/private/Repair.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public static function getRepairSteps(): array {
\OCP\Server::get(ClearGeneratedAvatarCache::class),
new AddPreviewBackgroundCleanupJob(\OC::$server->getJobList()),
new AddCleanupUpdaterBackupsJob(\OC::$server->getJobList()),
new CleanupCardDAVPhotoCache(\OC::$server->getConfig(), \OC::$server->getAppDataDir('dav-photocache'), \OC::$server->get(LoggerInterface::class)),
new CleanupCardDAVPhotoCache(\OC::$server->getConfig(), \OCP\Server::get(IAppDataFactory::class), \OC::$server->get(LoggerInterface::class)),
new AddClenupLoginFlowV2BackgroundJob(\OC::$server->getJobList()),
new RemoveLinkShares(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig(), \OC::$server->getGroupManager(), \OC::$server->get(INotificationManager::class), \OCP\Server::get(ITimeFactory::class)),
new ClearCollectionsAccessCache(\OC::$server->getConfig(), \OCP\Server::get(IManager::class)),
Expand Down
24 changes: 10 additions & 14 deletions lib/private/Repair/NC16/CleanupCardDAVPhotoCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OC\Repair\NC16;

use OCP\Files\IAppData;
use OCP\Files\AppData\IAppDataFactory;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\IConfig;
Expand All @@ -27,27 +28,22 @@
* photo could be returned for this vcard. These invalid files are removed by this migration step.
*/
class CleanupCardDAVPhotoCache implements IRepairStep {
/** @var IConfig */
private $config;

/** @var IAppData */
private $appData;

private LoggerInterface $logger;

public function __construct(IConfig $config, IAppData $appData, LoggerInterface $logger) {
$this->config = $config;
$this->appData = $appData;
$this->logger = $logger;
public function __construct(
private IConfig $config,
private IAppDataFactory $appDataFactory,
private LoggerInterface $logger,
) {
}

public function getName(): string {
return 'Cleanup invalid photocache files for carddav';
}

private function repair(IOutput $output): void {
$photoCacheAppData = $this->appDataFactory->get('dav-photocache');

try {
$folders = $this->appData->getDirectoryListing();
$folders = $photoCacheAppData->getDirectoryListing();
} catch (NotFoundException $e) {
return;
} catch (RuntimeException $e) {
Expand Down