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

[stable29] perf: use more optimized way to get user storage info in ocs user info #50211

Closed
wants to merge 1 commit into from
Closed
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
78 changes: 53 additions & 25 deletions apps/provisioning_api/lib/Controller/AUserData.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@
use OC\Group\Manager;
use OC\User\Backend;
use OC\User\NoUserException;
use OC_Helper;
use OCA\Provisioning_API\ResponseDefinitions;
use OCP\Accounts\IAccountManager;
use OCP\Accounts\PropertyDoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\OCS\OCSException;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;
use OCP\Files\FileInfo;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\IConfig;
use OCP\IGroupManager;
Expand All @@ -52,6 +53,7 @@
use OCP\L10N\IFactory;
use OCP\User\Backend\ISetDisplayNameBackend;
use OCP\User\Backend\ISetPasswordBackend;
use OCP\Util;

/**
* @psalm-import-type Provisioning_APIUserDetails from ResponseDefinitions
Expand Down Expand Up @@ -251,34 +253,62 @@ protected function getUserSubAdminGroupsData(string $userId): array {
}

/**
* @param string $userId
* @param IUser $user
* @return Provisioning_APIUserDetailsQuota
* @throws OCSException
*/
protected function fillStorageInfo(string $userId): array {
try {
\OC_Util::tearDownFS();
\OC_Util::setupFS($userId);
$storage = OC_Helper::getStorageInfo('/', null, true, false);
$data = [
'free' => $storage['free'],
'used' => $storage['used'],
'total' => $storage['total'],
'relative' => $storage['relative'],
self::USER_FIELD_QUOTA => $storage['quota'],
];
} catch (NotFoundException $ex) {
// User fs is not setup yet
$user = $this->userManager->get($userId);
if ($user === null) {
throw new OCSException('User does not exist', 101);
protected function fillStorageInfo(IUser $user): array {
$includeExternal = $this->config->getSystemValueBool('quota_include_external_storage');
$userId = $user->getUID();

$quota = $user->getQuota();
if ($quota === 'none') {
$quota = FileInfo::SPACE_UNLIMITED;
} else {
$quota = Util::computerFileSize($quota);
if ($quota === false) {
$quota = FileInfo::SPACE_UNLIMITED;
}
$quota = $user->getQuota();
if ($quota !== 'none') {
$quota = OC_Helper::computerFileSize($quota);
}

try {
if ($includeExternal) {
\OC_Util::tearDownFS();
\OC_Util::setupFS($user->getUID());
$storage = \OC_Helper::getStorageInfo('/', null, true, false);
$data = [
'free' => $storage['free'],
'used' => $storage['used'],
'total' => $storage['total'],
'relative' => $storage['relative'],
self::USER_FIELD_QUOTA => $storage['quota'],
];
} else {
$userFileInfo = $this->rootFolder->getUserFolder($userId)->getStorage()->getCache()->get('');
$used = $userFileInfo->getSize();

if ($quota > 0) {
// prevent division by zero or error codes (negative values)
$relative = round(($used / $quota) * 10000) / 100;
$free = $quota - $used;
$total = $quota;
} else {
$relative = 0;
$free = FileInfo::SPACE_UNLIMITED;
$total = FileInfo::SPACE_UNLIMITED;
}

$data = [
'free' => $free,
'used' => $used,
'total' => $total,
'relative' => $relative,
self::USER_FIELD_QUOTA => $quota,
];
}
} catch (NotFoundException $ex) {
$data = [
self::USER_FIELD_QUOTA => $quota !== false ? $quota : 'none',
self::USER_FIELD_QUOTA => $quota >= 0 ? $quota : 'none',
'used' => 0
];
} catch (\Exception $e) {
Expand All @@ -290,8 +320,6 @@ protected function fillStorageInfo(string $userId): array {
'exception' => $e,
]
);
/* In case the Exception left things in a bad state */
\OC_Util::tearDownFS();
return [];
}
return $data;
Expand Down
1 change: 1 addition & 0 deletions apps/provisioning_api/lib/Controller/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public function __construct(
IUserSession $userSession,
IAccountManager $accountManager,
IFactory $l10nFactory,
IRootFolder $rootFolder,
private IURLGenerator $urlGenerator,
private LoggerInterface $logger,
private NewUserMailHelper $newUserMailHelper,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class GroupsControllerTest extends \Test\TestCase {
/** @var GroupsController|\PHPUnit\Framework\MockObject\MockObject */
protected $api;

private IRootFolder $rootFolder;


protected function setUp(): void {
parent::setUp();
Expand All @@ -78,6 +80,7 @@ protected function setUp(): void {
$this->accountManager = $this->createMock(IAccountManager::class);
$this->l10nFactory = $this->createMock(IFactory::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->rootFolder = $this->createMock(IRootFolder::class);

$this->subAdminManager = $this->createMock(SubAdmin::class);

Expand All @@ -95,6 +98,7 @@ protected function setUp(): void {
$this->userSession,
$this->accountManager,
$this->l10nFactory,
$this->rootFolder,
$this->logger
])
->setMethods(['fillStorageInfo'])
Expand Down
12 changes: 9 additions & 3 deletions apps/provisioning_api/tests/Controller/UsersControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class UsersControllerTest extends TestCase {
private $knownUserService;
/** @var IEventDispatcher|MockObject */
private $eventDispatcher;
private IRootFolder $rootFolder;
/** @var IPhoneNumberUtil */
private $phoneNumberUtil;

Expand All @@ -128,6 +129,7 @@ protected function setUp(): void {
$this->knownUserService = $this->createMock(KnownUserService::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->phoneNumberUtil = new PhoneNumberUtil();
$this->rootFolder = $this->createMock(IRootFolder::class);

$l10n = $this->createMock(IL10N::class);
$l10n->method('t')->willReturnCallback(fn (string $txt, array $replacement = []) => sprintf($txt, ...$replacement));
Expand All @@ -143,6 +145,7 @@ protected function setUp(): void {
$this->userSession,
$this->accountManager,
$this->l10nFactory,
$this->rootFolder,
$this->urlGenerator,
$this->logger,
$this->newUserMailHelper,
Expand Down Expand Up @@ -536,6 +539,7 @@ public function testAddUserSuccessfulWithDisplayName() {
$this->userSession,
$this->accountManager,
$this->l10nFactory,
$this->rootFolder,
$this->urlGenerator,
$this->logger,
$this->newUserMailHelper,
Expand Down Expand Up @@ -1158,7 +1162,7 @@ public function testGetUserDataAsAdmin() {
$this->api
->expects($this->once())
->method('fillStorageInfo')
->with('UID')
->with($targetUser)
->willReturn(['DummyValue']);

$backend = $this->createMock(UserInterface::class);
Expand Down Expand Up @@ -1288,7 +1292,7 @@ public function testGetUserDataAsSubAdminAndUserIsAccessible() {
$this->api
->expects($this->once())
->method('fillStorageInfo')
->with('UID')
->with($targetUser)
->willReturn(['DummyValue']);

$backend = $this->createMock(UserInterface::class);
Expand Down Expand Up @@ -1465,7 +1469,7 @@ public function testGetUserDataAsSubAdminSelfLookup() {
$this->api
->expects($this->once())
->method('fillStorageInfo')
->with('UID')
->with($targetUser)
->willReturn(['DummyValue']);

$backend = $this->createMock(UserInterface::class);
Expand Down Expand Up @@ -3819,6 +3823,7 @@ public function testGetCurrentUserLoggedIn() {
$this->userSession,
$this->accountManager,
$this->l10nFactory,
$this->rootFolder,
$this->urlGenerator,
$this->logger,
$this->newUserMailHelper,
Expand Down Expand Up @@ -3906,6 +3911,7 @@ public function testGetUser() {
$this->userSession,
$this->accountManager,
$this->l10nFactory,
$this->rootFolder,
$this->urlGenerator,
$this->logger,
$this->newUserMailHelper,
Expand Down