aboutsummaryrefslogtreecommitdiffstats
path: root/apps/settings/lib/UserMigration/AccountMigrator.php
diff options
context:
space:
mode:
Diffstat (limited to 'apps/settings/lib/UserMigration/AccountMigrator.php')
-rw-r--r--apps/settings/lib/UserMigration/AccountMigrator.php89
1 files changed, 57 insertions, 32 deletions
diff --git a/apps/settings/lib/UserMigration/AccountMigrator.php b/apps/settings/lib/UserMigration/AccountMigrator.php
index 7b60a101cee..1c51aec5104 100644
--- a/apps/settings/lib/UserMigration/AccountMigrator.php
+++ b/apps/settings/lib/UserMigration/AccountMigrator.php
@@ -3,54 +3,37 @@
declare(strict_types=1);
/**
- * @copyright 2022 Christopher Ng <chrng8@gmail.com>
- *
- * @author Christopher Ng <chrng8@gmail.com>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Settings\UserMigration;
use InvalidArgumentException;
use OC\Accounts\TAccountsHelper;
+use OC\Core\Db\ProfileConfigMapper;
use OC\NotSquareException;
+use OC\Profile\ProfileManager;
use OCA\Settings\AppInfo\Application;
use OCP\Accounts\IAccountManager;
use OCP\IAvatarManager;
use OCP\IL10N;
+use OCP\Image;
use OCP\IUser;
use OCP\UserMigration\IExportDestination;
use OCP\UserMigration\IImportSource;
use OCP\UserMigration\IMigrator;
+use OCP\UserMigration\ISizeEstimationMigrator;
use OCP\UserMigration\TMigratorBasicVersionHandling;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;
-class AccountMigrator implements IMigrator {
+class AccountMigrator implements IMigrator, ISizeEstimationMigrator {
use TMigratorBasicVersionHandling;
use TAccountsHelper;
- private IAccountManager $accountManager;
-
- private IAvatarManager $avatarManager;
-
- private IL10N $l10n;
+ private ProfileManager $profileManager;
private const PATH_ROOT = Application::APP_ID . '/';
@@ -58,14 +41,35 @@ class AccountMigrator implements IMigrator {
private const AVATAR_BASENAME = 'avatar';
+ private const PATH_CONFIG_FILE = AccountMigrator::PATH_ROOT . 'config.json';
+
public function __construct(
- IAccountManager $accountManager,
- IAvatarManager $avatarManager,
- IL10N $l10n
+ private IAccountManager $accountManager,
+ private IAvatarManager $avatarManager,
+ ProfileManager $profileManager,
+ private ProfileConfigMapper $configMapper,
+ private IL10N $l10n,
) {
- $this->accountManager = $accountManager;
- $this->avatarManager = $avatarManager;
- $this->l10n = $l10n;
+ $this->profileManager = $profileManager;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function getEstimatedExportSize(IUser $user): int|float {
+ $size = 100; // 100KiB for account JSON
+
+ try {
+ $avatar = $this->avatarManager->getAvatar($user->getUID());
+ if ($avatar->isCustomAvatar()) {
+ $avatarFile = $avatar->getFile(-1);
+ $size += $avatarFile->getSize() / 1024;
+ }
+ } catch (Throwable $e) {
+ // Skip avatar in size estimate on failure
+ }
+
+ return ceil($size);
}
/**
@@ -93,6 +97,14 @@ class AccountMigrator implements IMigrator {
} catch (Throwable $e) {
throw new AccountMigratorException('Could not export avatar', 0, $e);
}
+
+ try {
+ $output->writeln('Exporting profile config in ' . AccountMigrator::PATH_CONFIG_FILE . '…');
+ $config = $this->profileManager->getProfileConfig($user, $user);
+ $exportDestination->addFileContents(AccountMigrator::PATH_CONFIG_FILE, json_encode($config));
+ } catch (Throwable $e) {
+ throw new AccountMigratorException('Could not export profile config', 0, $e);
+ }
}
/**
@@ -133,7 +145,7 @@ class AccountMigrator implements IMigrator {
$output->writeln('Importing avatar from ' . $importPath . '…');
$stream = $importSource->getFileAsStream($importPath);
- $image = new \OC_Image();
+ $image = new Image();
$image->loadFromFileHandle($stream);
try {
@@ -145,6 +157,19 @@ class AccountMigrator implements IMigrator {
throw new AccountMigratorException('Failed to import avatar', 0, $e);
}
}
+
+ try {
+ $output->writeln('Importing profile config from ' . AccountMigrator::PATH_CONFIG_FILE . '…');
+ /** @var array $configData */
+ $configData = json_decode($importSource->getFileContents(AccountMigrator::PATH_CONFIG_FILE), true, 512, JSON_THROW_ON_ERROR);
+ // Ensure that a profile config entry exists in the database
+ $this->profileManager->getProfileConfig($user, $user);
+ $config = $this->configMapper->get($user->getUID());
+ $config->setConfigArray($configData);
+ $this->configMapper->update($config);
+ } catch (Throwable $e) {
+ throw new AccountMigratorException('Failed to import profile config');
+ }
}
/**