aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Appelman <robin@icewind.nl>2025-01-16 17:03:57 +0100
committerGitHub <noreply@github.com>2025-01-16 17:03:57 +0100
commit8b46ec2abbe3280dd8c9e50152a5b4be382098ca (patch)
treec80a6da1f6d6d038a20981592fd83ef7eeb92efb
parent8998e77c9d35d1c6183ac5d47f8e1ee9cdefa77f (diff)
parent118ff883b01ca3714f58bbb904d16137a1efaaf5 (diff)
downloadnextcloud-server-8b46ec2abbe3280dd8c9e50152a5b4be382098ca.tar.gz
nextcloud-server-8b46ec2abbe3280dd8c9e50152a5b4be382098ca.zip
Merge pull request #49476 from nextcloud/ocs-user-info-quota-optimize
perf: use more optimized way to get user storage info in ocs user info
-rw-r--r--apps/provisioning_api/lib/Controller/AUserDataOCSController.php81
-rw-r--r--apps/provisioning_api/lib/Controller/GroupsController.php5
-rw-r--r--apps/provisioning_api/lib/Controller/UsersController.php5
-rw-r--r--apps/provisioning_api/tests/Controller/GroupsControllerTest.php5
-rw-r--r--apps/provisioning_api/tests/Controller/UsersControllerTest.php13
5 files changed, 78 insertions, 31 deletions
diff --git a/apps/provisioning_api/lib/Controller/AUserDataOCSController.php b/apps/provisioning_api/lib/Controller/AUserDataOCSController.php
index 4cded6c1137..4db3528f8d3 100644
--- a/apps/provisioning_api/lib/Controller/AUserDataOCSController.php
+++ b/apps/provisioning_api/lib/Controller/AUserDataOCSController.php
@@ -11,7 +11,6 @@ namespace OCA\Provisioning_API\Controller;
use OC\Group\Manager as GroupManager;
use OC\User\Backend;
use OC\User\NoUserException;
-use OC_Helper;
use OCA\Provisioning_API\ResponseDefinitions;
use OCP\Accounts\IAccountManager;
use OCP\Accounts\PropertyDoesNotExistException;
@@ -19,6 +18,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\Group\ISubAdmin;
use OCP\IConfig;
@@ -29,6 +30,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
@@ -56,6 +58,7 @@ abstract class AUserDataOCSController extends OCSController {
protected IAccountManager $accountManager,
protected ISubAdmin $subAdminManager,
protected IFactory $l10nFactory,
+ protected IRootFolder $rootFolder,
) {
parent::__construct($appName, $request);
}
@@ -121,7 +124,7 @@ abstract class AUserDataOCSController extends OCSController {
$data['lastLogin'] = $targetUserObject->getLastLogin() * 1000;
$data['backend'] = $targetUserObject->getBackendClassName();
$data['subadmin'] = $this->getUserSubAdminGroupsData($targetUserObject->getUID());
- $data[self::USER_FIELD_QUOTA] = $this->fillStorageInfo($targetUserObject->getUID());
+ $data[self::USER_FIELD_QUOTA] = $this->fillStorageInfo($targetUserObject);
$managers = $this->getManagers($targetUserObject);
$data[self::USER_FIELD_MANAGER] = empty($managers) ? '' : $managers[0];
@@ -245,34 +248,62 @@ abstract class AUserDataOCSController 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) {
@@ -284,8 +315,6 @@ abstract class AUserDataOCSController 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/lib/Controller/GroupsController.php b/apps/provisioning_api/lib/Controller/GroupsController.php
index 0ae7faae7e8..3d9fa6b1cfc 100644
--- a/apps/provisioning_api/lib/Controller/GroupsController.php
+++ b/apps/provisioning_api/lib/Controller/GroupsController.php
@@ -21,6 +21,7 @@ use OCP\AppFramework\OCS\OCSException;
use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;
+use OCP\Files\IRootFolder;
use OCP\Group\ISubAdmin;
use OCP\IConfig;
use OCP\IGroup;
@@ -48,6 +49,7 @@ class GroupsController extends AUserDataOCSController {
IAccountManager $accountManager,
ISubAdmin $subAdminManager,
IFactory $l10nFactory,
+ IRootFolder $rootFolder,
private LoggerInterface $logger,
) {
parent::__construct($appName,
@@ -58,7 +60,8 @@ class GroupsController extends AUserDataOCSController {
$userSession,
$accountManager,
$subAdminManager,
- $l10nFactory
+ $l10nFactory,
+ $rootFolder,
);
}
diff --git a/apps/provisioning_api/lib/Controller/UsersController.php b/apps/provisioning_api/lib/Controller/UsersController.php
index 7f9ea26b79c..301ee47c0a6 100644
--- a/apps/provisioning_api/lib/Controller/UsersController.php
+++ b/apps/provisioning_api/lib/Controller/UsersController.php
@@ -31,6 +31,7 @@ use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;
use OCP\EventDispatcher\IEventDispatcher;
+use OCP\Files\IRootFolder;
use OCP\Group\ISubAdmin;
use OCP\HintException;
use OCP\IConfig;
@@ -67,6 +68,7 @@ class UsersController extends AUserDataOCSController {
IAccountManager $accountManager,
ISubAdmin $subAdminManager,
IFactory $l10nFactory,
+ IRootFolder $rootFolder,
private IURLGenerator $urlGenerator,
private LoggerInterface $logger,
private NewUserMailHelper $newUserMailHelper,
@@ -85,7 +87,8 @@ class UsersController extends AUserDataOCSController {
$userSession,
$accountManager,
$subAdminManager,
- $l10nFactory
+ $l10nFactory,
+ $rootFolder,
);
$this->l10n = $l10nFactory->get($appName);
diff --git a/apps/provisioning_api/tests/Controller/GroupsControllerTest.php b/apps/provisioning_api/tests/Controller/GroupsControllerTest.php
index b1a708b9aa2..29b098429e8 100644
--- a/apps/provisioning_api/tests/Controller/GroupsControllerTest.php
+++ b/apps/provisioning_api/tests/Controller/GroupsControllerTest.php
@@ -12,6 +12,7 @@ use OC\User\NoUserException;
use OCA\Provisioning_API\Controller\GroupsController;
use OCP\Accounts\IAccountManager;
use OCP\AppFramework\OCS\OCSException;
+use OCP\Files\IRootFolder;
use OCP\Group\ISubAdmin;
use OCP\IConfig;
use OCP\IGroup;
@@ -46,6 +47,8 @@ class GroupsControllerTest extends \Test\TestCase {
/** @var GroupsController|\PHPUnit\Framework\MockObject\MockObject */
protected $api;
+ private IRootFolder $rootFolder;
+
protected function setUp(): void {
parent::setUp();
@@ -59,6 +62,7 @@ class GroupsControllerTest extends \Test\TestCase {
$this->subAdminManager = $this->createMock(ISubAdmin::class);
$this->l10nFactory = $this->createMock(IFactory::class);
$this->logger = $this->createMock(LoggerInterface::class);
+ $this->rootFolder = $this->createMock(IRootFolder::class);
$this->groupManager
->method('getSubAdmin')
@@ -75,6 +79,7 @@ class GroupsControllerTest extends \Test\TestCase {
$this->accountManager,
$this->subAdminManager,
$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 52dd82069f9..439ee0be250 100644
--- a/apps/provisioning_api/tests/Controller/UsersControllerTest.php
+++ b/apps/provisioning_api/tests/Controller/UsersControllerTest.php
@@ -23,6 +23,7 @@ use OCP\Accounts\IAccountPropertyCollection;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSException;
use OCP\EventDispatcher\IEventDispatcher;
+use OCP\Files\IRootFolder;
use OCP\Group\ISubAdmin;
use OCP\IConfig;
use OCP\IGroup;
@@ -76,6 +77,7 @@ class UsersControllerTest extends TestCase {
private $knownUserService;
/** @var IEventDispatcher|MockObject */
private $eventDispatcher;
+ private IRootFolder $rootFolder;
/** @var IPhoneNumberUtil */
private $phoneNumberUtil;
@@ -98,6 +100,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);
$l10n = $this->createMock(IL10N::class);
$l10n->method('t')->willReturnCallback(fn (string $txt, array $replacement = []) => sprintf($txt, ...$replacement));
@@ -114,6 +117,7 @@ class UsersControllerTest extends TestCase {
$this->accountManager,
$this->subAdminManager,
$this->l10nFactory,
+ $this->rootFolder,
$this->urlGenerator,
$this->logger,
$this->newUserMailHelper,
@@ -508,6 +512,7 @@ class UsersControllerTest extends TestCase {
$this->accountManager,
$this->subAdminManager,
$this->l10nFactory,
+ $this->rootFolder,
$this->urlGenerator,
$this->logger,
$this->newUserMailHelper,
@@ -1127,7 +1132,7 @@ class UsersControllerTest extends TestCase {
$this->api
->expects($this->once())
->method('fillStorageInfo')
- ->with('UID')
+ ->with($targetUser)
->willReturn(['DummyValue']);
$backend = $this->createMock(UserInterface::class);
@@ -1260,7 +1265,7 @@ class UsersControllerTest extends TestCase {
$this->api
->expects($this->once())
->method('fillStorageInfo')
- ->with('UID')
+ ->with($targetUser)
->willReturn(['DummyValue']);
$backend = $this->createMock(UserInterface::class);
@@ -1441,7 +1446,7 @@ class UsersControllerTest extends TestCase {
$this->api
->expects($this->once())
->method('fillStorageInfo')
- ->with('UID')
+ ->with($targetUser)
->willReturn(['DummyValue']);
$backend = $this->createMock(UserInterface::class);
@@ -3806,6 +3811,7 @@ class UsersControllerTest extends TestCase {
$this->accountManager,
$this->subAdminManager,
$this->l10nFactory,
+ $this->rootFolder,
$this->urlGenerator,
$this->logger,
$this->newUserMailHelper,
@@ -3896,6 +3902,7 @@ class UsersControllerTest extends TestCase {
$this->accountManager,
$this->subAdminManager,
$this->l10nFactory,
+ $this->rootFolder,
$this->urlGenerator,
$this->logger,
$this->newUserMailHelper,