Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip instead of throwing on invalid DAV data migration #32466

Merged
merged 2 commits into from
Feb 23, 2024
Merged
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
14 changes: 10 additions & 4 deletions apps/dav/lib/UserMigration/CalendarMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ function (ICalendar $calendar) use ($user, $output) {
)));
}

/**
* @throws InvalidCalendarException
*/
private function getUniqueCalendarUri(IUser $user, string $initialCalendarUri): string {
$principalUri = $this->getPrincipalUri($user);

Expand All @@ -190,7 +193,7 @@ private function getUniqueCalendarUri(IUser $user, string $initialCalendarUri):
: CalendarMigrator::MIGRATED_URI_PREFIX . $initialCalendarUri;

if ($initialCalendarUri === '') {
throw new CalendarMigratorException('Failed to get unique calendar URI');
throw new InvalidCalendarException();
}

$existingCalendarUris = array_map(
Expand Down Expand Up @@ -457,17 +460,20 @@ public function import(IUser $user, IImportSource $importSource, OutputInterface
VObjectReader::OPTION_FORGIVING,
);
} catch (Throwable $e) {
throw new CalendarMigratorException("Failed to read file \"$importPath\"", 0, $e);
$output->writeln("Failed to read file \"$importPath\", skipping…");
continue;
}

$problems = $vCalendar->validate();
if (!empty($problems)) {
throw new CalendarMigratorException("Invalid calendar data contained in \"$importPath\"");
$output->writeln("Invalid calendar data contained in \"$importPath\", skipping…");
continue;
}

$splitFilename = explode('.', $filename, 2);
if (count($splitFilename) !== 2) {
throw new CalendarMigratorException("Invalid filename \"$filename\", expected filename of the format \"<calendar_name>" . CalendarMigrator::FILENAME_EXT . '"');
$output->writeln("Invalid filename \"$filename\", expected filename of the format \"<calendar_name>" . CalendarMigrator::FILENAME_EXT . '", skipping…');
continue;
}
[$initialCalendarUri, $ext] = $splitFilename;

Expand Down
36 changes: 23 additions & 13 deletions apps/dav/lib/UserMigration/ContactsMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@
)));
}

/**
* @throws InvalidAddressBookException
*/
private function getUniqueAddressBookUri(IUser $user, string $initialAddressBookUri): string {
$principalUri = $this->getPrincipalUri($user);

Expand All @@ -168,7 +171,7 @@
: ContactsMigrator::MIGRATED_URI_PREFIX . $initialAddressBookUri;

if ($initialAddressBookUri === '') {
throw new ContactsMigratorException('Failed to get unique address book URI');
throw new InvalidAddressBookException();
}

$existingAddressBookUris = array_map(
Expand Down Expand Up @@ -272,6 +275,8 @@
/**
* @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);
Expand Down Expand Up @@ -366,24 +371,29 @@

$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,
Dismissed Show dismissed Hide dismissed
$output,
);
} catch (InvalidAddressBookException $e) {
// Allow this exception to skip a failed import
} finally {
foreach ($vCards as $vCard) {
$vCard->destroy();
}
}
}
}
Expand Down
Loading