Skip to content

Commit 4ca4228

Browse files
authored
Merge pull request #32664 from nextcloud/backport/32206/stable24
2 parents 01ae33f + a541f97 commit 4ca4228

7 files changed

Lines changed: 137 additions & 4 deletions

File tree

apps/dav/lib/UserMigration/CalendarMigrator.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
use OCP\UserMigration\IExportDestination;
4343
use OCP\UserMigration\IImportSource;
4444
use OCP\UserMigration\IMigrator;
45+
use OCP\UserMigration\ISizeEstimationMigrator;
4546
use OCP\UserMigration\TMigratorBasicVersionHandling;
4647
use Sabre\VObject\Component as VObjectComponent;
4748
use Sabre\VObject\Component\VCalendar;
@@ -50,10 +51,11 @@
5051
use Sabre\VObject\Reader as VObjectReader;
5152
use Sabre\VObject\UUIDUtil;
5253
use Safe\Exceptions\StringsException;
54+
use Symfony\Component\Console\Output\NullOutput;
5355
use Symfony\Component\Console\Output\OutputInterface;
5456
use Throwable;
5557

56-
class CalendarMigrator implements IMigrator {
58+
class CalendarMigrator implements IMigrator, ISizeEstimationMigrator {
5759

5860
use TMigratorBasicVersionHandling;
5961

@@ -206,6 +208,31 @@ private function getUniqueCalendarUri(IUser $user, string $initialCalendarUri):
206208
return $calendarUri;
207209
}
208210

211+
/**
212+
* {@inheritDoc}
213+
*/
214+
public function getEstimatedExportSize(IUser $user): int {
215+
$calendarExports = $this->getCalendarExports($user, new NullOutput());
216+
$calendarCount = count($calendarExports);
217+
218+
// 150B for top-level properties
219+
$size = ($calendarCount * 150) / 1024;
220+
221+
$componentCount = array_sum(array_map(
222+
function (array $data): int {
223+
/** @var VCalendar $vCalendar */
224+
$vCalendar = $data['vCalendar'];
225+
return count($vCalendar->getComponents());
226+
},
227+
$calendarExports,
228+
));
229+
230+
// 450B for each component (events, todos, alarms, etc.)
231+
$size += ($componentCount * 450) / 1024;
232+
233+
return (int)ceil($size);
234+
}
235+
209236
/**
210237
* {@inheritDoc}
211238
*/

apps/dav/lib/UserMigration/ContactsMigrator.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
use OCP\UserMigration\IExportDestination;
4040
use OCP\UserMigration\IImportSource;
4141
use OCP\UserMigration\IMigrator;
42+
use OCP\UserMigration\ISizeEstimationMigrator;
4243
use OCP\UserMigration\TMigratorBasicVersionHandling;
4344
use Sabre\VObject\Component\VCard;
4445
use Sabre\VObject\Parser\Parser as VObjectParser;
@@ -47,10 +48,11 @@
4748
use Sabre\VObject\UUIDUtil;
4849
use Safe\Exceptions\ArrayException;
4950
use Safe\Exceptions\StringsException;
51+
use Symfony\Component\Console\Output\NullOutput;
5052
use Symfony\Component\Console\Output\OutputInterface;
5153
use Throwable;
5254

53-
class ContactsMigrator implements IMigrator {
55+
class ContactsMigrator implements IMigrator, ISizeEstimationMigrator {
5456

5557
use TMigratorBasicVersionHandling;
5658

@@ -193,6 +195,27 @@ private function serializeCards(array $vCards): string {
193195
);
194196
}
195197

198+
/**
199+
* {@inheritDoc}
200+
*/
201+
public function getEstimatedExportSize(IUser $user): int {
202+
$addressBookExports = $this->getAddressBookExports($user, new NullOutput());
203+
$addressBookCount = count($addressBookExports);
204+
205+
// 50B for each metadata JSON
206+
$size = ($addressBookCount * 50) / 1024;
207+
208+
$contactsCount = array_sum(array_map(
209+
fn (array $data): int => count($data['vCards']),
210+
$addressBookExports,
211+
));
212+
213+
// 350B for each contact
214+
$size += ($contactsCount * 350) / 1024;
215+
216+
return (int)ceil($size);
217+
}
218+
196219
/**
197220
* {@inheritDoc}
198221
*/

apps/files_trashbin/lib/UserMigration/TrashbinMigrator.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@
3636
use OCP\UserMigration\IExportDestination;
3737
use OCP\UserMigration\IImportSource;
3838
use OCP\UserMigration\IMigrator;
39+
use OCP\UserMigration\ISizeEstimationMigrator;
3940
use OCP\UserMigration\TMigratorBasicVersionHandling;
4041
use OCP\UserMigration\UserMigrationException;
4142
use Symfony\Component\Console\Output\OutputInterface;
4243

43-
class TrashbinMigrator implements IMigrator {
44+
class TrashbinMigrator implements IMigrator, ISizeEstimationMigrator {
4445

4546
use TMigratorBasicVersionHandling;
4647

@@ -63,6 +64,23 @@ public function __construct(
6364
$this->l10n = $l10n;
6465
}
6566

67+
/**
68+
* {@inheritDoc}
69+
*/
70+
public function getEstimatedExportSize(IUser $user): int {
71+
$uid = $user->getUID();
72+
73+
try {
74+
$trashbinFolder = $this->root->get('/'.$uid.'/files_trashbin');
75+
if (!$trashbinFolder instanceof Folder) {
76+
return 0;
77+
}
78+
return (int)ceil($trashbinFolder->getSize() / 1024);
79+
} catch (\Throwable $e) {
80+
return 0;
81+
}
82+
}
83+
6684
/**
6785
* {@inheritDoc}
6886
*/

apps/settings/lib/UserMigration/AccountMigrator.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@
3737
use OCP\UserMigration\IExportDestination;
3838
use OCP\UserMigration\IImportSource;
3939
use OCP\UserMigration\IMigrator;
40+
use OCP\UserMigration\ISizeEstimationMigrator;
4041
use OCP\UserMigration\TMigratorBasicVersionHandling;
4142
use Symfony\Component\Console\Output\OutputInterface;
4243
use Throwable;
4344

44-
class AccountMigrator implements IMigrator {
45+
class AccountMigrator implements IMigrator, ISizeEstimationMigrator {
4546
use TMigratorBasicVersionHandling;
4647

4748
use TAccountsHelper;
@@ -68,6 +69,25 @@ public function __construct(
6869
$this->l10n = $l10n;
6970
}
7071

72+
/**
73+
* {@inheritDoc}
74+
*/
75+
public function getEstimatedExportSize(IUser $user): int {
76+
$size = 100; // 100KiB for account JSON
77+
78+
try {
79+
$avatar = $this->avatarManager->getAvatar($user->getUID());
80+
if ($avatar->isCustomAvatar()) {
81+
$avatarFile = $avatar->getFile(-1);
82+
$size += $avatarFile->getSize() / 1024;
83+
}
84+
} catch (Throwable $e) {
85+
// Skip avatar in size estimate on failure
86+
}
87+
88+
return (int)ceil($size);
89+
}
90+
7191
/**
7292
* {@inheritDoc}
7393
*/

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@
563563
'OCP\\UserMigration\\IExportDestination' => $baseDir . '/lib/public/UserMigration/IExportDestination.php',
564564
'OCP\\UserMigration\\IImportSource' => $baseDir . '/lib/public/UserMigration/IImportSource.php',
565565
'OCP\\UserMigration\\IMigrator' => $baseDir . '/lib/public/UserMigration/IMigrator.php',
566+
'OCP\\UserMigration\\ISizeEstimationMigrator' => $baseDir . '/lib/public/UserMigration/ISizeEstimationMigrator.php',
566567
'OCP\\UserMigration\\TMigratorBasicVersionHandling' => $baseDir . '/lib/public/UserMigration/TMigratorBasicVersionHandling.php',
567568
'OCP\\UserMigration\\UserMigrationException' => $baseDir . '/lib/public/UserMigration/UserMigrationException.php',
568569
'OCP\\UserStatus\\IManager' => $baseDir . '/lib/public/UserStatus/IManager.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
592592
'OCP\\UserMigration\\IExportDestination' => __DIR__ . '/../../..' . '/lib/public/UserMigration/IExportDestination.php',
593593
'OCP\\UserMigration\\IImportSource' => __DIR__ . '/../../..' . '/lib/public/UserMigration/IImportSource.php',
594594
'OCP\\UserMigration\\IMigrator' => __DIR__ . '/../../..' . '/lib/public/UserMigration/IMigrator.php',
595+
'OCP\\UserMigration\\ISizeEstimationMigrator' => __DIR__ . '/../../..' . '/lib/public/UserMigration/ISizeEstimationMigrator.php',
595596
'OCP\\UserMigration\\TMigratorBasicVersionHandling' => __DIR__ . '/../../..' . '/lib/public/UserMigration/TMigratorBasicVersionHandling.php',
596597
'OCP\\UserMigration\\UserMigrationException' => __DIR__ . '/../../..' . '/lib/public/UserMigration/UserMigrationException.php',
597598
'OCP\\UserStatus\\IManager' => __DIR__ . '/../../..' . '/lib/public/UserStatus/IManager.php',
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright 2022 Christopher Ng <chrng8@gmail.com>
7+
*
8+
* @author Christopher Ng <chrng8@gmail.com>
9+
* @author Côme Chilliet <come.chilliet@nextcloud.com>
10+
*
11+
* @license GNU AGPL version 3 or any later version
12+
*
13+
* This program is free software: you can redistribute it and/or modify
14+
* it under the terms of the GNU Affero General Public License as
15+
* published by the Free Software Foundation, either version 3 of the
16+
* License, or (at your option) any later version.
17+
*
18+
* This program is distributed in the hope that it will be useful,
19+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
20+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21+
* GNU Affero General Public License for more details.
22+
*
23+
* You should have received a copy of the GNU Affero General Public License
24+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
25+
*
26+
*/
27+
28+
namespace OCP\UserMigration;
29+
30+
use OCP\IUser;
31+
32+
/**
33+
* @since 25.0.0
34+
*/
35+
interface ISizeEstimationMigrator {
36+
/**
37+
* Returns an estimate of the exported data size in KiB.
38+
* Should be fast, favor performance over accuracy.
39+
*
40+
* @since 25.0.0
41+
*/
42+
public function getEstimatedExportSize(IUser $user): int;
43+
}

0 commit comments

Comments
 (0)