]> source.dussan.org Git - nextcloud-server.git/commitdiff
Adapt existing migrators to new API
authorCôme Chilliet <come.chilliet@nextcloud.com>
Tue, 12 Apr 2022 14:25:13 +0000 (16:25 +0200)
committerVincent Petry <vincent@nextcloud.com>
Wed, 13 Apr 2022 14:52:40 +0000 (16:52 +0200)
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
apps/dav/lib/UserMigration/CalendarMigrator.php
apps/dav/lib/UserMigration/ContactsMigrator.php
apps/files_trashbin/lib/UserMigration/TrashbinMigrator.php
apps/settings/lib/UserMigration/AccountMigrator.php

index 908b0a564d15d3f6006a649458f43e5c1058c93c..d94e3ec109ee15b4c70b962504b2654cfe1bbea5 100644 (file)
@@ -225,18 +225,20 @@ class CalendarMigrator implements IMigrator {
                        $output->writeln('No calendars to export…');
                }
 
-               /**
-                * @var string $name
-                * @var VCalendar $vCalendar
-                */
-               foreach ($calendarExports as ['name' => $name, 'vCalendar' => $vCalendar]) {
-                       // Set filename to sanitized calendar name appended with the date
-                       $filename = preg_replace('/[^a-zA-Z0-9-_ ]/um', '', $name) . '_' . date('Y-m-d') . CalendarMigrator::FILENAME_EXT;
-                       $exportPath = CalendarMigrator::EXPORT_ROOT . $filename;
-
-                       if ($exportDestination->addFileContents($exportPath, $vCalendar->serialize()) === false) {
-                               throw new CalendarMigratorException('Could not export calendars');
+               try {
+                       /**
+                       * @var string $name
+                       * @var VCalendar $vCalendar
+                       */
+                       foreach ($calendarExports as ['name' => $name, 'vCalendar' => $vCalendar]) {
+                               // Set filename to sanitized calendar name appended with the date
+                               $filename = preg_replace('/[^a-zA-Z0-9-_ ]/um', '', $name) . '_' . date('Y-m-d') . CalendarMigrator::FILENAME_EXT;
+                               $exportPath = CalendarMigrator::EXPORT_ROOT . $filename;
+
+                               $exportDestination->addFileContents($exportPath, $vCalendar->serialize());
                        }
+               } catch (Throwable $e) {
+                       throw new CalendarMigratorException('Could not export calendars', 0, $e);
                }
        }
 
index 99eea2700a5bc803b345fb52608a6927199bcaf5..6a292eff8e84f85a41f295238e1dd8797a77cd65 100644 (file)
@@ -205,26 +205,26 @@ class ContactsMigrator implements IMigrator {
                        $output->writeln('No contacts to export…');
                }
 
-               /**
-                * @var string $name
-                * @var string $displayName
-                * @var ?string $description
-                * @var VCard[] $vCards
-                */
-               foreach ($addressBookExports as ['name' => $name, 'displayName' => $displayName, 'description' => $description, 'vCards' => $vCards]) {
-                       // Set filename to sanitized address book name appended with the date
-                       $basename = preg_replace('/[^a-zA-Z0-9-_ ]/um', '', $name) . '_' . date('Y-m-d');
-                       $exportPath = ContactsMigrator::PATH_ROOT . $basename . '.' . ContactsMigrator::FILENAME_EXT;
-                       $metadataExportPath = ContactsMigrator::PATH_ROOT . $basename . '.' . ContactsMigrator::METADATA_EXT;
-
-                       if ($exportDestination->addFileContents($exportPath, $this->serializeCards($vCards)) === false) {
-                               throw new ContactsMigratorException('Could not export address book');
-                       }
-
-                       $metadata = array_filter(['displayName' => $displayName, 'description' => $description]);
-                       if ($exportDestination->addFileContents($metadataExportPath, json_encode($metadata)) === false) {
-                               throw new ContactsMigratorException('Could not export address book metadata');
+               try {
+                       /**
+                       * @var string $name
+                       * @var string $displayName
+                       * @var ?string $description
+                       * @var VCard[] $vCards
+                       */
+                       foreach ($addressBookExports as ['name' => $name, 'displayName' => $displayName, 'description' => $description, 'vCards' => $vCards]) {
+                               // Set filename to sanitized address book name appended with the date
+                               $basename = preg_replace('/[^a-zA-Z0-9-_ ]/um', '', $name) . '_' . date('Y-m-d');
+                               $exportPath = ContactsMigrator::PATH_ROOT . $basename . '.' . ContactsMigrator::FILENAME_EXT;
+                               $metadataExportPath = ContactsMigrator::PATH_ROOT . $basename . '.' . ContactsMigrator::METADATA_EXT;
+
+                               $exportDestination->addFileContents($exportPath, $this->serializeCards($vCards);
+
+                               $metadata = array_filter(['displayName' => $displayName, 'description' => $description]);
+                               $exportDestination->addFileContents($metadataExportPath, json_encode($metadata);
                        }
+               } catch (Throwable $e) {
+                       throw new CalendarMigratorException('Could not export address book', 0, $e);
                }
        }
 
index 95ed25088b16376dd4ce0055e0aac507b2fb1816..db5231ae01981046e9919c2a31ae2432ca69d63d 100644 (file)
@@ -74,18 +74,16 @@ class TrashbinMigrator implements IMigrator {
                try {
                        $trashbinFolder = $this->root->get('/'.$uid.'/files_trashbin');
                        if (!$trashbinFolder instanceof Folder) {
-                               throw new UserMigrationException('Could not export trashbin, /'.$uid.'/files_trashbin is not a folder');
+                               throw new UserMigrationException('/'.$uid.'/files_trashbin is not a folder');
                        }
                        $output->writeln("Exporting trashbin files…");
-                       if ($exportDestination->copyFolder($trashbinFolder, static::PATH_FILES_FOLDER) === false) {
-                               throw new UserMigrationException("Could not export trashbin.");
-                       }
+                       $exportDestination->copyFolder($trashbinFolder, static::PATH_FILES_FOLDER);
                        $originalLocations = \OCA\Files_Trashbin\Trashbin::getLocations($uid);
-                       if ($exportDestination->addFileContents(static::PATH_LOCATIONS_FILE, json_encode($originalLocations)) === false) {
-                               throw new UserMigrationException("Could not export trashbin.");
-                       }
+                       $exportDestination->addFileContents(static::PATH_LOCATIONS_FILE, json_encode($originalLocations);
                } catch (NotFoundException $e) {
                        $output->writeln("No trashbin to export…");
+               } catch (UserMigrationException $e) {
+                       throw new UserMigrationException("Could not export trashbin: ".$e->getMessage(), 0, $e);
                }
        }
 
index e4218d72a76eed995870968e500916c2c5688588..32cf898f2bc551a7611cde00e31f6ae4d596b5eb 100644 (file)
@@ -74,20 +74,20 @@ class AccountMigrator implements IMigrator {
        public function export(IUser $user, IExportDestination $exportDestination, OutputInterface $output): void {
                $output->writeln('Exporting account information in ' . AccountMigrator::PATH_ACCOUNT_FILE . '…');
 
-               $account = $this->accountManager->getAccount($user);
-               if ($exportDestination->addFileContents(AccountMigrator::PATH_ACCOUNT_FILE, json_encode($account)) === false) {
-                       throw new AccountMigratorException('Could not export account information');
-               }
+               try {
+                       $account = $this->accountManager->getAccount($user);
+                       $exportDestination->addFileContents(AccountMigrator::PATH_ACCOUNT_FILE, json_encode($account));
 
-               $avatar = $this->avatarManager->getAvatar($user->getUID());
-               if ($avatar->isCustomAvatar()) {
-                       $avatarFile = $avatar->getFile(-1);
-                       $exportPath = AccountMigrator::PATH_ROOT . AccountMigrator::AVATAR_BASENAME . '.' . $avatarFile->getExtension();
+                       $avatar = $this->avatarManager->getAvatar($user->getUID());
+                       if ($avatar->isCustomAvatar()) {
+                               $avatarFile = $avatar->getFile(-1);
+                               $exportPath = AccountMigrator::PATH_ROOT . AccountMigrator::AVATAR_BASENAME . '.' . $avatarFile->getExtension();
 
-                       $output->writeln('Exporting avatar to ' . $exportPath . '…');
-                       if ($exportDestination->addFileAsStream($exportPath, $avatarFile->read()) === false) {
-                               throw new AccountMigratorException('Could not export avatar');
+                               $output->writeln('Exporting avatar to ' . $exportPath . '…');
+                               $exportDestination->addFileAsStream($exportPath, $avatarFile->read());
                        }
+               } catch (Throwable $e) {
+                       throw new AccountMigratorException('Could not export account information', 0, $e);
                }
        }