]> source.dussan.org Git - nextcloud-server.git/commitdiff
Skip instead of throwing on invalid address book migration 32466/head
authorChristopher Ng <chrng8@gmail.com>
Wed, 18 May 2022 01:27:33 +0000 (01:27 +0000)
committerskjnldsv <skjnldsv@protonmail.com>
Fri, 23 Feb 2024 14:24:46 +0000 (15:24 +0100)
Signed-off-by: Christopher Ng <chrng8@gmail.com>
apps/dav/lib/UserMigration/ContactsMigrator.php

index 879642b9dc10de4629ac9094a2505df831ebae5a..38c53a1f76e270800f817748800e4dde44e5c25d 100644 (file)
@@ -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 \"<address_book_name>." . ContactsMigrator::FILENAME_EXT . '"');
+                               $output->writeln("Invalid filename \"$addressBookFilename\", expected filename of the format \"<address_book_name>." . 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();
+                               }
                        }
                }
        }