Skip to content

Commit

Permalink
Add export estimate endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Ng <chrng8@gmail.com>
  • Loading branch information
Pytal committed Jun 3, 2022
1 parent 14b7f2a commit bb1a078
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
3 changes: 2 additions & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
['name' => 'Api#migrators', 'url' => '/api/v{apiVersion}/migrators', 'verb' => 'GET', 'requirements' => $requirements],
['name' => 'Api#status', 'url' => '/api/v{apiVersion}/status', 'verb' => 'GET', 'requirements' => $requirements],
['name' => 'Api#cancel', 'url' => '/api/v{apiVersion}/cancel', 'verb' => 'PUT', 'requirements' => $requirements],
['name' => 'Api#exportable', 'url' => '/api/v{apiVersion}/exportable', 'verb' => 'GET', 'requirements' => $requirements],
['name' => 'Api#estimate', 'url' => '/api/v{apiVersion}/export/estimate', 'verb' => 'GET', 'requirements' => $requirements],
['name' => 'Api#exportable', 'url' => '/api/v{apiVersion}/export', 'verb' => 'GET', 'requirements' => $requirements],
['name' => 'Api#export', 'url' => '/api/v{apiVersion}/export', 'verb' => 'POST', 'requirements' => $requirements],
['name' => 'Api#import', 'url' => '/api/v{apiVersion}/import', 'verb' => 'POST', 'requirements' => $requirements],
],
Expand Down
22 changes: 22 additions & 0 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,28 @@ private function checkJobAndGetUser(): IUser {
return $user;
}

/**
* @NoAdminRequired
* @NoSubAdminRequired
*
* @throws OCSException
*/
public function estimate(?array $migrators): DataResponse {
$user = $this->checkJobAndGetUser();

if (!is_null($migrators)) {
$this->checkMigrators($migrators);
}

try {
$size = $this->migrationService->estimateExportSize($user, $migrators);
} catch (UserMigrationException $e) {
throw new OCSException($e->getMessage());
}

return new DataResponse(['size' => $size], Http::STATUS_OK);
}

/**
* @NoAdminRequired
* @NoSubAdminRequired
Expand Down
10 changes: 8 additions & 2 deletions lib/Service/UserMigrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,10 @@ public function __construct(
* @param ?string[] $filteredMigratorList If not null, only these migrators will run. If empty only the main account data will be exported.
*
* @return int Estimated size in KiB
*
* @throws UserMigrationException
*/
private function estimateExportSize(IUser $user, ?array $filteredMigratorList = null): int {
public function estimateExportSize(IUser $user, ?array $filteredMigratorList = null): int {
// 1MiB for base user data
$size = 1024;

Expand All @@ -125,7 +127,11 @@ private function estimateExportSize(IUser $user, ?array $filteredMigratorList =
continue;
}
if ($migrator instanceof ISizeEstimationMigrator) {
$migratorSize = $migrator->getEstimatedExportSize($user);
try {
$migratorSize = $migrator->getEstimatedExportSize($user);
} catch (Throwable $e) {
throw new UserMigrationException('Could not estimate export size for ' . $migrator->getDisplayName(), 0, $e);
}
$this->internalCache->set($cacheKey, $migratorSize);
$size += $migratorSize;
}
Expand Down

0 comments on commit bb1a078

Please sign in to comment.