2727namespace OC \Accounts ;
2828
2929use InvalidArgumentException ;
30+ use OC \NotSquareException ;
3031use OCP \Accounts \IAccountManager ;
32+ use OCP \IAvatarManager ;
3133use OCP \IUser ;
3234use OCP \UserMigration \IExportDestination ;
3335use OCP \UserMigration \IImportSource ;
3436use OCP \UserMigration \IMigrator ;
3537use OCP \UserMigration \TMigratorBasicVersionHandling ;
3638use Symfony \Component \Console \Output \OutputInterface ;
39+ use Throwable ;
3740
3841class 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