Skip to content

Commit 664e827

Browse files
committed
Avatar export and import
Signed-off-by: Christopher Ng <chrng8@gmail.com>
1 parent ae50573 commit 664e827

1 file changed

Lines changed: 50 additions & 2 deletions

File tree

lib/private/Accounts/AccountMigrator.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,45 @@
2727
namespace OC\Accounts;
2828

2929
use InvalidArgumentException;
30+
use OC\NotSquareException;
3031
use OCP\Accounts\IAccountManager;
32+
use OCP\IAvatarManager;
3133
use OCP\IUser;
3234
use OCP\UserMigration\IExportDestination;
3335
use OCP\UserMigration\IImportSource;
3436
use OCP\UserMigration\IMigrator;
3537
use OCP\UserMigration\TMigratorBasicVersionHandling;
3638
use Symfony\Component\Console\Output\OutputInterface;
39+
use Throwable;
3740

3841
class AccountMigrator implements IMigrator {
39-
4042
use TMigratorBasicVersionHandling;
4143

4244
use TAccountsHelper;
4345

4446
private IAccountManager $accountManager;
4547

48+
private IAvatarManager $avatarManager;
49+
4650
private const EXPORT_FILE = 'account.json';
4751

4852
public function __construct(
49-
IAccountManager $accountManager
53+
IAccountManager $accountManager,
54+
IAvatarManager $avatarManager
5055
) {
5156
$this->accountManager = $accountManager;
57+
$this->avatarManager = $avatarManager;
58+
}
59+
60+
private function getExtension(string $mimeType): string {
61+
switch ($mimeType) {
62+
case 'image/jpeg':
63+
return 'jpg';
64+
case 'image/png':
65+
return 'png';
66+
default:
67+
throw new AccountMigratorException("Invalid avatar mimetype: \"$mimeType\"");
68+
}
5269
}
5370

5471
/**
@@ -60,6 +77,16 @@ public function export(IUser $user, IExportDestination $exportDestination, Outpu
6077
if ($exportDestination->addFileContents(AccountMigrator::EXPORT_FILE, json_encode($this->accountManager->getAccount($user))) === false) {
6178
throw new AccountMigratorException('Could not export account information');
6279
}
80+
81+
$avatar = $this->avatarManager->getAvatar($user->getUID());
82+
if ($avatar->isCustomAvatar()) {
83+
$avatarData = $avatar->get(-1)->data();
84+
$ext = $this->getExtension($avatar->get(-1)->dataMimeType());
85+
$output->writeln('Exporting avatar to avatar.' . $ext . '');
86+
if ($exportDestination->addFileContents("avatar.$ext", $avatarData) === false) {
87+
throw new AccountMigratorException('Could not export avatar');
88+
}
89+
}
6390
}
6491

6592
/**
@@ -99,5 +126,26 @@ public function import(IUser $user, IImportSource $importSource, OutputInterface
99126
} catch (InvalidArgumentException $e) {
100127
throw new AccountMigratorException('Failed to import account information');
101128
}
129+
130+
foreach ($importSource->getFolderListing('') as $filename) {
131+
if (str_starts_with($filename, 'avatar.')) {
132+
$avatarFilename = $filename;
133+
}
134+
}
135+
136+
if (isset($avatarFilename)) {
137+
$output->writeln('Importing avatar from ' . $avatarFilename . '');
138+
$avatar = $importSource->getFileContents($avatarFilename);
139+
$image = new \OC_Image();
140+
$image->loadFromData($avatar);
141+
try {
142+
$avatar = $this->avatarManager->getAvatar($user->getUID());
143+
$avatar->set($image);
144+
} catch (NotSquareException $e) {
145+
throw new AccountMigratorException('Avatar image must be square');
146+
} catch (Throwable $e) {
147+
throw new AccountMigratorException('Failed to import avatar', 0, $e);
148+
}
149+
}
102150
}
103151
}

0 commit comments

Comments
 (0)