mirror of
https://github.com/nextcloud/server.git
synced 2026-06-08 16:26:59 -04:00
feat(IUser): add getQuotaBytes method to get machine readable quota
Proper replacement for deprecated `OC_Util::getUserQuota`. Also we still use this in some cases we can now replace, moreover it just makes sense to have a machine readable format in the API instead of only the human readable format which is less precise. Alings also with `getQuota` of the quota storage, which already returned the machine readable format. Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
This commit is contained in:
parent
a48bc55e2a
commit
e143921896
7 changed files with 34 additions and 8 deletions
|
|
@ -23,7 +23,6 @@ use OC\Share\Share;
|
|||
use OC\Share20\ShareDisableChecker;
|
||||
use OC_App;
|
||||
use OC_Hook;
|
||||
use OC_Util;
|
||||
use OCA\Files_External\Config\ExternalMountPoint;
|
||||
use OCA\Files_Sharing\External\Mount;
|
||||
use OCA\Files_Sharing\ISharedMountPoint;
|
||||
|
|
@ -157,7 +156,7 @@ class SetupManager {
|
|||
if ($mount instanceof HomeMountPoint) {
|
||||
$user = $mount->getUser();
|
||||
return new Quota(['storage' => $storage, 'quotaCallback' => function () use ($user) {
|
||||
return OC_Util::getUserQuota($user);
|
||||
return $user->getQuotaBytes();
|
||||
}, 'root' => 'files', 'include_external_storage' => $quotaIncludeExternal]);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -160,6 +160,10 @@ class LazyUser implements IUser {
|
|||
return $this->getUser()->getQuota();
|
||||
}
|
||||
|
||||
public function getQuotaBytes(): int|float {
|
||||
return $this->getUser()->getQuotaBytes();
|
||||
}
|
||||
|
||||
public function setQuota($quota) {
|
||||
$this->getUser()->setQuota($quota);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -558,6 +558,19 @@ class User implements IUser {
|
|||
return $quota;
|
||||
}
|
||||
|
||||
public function getQuotaBytes(): int|float {
|
||||
$quota = $this->getQuota();
|
||||
if ($quota === 'none') {
|
||||
return \OCP\Files\FileInfo::SPACE_UNLIMITED;
|
||||
}
|
||||
|
||||
$bytes = \OCP\Util::computerFileSize($quota);
|
||||
if ($bytes === false) {
|
||||
return \OCP\Files\FileInfo::SPACE_UNKNOWN;
|
||||
}
|
||||
return $bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the users' quota
|
||||
*
|
||||
|
|
|
|||
|
|
@ -272,7 +272,7 @@ class OC_Helper {
|
|||
} else {
|
||||
$user = \OC::$server->getUserSession()->getUser();
|
||||
}
|
||||
$quota = OC_Util::getUserQuota($user);
|
||||
$quota = $user?->getQuotaBytes() ?? \OCP\Files\FileInfo::SPACE_UNKNOWN;
|
||||
if ($quota !== \OCP\Files\FileInfo::SPACE_UNLIMITED) {
|
||||
// always get free space / total space from root + mount points
|
||||
return self::getGlobalStorageInfo($quota, $user, $mount);
|
||||
|
|
|
|||
|
|
@ -98,7 +98,7 @@ class OC_Util {
|
|||
*
|
||||
* @param IUser|null $user
|
||||
* @return int|\OCP\Files\FileInfo::SPACE_UNLIMITED|false|float Quota bytes
|
||||
* @deprecated 9.0.0 - Use \OCP\IUser::getQuota
|
||||
* @deprecated 9.0.0 - Use \OCP\IUser::getQuota or \OCP\IUser::getQuotaBytes
|
||||
*/
|
||||
public static function getUserQuota(?IUser $user) {
|
||||
if (is_null($user)) {
|
||||
|
|
|
|||
|
|
@ -280,6 +280,15 @@ interface IUser {
|
|||
*/
|
||||
public function getQuota();
|
||||
|
||||
/**
|
||||
* Get the users' quota in machine readable form. If a specific quota is set
|
||||
* for the user, then the quota is returned in bytes. Otherwise the default value is returned.
|
||||
* If a default setting was not set, it is return as `\OCP\Files\FileInfo::SPACE_UNLIMITED`, i.e. quota is not limited.
|
||||
*
|
||||
* @since 32.0.0
|
||||
*/
|
||||
public function getQuotaBytes(): int|float;
|
||||
|
||||
/**
|
||||
* set the users' quota
|
||||
*
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ use OC\Hooks\PublicEmitter;
|
|||
use OC\User\User;
|
||||
use OCP\Comments\ICommentsManager;
|
||||
use OCP\EventDispatcher\IEventDispatcher;
|
||||
use OCP\Files\FileInfo;
|
||||
use OCP\Files\Storage\IStorageFactory;
|
||||
use OCP\IConfig;
|
||||
use OCP\IURLGenerator;
|
||||
|
|
@ -834,8 +835,8 @@ class UserTest extends TestCase {
|
|||
$config->method('getAppValue')
|
||||
->will($this->returnValueMap($appValueMap));
|
||||
|
||||
$quota = $user->getQuota();
|
||||
$this->assertEquals('none', $quota);
|
||||
$this->assertEquals('none', $user->getQuota());
|
||||
$this->assertEquals(FileInfo::SPACE_UNLIMITED, $user->getQuotaBytes());
|
||||
}
|
||||
|
||||
public function testGetDefaultUnlimitedQuotaForbidden(): void {
|
||||
|
|
@ -868,8 +869,8 @@ class UserTest extends TestCase {
|
|||
$config->method('getAppValue')
|
||||
->will($this->returnValueMap($appValueMap));
|
||||
|
||||
$quota = $user->getQuota();
|
||||
$this->assertEquals('1 GB', $quota);
|
||||
$this->assertEquals('1 GB', $user->getQuota());
|
||||
$this->assertEquals(1024 * 1024 * 1024, $user->getQuotaBytes());
|
||||
}
|
||||
|
||||
public function testSetQuotaAddressNoChange(): void {
|
||||
|
|
|
|||
Loading…
Reference in a new issue