Skip to content

Commit e004ce7

Browse files
committed
perf: use more optimized way to get user storage info in ocs user info
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent 8995272 commit e004ce7

File tree

5 files changed

+61
-28
lines changed

5 files changed

+61
-28
lines changed

apps/provisioning_api/lib/Controller/AUserData.php

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use OCP\AppFramework\OCS\OCSException;
2020
use OCP\AppFramework\OCS\OCSNotFoundException;
2121
use OCP\AppFramework\OCSController;
22+
use OCP\Files\FileInfo;
23+
use OCP\Files\IRootFolder;
2224
use OCP\Files\NotFoundException;
2325
use OCP\Group\ISubAdmin;
2426
use OCP\IConfig;
@@ -56,6 +58,7 @@ public function __construct(
5658
protected IAccountManager $accountManager,
5759
protected ISubAdmin $subAdminManager,
5860
protected IFactory $l10nFactory,
61+
protected IRootFolder $rootFolder,
5962
) {
6063
parent::__construct($appName, $request);
6164
}
@@ -119,7 +122,7 @@ protected function getUserData(string $userId, bool $includeScopes = false): ?ar
119122
$data['lastLogin'] = $targetUserObject->getLastLogin() * 1000;
120123
$data['backend'] = $targetUserObject->getBackendClassName();
121124
$data['subadmin'] = $this->getUserSubAdminGroupsData($targetUserObject->getUID());
122-
$data[self::USER_FIELD_QUOTA] = $this->fillStorageInfo($targetUserObject->getUID());
125+
$data[self::USER_FIELD_QUOTA] = $this->fillStorageInfo($targetUserObject);
123126
$managers = $this->getManagers($targetUserObject);
124127
$data[self::USER_FIELD_MANAGER] = empty($managers) ? '' : $managers[0];
125128

@@ -243,34 +246,48 @@ protected function getUserSubAdminGroupsData(string $userId): array {
243246
}
244247

