Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a method to get estimated export size in IMigrator #32206

Merged
merged 8 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion apps/dav/lib/UserMigration/CalendarMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use OCP\UserMigration\IExportDestination;
use OCP\UserMigration\IImportSource;
use OCP\UserMigration\IMigrator;
use OCP\UserMigration\ISizeEstimationMigrator;
use OCP\UserMigration\TMigratorBasicVersionHandling;
use Sabre\VObject\Component as VObjectComponent;
use Sabre\VObject\Component\VCalendar;
Expand All @@ -50,10 +51,11 @@
use Sabre\VObject\Reader as VObjectReader;
use Sabre\VObject\UUIDUtil;
use Safe\Exceptions\StringsException;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;

class CalendarMigrator implements IMigrator {
class CalendarMigrator implements IMigrator, ISizeEstimationMigrator {

use TMigratorBasicVersionHandling;

Expand Down Expand Up @@ -206,6 +208,31 @@ private function getUniqueCalendarUri(IUser $user, string $initialCalendarUri):
return $calendarUri;
}

/**
* {@inheritDoc}
*/
public function getEstimatedExportSize(IUser $user): int {
$calendarExports = $this->getCalendarExports($user, new NullOutput());
come-nc marked this conversation as resolved.
Show resolved Hide resolved
$calendarCount = count($calendarExports);

// 150B for top-level properties
$size = ($calendarCount * 150) / 1024;

$componentCount = array_sum(array_map(
function (array $data): int {
/** @var VCalendar $vCalendar */
$vCalendar = $data['vCalendar'];
return count($vCalendar->getComponents());
},
$calendarExports,
));

// 450B for each component (events, todos, alarms, etc.)
$size += ($componentCount * 450) / 1024;

return (int)ceil($size);
}

/**
* {@inheritDoc}
*/
Expand Down
25 changes: 24 additions & 1 deletion apps/dav/lib/UserMigration/ContactsMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use OCP\UserMigration\IExportDestination;
use OCP\UserMigration\IImportSource;
use OCP\UserMigration\IMigrator;
use OCP\UserMigration\ISizeEstimationMigrator;
use OCP\UserMigration\TMigratorBasicVersionHandling;
use Sabre\VObject\Component\VCard;
use Sabre\VObject\Parser\Parser as VObjectParser;
Expand All @@ -47,10 +48,11 @@
use Sabre\VObject\UUIDUtil;
use Safe\Exceptions\ArrayException;
use Safe\Exceptions\StringsException;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;

class ContactsMigrator implements IMigrator {
class ContactsMigrator implements IMigrator, ISizeEstimationMigrator {

use TMigratorBasicVersionHandling;

Expand Down Expand Up @@ -193,6 +195,27 @@ private function serializeCards(array $vCards): string {
);
}

/**
* {@inheritDoc}
*/
public function getEstimatedExportSize(IUser $user): int {
$addressBookExports = $this->getAddressBookExports($user, new NullOutput());
$addressBookCount = count($addressBookExports);

// 50B for each metadata JSON
$size = ($addressBookCount * 50) / 1024;

$contactsCount = array_sum(array_map(
fn (array $data): int => count($data['vCards']),
$addressBookExports,
));

// 350B for each contact
$size += ($contactsCount * 350) / 1024;

return (int)ceil($size);
}

/**
* {@inheritDoc}
*/
Expand Down
20 changes: 19 additions & 1 deletion apps/files_trashbin/lib/UserMigration/TrashbinMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@
use OCP\UserMigration\IExportDestination;
use OCP\UserMigration\IImportSource;
use OCP\UserMigration\IMigrator;
use OCP\UserMigration\ISizeEstimationMigrator;
use OCP\UserMigration\TMigratorBasicVersionHandling;
use OCP\UserMigration\UserMigrationException;
use Symfony\Component\Console\Output\OutputInterface;

class TrashbinMigrator implements IMigrator {
class TrashbinMigrator implements IMigrator, ISizeEstimationMigrator {

use TMigratorBasicVersionHandling;

Expand All @@ -63,6 +64,23 @@ public function __construct(
$this->l10n = $l10n;
}

/**
* {@inheritDoc}
*/
public function getEstimatedExportSize(IUser $user): int {
$uid = $user->getUID();

try {
$trashbinFolder = $this->root->get('/'.$uid.'/files_trashbin');
if (!$trashbinFolder instanceof Folder) {
return 0;
}
return (int)ceil($trashbinFolder->getSize() / 1024);
} catch (\Throwable $e) {
return 0;
}
}

/**
* {@inheritDoc}
*/
Expand Down
22 changes: 21 additions & 1 deletion apps/settings/lib/UserMigration/AccountMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@
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;
Expand All @@ -68,6 +69,25 @@ public function __construct(
$this->l10n = $l10n;
}

/**
* {@inheritDoc}
*/
public function getEstimatedExportSize(IUser $user): int {
$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 (int)ceil($size);
}

/**
* {@inheritDoc}
*/
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@
'OCP\\UserMigration\\IExportDestination' => $baseDir . '/lib/public/UserMigration/IExportDestination.php',
'OCP\\UserMigration\\IImportSource' => $baseDir . '/lib/public/UserMigration/IImportSource.php',
'OCP\\UserMigration\\IMigrator' => $baseDir . '/lib/public/UserMigration/IMigrator.php',
'OCP\\UserMigration\\ISizeEstimationMigrator' => $baseDir . '/lib/public/UserMigration/ISizeEstimationMigrator.php',
'OCP\\UserMigration\\TMigratorBasicVersionHandling' => $baseDir . '/lib/public/UserMigration/TMigratorBasicVersionHandling.php',
'OCP\\UserMigration\\UserMigrationException' => $baseDir . '/lib/public/UserMigration/UserMigrationException.php',
'OCP\\UserStatus\\IManager' => $baseDir . '/lib/public/UserStatus/IManager.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\UserMigration\\IExportDestination' => __DIR__ . '/../../..' . '/lib/public/UserMigration/IExportDestination.php',
'OCP\\UserMigration\\IImportSource' => __DIR__ . '/../../..' . '/lib/public/UserMigration/IImportSource.php',
'OCP\\UserMigration\\IMigrator' => __DIR__ . '/../../..' . '/lib/public/UserMigration/IMigrator.php',
'OCP\\UserMigration\\ISizeEstimationMigrator' => __DIR__ . '/../../..' . '/lib/public/UserMigration/ISizeEstimationMigrator.php',
'OCP\\UserMigration\\TMigratorBasicVersionHandling' => __DIR__ . '/../../..' . '/lib/public/UserMigration/TMigratorBasicVersionHandling.php',
'OCP\\UserMigration\\UserMigrationException' => __DIR__ . '/../../..' . '/lib/public/UserMigration/UserMigrationException.php',
'OCP\\UserStatus\\IManager' => __DIR__ . '/../../..' . '/lib/public/UserStatus/IManager.php',
Expand Down
43 changes: 43 additions & 0 deletions lib/public/UserMigration/ISizeEstimationMigrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

/**
* @copyright 2022 Christopher Ng <chrng8@gmail.com>
*
* @author Christopher Ng <chrng8@gmail.com>
* @author Côme Chilliet <come.chilliet@nextcloud.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/>.
*
*/

namespace OCP\UserMigration;

use OCP\IUser;

/**
* @since 25.0.0
*/
interface ISizeEstimationMigrator {
/**
* Returns an estimate of the exported data size in KiB.
* Should be fast, favor performance over accuracy.
*
* @since 25.0.0
*/
public function getEstimatedExportSize(IUser $user): int;
}