Skip to content

Commit 7014a0f

Browse files
committed
Update avatar handling
Signed-off-by: Christopher Ng <chrng8@gmail.com>
1 parent 7cb0687 commit 7014a0f

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

lib/private/Accounts/AccountMigrator.php

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,6 @@ public function __construct(
5757
$this->avatarManager = $avatarManager;
5858
}
5959

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-
}
69-
}
70-
7160
/**
7261
* {@inheritDoc}
7362
*/
@@ -80,10 +69,12 @@ public function export(IUser $user, IExportDestination $exportDestination, Outpu
8069

8170
$avatar = $this->avatarManager->getAvatar($user->getUID());
8271
if ($avatar->isCustomAvatar()) {
83-
$avatarData = $avatar->get(-1)->data();
84-
$ext = $this->getExtension($avatar->get(-1)->dataMimeType());
72+
$avatarFile = $avatar->getFile(-1);
73+
$data = $avatarFile->getContent();
74+
$ext = $avatarFile->getExtension();
75+
8576
$output->writeln('Exporting avatar to avatar.' . $ext . '');
86-
if ($exportDestination->addFileContents("avatar.$ext", $avatarData) === false) {
77+
if ($exportDestination->addFileAsStream("avatar.$ext", $data) === false) {
8778
throw new AccountMigratorException('Could not export avatar');
8879
}
8980
}
@@ -127,17 +118,22 @@ public function import(IUser $user, IImportSource $importSource, OutputInterface
127118
throw new AccountMigratorException('Failed to import account information');
128119
}
129120

130-
foreach ($importSource->getFolderListing('') as $filename) {
131-
if (str_starts_with($filename, 'avatar.')) {
132-
$avatarFilename = $filename;
121+
$avatarFiles = array_filter(
122+
$importSource->getFolderListing(''),
123+
fn (string $filename) => reset(explode('.', $filename)) === 'avatar',
124+
);
125+
126+
if (!empty($avatarFiles)) {
127+
$avatarImportFile = reset($avatarFiles);
128+
129+
if (count($avatarFiles) >= 2) {
130+
$output->writeln('Expected single avatar image file, using first found file');
133131
}
134-
}
135132

136-
if (isset($avatarFilename)) {
137-
$output->writeln('Importing avatar from ' . $avatarFilename . '');
138-
$avatar = $importSource->getFileContents($avatarFilename);
133+
$output->writeln('Importing avatar from ' . $avatarImportFile . '');
134+
$data = $importSource->getFileContents($avatarImportFile);
139135
$image = new \OC_Image();
140-
$image->loadFromData($avatar);
136+
$image->loadFromData($data);
141137
try {
142138
$avatar = $this->avatarManager->getAvatar($user->getUID());
143139
$avatar->set($image);

0 commit comments

Comments
 (0)