245248
/**
246-
* @param string $userId
249+
* @param IUser $user
247250
* @return Provisioning_APIUserDetailsQuota
248251
* @throws OCSException
249252
*/
250-
protected function fillStorageInfo(string $userId): array {
253+
protected function fillStorageInfo(IUser $user): array {
254+
$userId = $user->getUID();
255+
256+
$quota = $user->getQuota();
257+
if ($quota === 'none') {
258+
$quota = FileInfo::SPACE_UNLIMITED;
259+
} else {
260+
$quota = OC_Helper::computerFileSize($quota);
261+
if ($quota === false) {
262+
$quota = FileInfo::SPACE_UNLIMITED;
263+
}
264+
}
265+
251266
try {
252-
\OC_Util::tearDownFS();
253-
\OC_Util::setupFS($userId);
254-
$storage = OC_Helper::getStorageInfo('/', null, true, false);
267+
$userFileInfo = $this->rootFolder->getUserFolder($userId)->getStorage()->getCache()->get('');
268+
$used = $userFileInfo->getSize();
269+
270+
if ($quota > 0) {
271+
// prevent division by zero or error codes (negative values)
272+
$relative = round(($used / $quota) * 10000) / 100;
273+
$free = $quota - $used;
274+
$total = $quota;
275+
} else {
276+
$relative = 0;
277+
$free = FileInfo::SPACE_UNLIMITED;
278+
$total = $used;
279+
}
280+
255281
$data = [
256-
'free' => $storage['free'],
257-
'used' => $storage['used'],
258-
'total' => $storage['total'],
259-
'relative' => $storage['relative'],
260-
self::USER_FIELD_QUOTA => $storage['quota'],
282+
'free' => $free,
283+
'used' => $used,
284+
'total' => $total,
285+
'relative' => $relative,
286+
self::USER_FIELD_QUOTA => $quota,
261287
];
262288
} catch (NotFoundException $ex) {
263-
// User fs is not setup yet
264-
$user = $this->userManager->get($userId);
265-
if ($user === null) {
266-
throw new OCSException('User does not exist', 101);
267-
}
268-
$quota = $user->getQuota();
269-
if ($quota !== 'none') {
270-
$quota = OC_Helper::computerFileSize($quota);
271-
}
272289
$data = [
273-
self::USER_FIELD_QUOTA => $quota !== false ? $quota : 'none',
290+
self::USER_FIELD_QUOTA => $quota >= 0 ? $quota : 'none',
274291
'used' => 0
275292
];
276293
} catch (\Exception $e) {
@@ -282,8 +299,6 @@ protected function fillStorageInfo(string $userId): array {
282299
'exception' => $e,
283300
]
284301
);
285-
/* In case the Exception left things in a bad state */
286-
\OC_Util::tearDownFS();
287302
return [];
288303
}
289304
return $data;

apps/provisioning_api/lib/Controller/GroupsController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use OCP\AppFramework\OCS\OCSForbiddenException;
2222
use OCP\AppFramework\OCS\OCSNotFoundException;
2323
use OCP\AppFramework\OCSController;
24+
use OCP\Files\IRootFolder;
2425
use OCP\Group\ISubAdmin;
2526
use OCP\IConfig;
2627
use OCP\IGroup;
@@ -48,6 +49,7 @@ public function __construct(
4849
IAccountManager $accountManager,
4950
ISubAdmin $subAdminManager,
5051
IFactory $l10nFactory,
52+
IRootFolder $rootFolder,
5153
private LoggerInterface $logger,
5254
) {
5355
parent::__construct($appName,
@@ -58,7 +60,8 @@ public function __construct(
5860
$userSession,
5961
$accountManager,
6062
$subAdminManager,
61-
$l10nFactory
63+
$l10nFactory,
64+
$rootFolder,
6265
);
6366
}
6467

apps/provisioning_api/lib/Controller/UsersController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use OCP\AppFramework\OCS\OCSNotFoundException;
3232
use OCP\AppFramework\OCSController;
3333
use OCP\EventDispatcher\IEventDispatcher;
34+
use OCP\Files\IRootFolder;
3435
use OCP\Group\ISubAdmin;
3536
use OCP\HintException;
3637
use OCP\IConfig;
@@ -67,6 +68,7 @@ public function __construct(
6768
IAccountManager $accountManager,
6869
ISubAdmin $subAdminManager,
6970
IFactory $l10nFactory,
71+
IRootFolder $rootFolder,
7072
private IURLGenerator $urlGenerator,
7173
private LoggerInterface $logger,
7274
private NewUserMailHelper $newUserMailHelper,
@@ -85,7 +87,8 @@ public function __construct(
8587
$userSession,
8688
$accountManager,
8789
$subAdminManager,
88-
$l10nFactory
90+
$l10nFactory,
91+
$rootFolder,
8992
);
9093

9194
$this->l10n = $l10nFactory->get($appName);

apps/provisioning_api/tests/Controller/GroupsControllerTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use OCA\Provisioning_API\Controller\GroupsController;
1313
use OCP\Accounts\IAccountManager;
1414
use OCP\AppFramework\OCS\OCSException;
15+
use OCP\Files\IRootFolder;
1516
use OCP\Group\ISubAdmin;
1617
use OCP\IConfig;
1718
use OCP\IGroup;
@@ -46,6 +47,8 @@ class GroupsControllerTest extends \Test\TestCase {
4647
/** @var GroupsController|\PHPUnit\Framework\MockObject\MockObject */
4748
protected $api;
4849

50+
private IRootFolder $rootFolder;
51+
4952

5053
protected function setUp(): void {
5154
parent::setUp();
@@ -59,6 +62,7 @@ protected function setUp(): void {
5962
$this->subAdminManager = $this->createMock(ISubAdmin::class);
6063
$this->l10nFactory = $this->createMock(IFactory::class);
6164
$this->logger = $this->createMock(LoggerInterface::class);
65+
$this->rootFolder = $this->createMock(IRootFolder::class);
6266

6367
$this->groupManager
6468
->method('getSubAdmin')
@@ -75,6 +79,7 @@ protected function setUp(): void {
7579
$this->accountManager,
7680
$this->subAdminManager,
7781
$this->l10nFactory,
82+
$this->rootFolder,
7883
$this->logger
7984
])
8085
->setMethods(['fillStorageInfo'])

apps/provisioning_api/tests/Controller/UsersControllerTest.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use OCP\AppFramework\Http\DataResponse;
2424
use OCP\AppFramework\OCS\OCSException;
2525
use OCP\EventDispatcher\IEventDispatcher;
26+
use OCP\Files\IRootFolder;
2627
use OCP\Group\ISubAdmin;
2728
use OCP\IConfig;
2829
use OCP\IGroup;
@@ -76,6 +77,7 @@ class UsersControllerTest extends TestCase {
7677
private $knownUserService;
7778
/** @var IEventDispatcher|MockObject */
7879
private $eventDispatcher;
80+
private IRootFolder $rootFolder;
7981
/** @var IPhoneNumberUtil */
8082
private $phoneNumberUtil;
8183

@@ -98,6 +100,7 @@ protected function setUp(): void {
98100
$this->knownUserService = $this->createMock(KnownUserService::class);
99101
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
100102
$this->phoneNumberUtil = new PhoneNumberUtil();
103+
$this->rootFolder = $this->createMock(IRootFolder::class);
101104

102105
$l10n = $this->createMock(IL10N::class);
103106
$l10n->method('t')->willReturnCallback(fn (string $txt, array $replacement = []) => sprintf($txt, ...$replacement));
@@ -114,6 +117,7 @@ protected function setUp(): void {
114117
$this->accountManager,
115118
$this->subAdminManager,
116119
$this->l10nFactory,
120+
$this->rootFolder,
117121
$this->urlGenerator,
118122
$this->logger,
119123
$this->newUserMailHelper,
@@ -508,6 +512,7 @@ public function testAddUserSuccessfulWithDisplayName(): void {
508512
$this->accountManager,
509513
$this->subAdminManager,
510514
$this->l10nFactory,
515+
$this->rootFolder,
511516
$this->urlGenerator,
512517
$this->logger,
513518
$this->newUserMailHelper,
@@ -1127,7 +1132,7 @@ public function testGetUserDataAsAdmin(): void {
11271132
$this->api
11281133
->expects($this->once())
11291134
->method('fillStorageInfo')
1130-
->with('UID')
1135+
->with($targetUser)
11311136
->willReturn(['DummyValue']);
11321137

11331138
$backend = $this->createMock(UserInterface::class);
@@ -1254,7 +1259,7 @@ public function testGetUserDataAsSubAdminAndUserIsAccessible(): void {
12541259
$this->api
12551260
->expects($this->once())
12561261
->method('fillStorageInfo')
1257-
->with('UID')
1262+
->with($targetUser)
12581263
->willReturn(['DummyValue']);
12591264

12601265
$backend = $this->createMock(UserInterface::class);
@@ -1429,7 +1434,7 @@ public function testGetUserDataAsSubAdminSelfLookup(): void {
14291434
$this->api
14301435
->expects($this->once())
14311436
->method('fillStorageInfo')
1432-
->with('UID')
1437+
->with($targetUser)
14331438
->willReturn(['DummyValue']);
14341439

14351440
$backend = $this->createMock(UserInterface::class);
@@ -3788,6 +3793,7 @@ public function testGetCurrentUserLoggedIn(): void {
37883793
$this->accountManager,
37893794
$this->subAdminManager,
37903795
$this->l10nFactory,
3796+
$this->rootFolder,
37913797
$this->urlGenerator,
37923798
$this->logger,
37933799
$this->newUserMailHelper,
@@ -3878,6 +3884,7 @@ public function testGetUser(): void {
38783884
$this->accountManager,
38793885
$this->subAdminManager,
38803886
$this->l10nFactory,
3887+
$this->rootFolder,
38813888
$this->urlGenerator,
38823889
$this->logger,
38833890
$this->newUserMailHelper,

0 commit comments

Comments
 (0)