Skip to content

Commit fb4a06f

Browse files
Merge pull request #53045 from nextcloud/feat/user-get-quota-bytes
2 parents dc8799f + e143921 commit fb4a06f

File tree

7 files changed

+34
-8
lines changed

7 files changed

+34
-8
lines changed

lib/private/Files/SetupManager.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use OC\Share20\ShareDisableChecker;
2424
use OC_App;
2525
use OC_Hook;
26-
use OC_Util;
2726
use OCA\Files_External\Config\ExternalMountPoint;
2827
use OCA\Files_Sharing\External\Mount;
2928
use OCA\Files_Sharing\ISharedMountPoint;
@@ -157,7 +156,7 @@ function ($mountPoint, IStorage $storage, IMountPoint $mount) use ($reSharingEna
157156
if ($mount instanceof HomeMountPoint) {
158157
$user = $mount->getUser();
159158
return new Quota(['storage' => $storage, 'quotaCallback' => function () use ($user) {
160-
return OC_Util::getUserQuota($user);
159+
return $user->getQuotaBytes();
161160
}, 'root' => 'files', 'include_external_storage' => $quotaIncludeExternal]);
162161
}
163162

lib/private/User/LazyUser.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ public function getQuota() {
160160
return $this->getUser()->getQuota();
161161
}
162162

163+
public function getQuotaBytes(): int|float {
164+
return $this->getUser()->getQuotaBytes();
165+
}
166+
163167
public function setQuota($quota) {
164168
$this->getUser()->setQuota($quota);
165169
}

lib/private/User/User.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,19 @@ public function getQuota() {
558558
return $quota;
559559
}
560560

561+
public function getQuotaBytes(): int|float {
562+
$quota = $this->getQuota();
563+
if ($quota === 'none') {
564+
return \OCP\Files\FileInfo::SPACE_UNLIMITED;
565+
}
566+
567+
$bytes = \OCP\Util::computerFileSize($quota);
568+
if ($bytes === false) {
569+
return \OCP\Files\FileInfo::SPACE_UNKNOWN;
570+
}
571+
return $bytes;
572+
}
573+
561574
/**
562575
* set the users' quota
563576
*

lib/private/legacy/OC_Helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ public static function getStorageInfo($path, $rootInfo = null, $includeMountPoin
272272
} else {
273273
$user = \OC::$server->getUserSession()->getUser();
274274
}
275-
$quota = OC_Util::getUserQuota($user);
275+
$quota = $user?->getQuotaBytes() ?? \OCP\Files\FileInfo::SPACE_UNKNOWN;
276276
if ($quota !== \OCP\Files\FileInfo::SPACE_UNLIMITED) {
277277
// always get free space / total space from root + mount points
278278
return self::getGlobalStorageInfo($quota, $user, $mount);

lib/private/legacy/OC_Util.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public static function isDefaultExpireDateEnforced() {
9898
*
9999
* @param IUser|null $user
100100
* @return int|\OCP\Files\FileInfo::SPACE_UNLIMITED|false|float Quota bytes
101-
* @deprecated 9.0.0 - Use \OCP\IUser::getQuota
101+
* @deprecated 9.0.0 - Use \OCP\IUser::getQuota or \OCP\IUser::getQuotaBytes
102102
*/
103103
public static function getUserQuota(?IUser $user) {
104104
if (is_null($user)) {

lib/public/IUser.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,15 @@ public function setPrimaryEMailAddress(string $mailAddress): void;
280280
*/
281281
public function getQuota();
282282

283+
/**
284+
* Get the users' quota in machine readable form. If a specific quota is set
285+
* for the user, then the quota is returned in bytes. Otherwise the default value is returned.
286+
* If a default setting was not set, it is return as `\OCP\Files\FileInfo::SPACE_UNLIMITED`, i.e. quota is not limited.
287+
*
288+
* @since 32.0.0
289+
*/
290+
public function getQuotaBytes(): int|float;
291+
283292
/**
284293
* set the users' quota
285294
*

tests/lib/User/UserTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use OC\User\User;
1515
use OCP\Comments\ICommentsManager;
1616
use OCP\EventDispatcher\IEventDispatcher;
17+
use OCP\Files\FileInfo;
1718
use OCP\Files\Storage\IStorageFactory;
1819
use OCP\IConfig;
1920
use OCP\IURLGenerator;
@@ -834,8 +835,8 @@ public function testGetDefaultUnlimitedQuota(): void {
834835
$config->method('getAppValue')
835836
->will($this->returnValueMap($appValueMap));
836837

837-
$quota = $user->getQuota();
838-
$this->assertEquals('none', $quota);
838+
$this->assertEquals('none', $user->getQuota());
839+
$this->assertEquals(FileInfo::SPACE_UNLIMITED, $user->getQuotaBytes());
839840
}
840841

841842
public function testGetDefaultUnlimitedQuotaForbidden(): void {
@@ -868,8 +869,8 @@ public function testGetDefaultUnlimitedQuotaForbidden(): void {
868869
$config->method('getAppValue')
869870
->will($this->returnValueMap($appValueMap));
870871

871-
$quota = $user->getQuota();
872-
$this->assertEquals('1 GB', $quota);
872+
$this->assertEquals('1 GB', $user->getQuota());
873+
$this->assertEquals(1024 * 1024 * 1024, $user->getQuotaBytes());
873874
}
874875

875876
public function testSetQuotaAddressNoChange(): void {

0 commit comments

Comments
 (0)