diff options
author | Robin Appelman <robin@icewind.nl> | 2024-11-25 17:30:32 +0100 |
---|---|---|
committer | backportbot[bot] <backportbot[bot]@users.noreply.github.com> | 2025-01-16 16:10:54 +0000 |
commit | 7c6b1610223b57575b27eccd5a5e8900933ada45 (patch) | |
tree | a2b706794fa8b7f132ed4c67550b98b058fc96ed | |
parent | eac3c5fd629c3dfab7e07cb97df96dfa75f06e92 (diff) | |
download | nextcloud-server-backport/49476/stable28.tar.gz nextcloud-server-backport/49476/stable28.zip |
perf: use more optimized way to get user storage info in ocs user info when possiblebackport/49476/stable28
Signed-off-by: Robin Appelman <robin@icewind.nl>
[skip ci]
3 files changed, 62 insertions, 28 deletions
diff --git a/apps/provisioning_api/lib/Controller/AUserData.php b/apps/provisioning_api/lib/Controller/AUserData.php index 77d7f31c6da..8d11ea21d2a 100644 --- a/apps/provisioning_api/lib/Controller/AUserData.php +++ b/apps/provisioning_api/lib/Controller/AUserData.php @@ -35,7 +35,6 @@ namespace OCA\Provisioning_API\Controller; 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; @@ -43,6 +42,8 @@ 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; @@ -52,6 +53,7 @@ use OCP\IUserSession; use OCP\L10N\IFactory; use OCP\User\Backend\ISetDisplayNameBackend; use OCP\User\Backend\ISetPasswordBackend; +use OCP\Util; /** * @psalm-import-type Provisioning_APIUserDetails from ResponseDefinitions @@ -251,34 +253,62 @@ abstract class AUserData extends OCSController { } /** - * @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) { @@ -290,8 +320,6 @@ abstract class AUserData extends OCSController { 'exception' => $e, ] ); - /* In case the Exception left things in a bad state */ - \OC_Util::tearDownFS(); return []; } return $data; diff --git a/apps/provisioning_api/tests/Controller/GroupsControllerTest.php b/apps/provisioning_api/tests/Controller/GroupsControllerTest.php index a60d01f68c3..726e3093306 100644 --- a/apps/provisioning_api/tests/Controller/GroupsControllerTest.php +++ b/apps/provisioning_api/tests/Controller/GroupsControllerTest.php @@ -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(); @@ -78,6 +80,7 @@ class GroupsControllerTest extends \Test\TestCase { $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); @@ -95,6 +98,7 @@ class GroupsControllerTest extends \Test\TestCase { $this->userSession, $this->accountManager, $this->l10nFactory, + $this->rootFolder, $this->logger ]) ->setMethods(['fillStorageInfo']) diff --git a/apps/provisioning_api/tests/Controller/UsersControllerTest.php b/apps/provisioning_api/tests/Controller/UsersControllerTest.php index cfb7f2d9543..1d9abc354af 100644 --- a/apps/provisioning_api/tests/Controller/UsersControllerTest.php +++ b/apps/provisioning_api/tests/Controller/UsersControllerTest.php @@ -107,6 +107,7 @@ class UsersControllerTest extends TestCase { private $knownUserService; /** @var IEventDispatcher|MockObject */ private $eventDispatcher; + private IRootFolder $rootFolder; /** @var IPhoneNumberUtil */ private $phoneNumberUtil; @@ -128,6 +129,7 @@ class UsersControllerTest extends TestCase { $this->knownUserService = $this->createMock(KnownUserService::class); $this->eventDispatcher = $this->createMock(IEventDispatcher::class); $this->phoneNumberUtil = new PhoneNumberUtil(); + $this->rootFolder = $this->createMock(IRootFolder::class); $this->api = $this->getMockBuilder(UsersController::class) ->setConstructorArgs([ @@ -1157,7 +1159,7 @@ class UsersControllerTest extends TestCase { $this->api ->expects($this->once()) ->method('fillStorageInfo') - ->with('UID') + ->with($targetUser) ->willReturn(['DummyValue']); $backend = $this->createMock(UserInterface::class); @@ -1287,7 +1289,7 @@ class UsersControllerTest extends TestCase { $this->api ->expects($this->once()) ->method('fillStorageInfo') - ->with('UID') + ->with($targetUser) ->willReturn(['DummyValue']); $backend = $this->createMock(UserInterface::class); @@ -1464,7 +1466,7 @@ class UsersControllerTest extends TestCase { $this->api ->expects($this->once()) ->method('fillStorageInfo') - ->with('UID') + ->with($targetUser) ->willReturn(['DummyValue']); $backend = $this->createMock(UserInterface::class); |