From: Christopher Ng Date: Wed, 18 May 2022 01:27:33 +0000 (+0000) Subject: Skip instead of throwing on invalid address book migration X-Git-Tag: v29.0.0beta1~136^2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=refs%2Fpull%2F32466%2Fhead;p=nextcloud-server.git Skip instead of throwing on invalid address book migration Signed-off-by: Christopher Ng --- diff --git a/apps/dav/lib/UserMigration/ContactsMigrator.php b/apps/dav/lib/UserMigration/ContactsMigrator.php index 879642b9dc1..38c53a1f76e 100644 --- a/apps/dav/lib/UserMigration/ContactsMigrator.php +++ b/apps/dav/lib/UserMigration/ContactsMigrator.php @@ -160,6 +160,9 @@ class ContactsMigrator implements IMigrator, ISizeEstimationMigrator { ))); } + /** + * @throws InvalidAddressBookException + */ private function getUniqueAddressBookUri(IUser $user, string $initialAddressBookUri): string { $principalUri = $this->getPrincipalUri($user); @@ -168,7 +171,7 @@ class ContactsMigrator implements IMigrator, ISizeEstimationMigrator { : ContactsMigrator::MIGRATED_URI_PREFIX . $initialAddressBookUri; if ($initialAddressBookUri === '') { - throw new ContactsMigratorException('Failed to get unique address book URI'); + throw new InvalidAddressBookException(); } $existingAddressBookUris = array_map( @@ -272,6 +275,8 @@ class ContactsMigrator implements IMigrator, ISizeEstimationMigrator { /** * @param array{displayName: string, description?: string} $metadata * @param VCard[] $vCards + * + * @throws InvalidAddressBookException */ private function importAddressBook(IUser $user, string $filename, string $initialAddressBookUri, array $metadata, array $vCards, OutputInterface $output): void { $principalUri = $this->getPrincipalUri($user); @@ -366,24 +371,29 @@ class ContactsMigrator implements IMigrator, ISizeEstimationMigrator { $splitFilename = explode('.', $addressBookFilename, 2); if (count($splitFilename) !== 2) { - throw new ContactsMigratorException("Invalid filename \"$addressBookFilename\", expected filename of the format \"." . ContactsMigrator::FILENAME_EXT . '"'); + $output->writeln("Invalid filename \"$addressBookFilename\", expected filename of the format \"." . ContactsMigrator::FILENAME_EXT . '", skipping…'); + continue; } [$initialAddressBookUri, $ext] = $splitFilename; /** @var array{displayName: string, description?: string} $metadata */ $metadata = json_decode($importSource->getFileContents($metadataImportPath), true, 512, JSON_THROW_ON_ERROR); - $this->importAddressBook( - $user, - $addressBookFilename, - $initialAddressBookUri, - $metadata, - $vCards, - $output, - ); - - foreach ($vCards as $vCard) { - $vCard->destroy(); + try { + $this->importAddressBook( + $user, + $addressBookFilename, + $initialAddressBookUri, + $metadata, + $vCards, + $output, + ); + } catch (InvalidAddressBookException $e) { + // Allow this exception to skip a failed import + } finally { + foreach ($vCards as $vCard) { + $vCard->destroy(); + } } } }