chore: replace leagcy OC_Helper calls with OCP\Util

- Replace legacy calls with OCP\Util
- Add missing deprecation notices
- Inline implementation in OCP\Util and call it from OC_Helper

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
pull/52801/head
Ferdinand Thiessen 2025-05-14 00:38:32 +07:00
parent 0f03a892b9
commit 1ae3fa4003
No known key found for this signature in database
GPG Key ID: 45FAE7268762B400
9 changed files with 72 additions and 71 deletions

@ -96,7 +96,7 @@ class CleanUp extends Command {
$node = $this->rootFolder->get($path); $node = $this->rootFolder->get($path);
if ($verbose) { if ($verbose) {
$output->writeln('Deleting <info>' . \OC_Helper::humanFileSize($node->getSize()) . "</info> in trash for <info>$uid</info>."); $output->writeln('Deleting <info>' . \OCP\Util::humanFileSize($node->getSize()) . "</info> in trash for <info>$uid</info>.");
} }
$node->delete(); $node->delete();
if ($this->rootFolder->nodeExists($path)) { if ($this->rootFolder->nodeExists($path)) {

@ -45,7 +45,7 @@ class Size extends Base {
$size = $input->getArgument('size'); $size = $input->getArgument('size');
if ($size) { if ($size) {
$parsedSize = \OC_Helper::computerFileSize($size); $parsedSize = \OCP\Util::computerFileSize($size);
if ($parsedSize === false) { if ($parsedSize === false) {
$output->writeln('<error>Failed to parse input size</error>'); $output->writeln('<error>Failed to parse input size</error>');
return -1; return -1;
@ -70,7 +70,7 @@ class Size extends Base {
if ($globalSize < 0) { if ($globalSize < 0) {
$globalHumanSize = 'default (50% of available space)'; $globalHumanSize = 'default (50% of available space)';
} else { } else {
$globalHumanSize = \OC_Helper::humanFileSize($globalSize); $globalHumanSize = \OCP\Util::humanFileSize($globalSize);
} }
if ($user) { if ($user) {
@ -79,7 +79,7 @@ class Size extends Base {
if ($userSize < 0) { if ($userSize < 0) {
$userHumanSize = ($globalSize < 0) ? $globalHumanSize : "default($globalHumanSize)"; $userHumanSize = ($globalSize < 0) ? $globalHumanSize : "default($globalHumanSize)";
} else { } else {
$userHumanSize = \OC_Helper::humanFileSize($userSize); $userHumanSize = \OCP\Util::humanFileSize($userSize);
} }
if ($input->getOption('output') == self::OUTPUT_FORMAT_PLAIN) { if ($input->getOption('output') == self::OUTPUT_FORMAT_PLAIN) {
@ -106,7 +106,7 @@ class Size extends Base {
if (count($userValues)) { if (count($userValues)) {
$output->writeln('Per-user sizes:'); $output->writeln('Per-user sizes:');
$this->writeArrayInOutputFormat($input, $output, array_map(function ($size) { $this->writeArrayInOutputFormat($input, $output, array_map(function ($size) {
return \OC_Helper::humanFileSize($size); return \OCP\Util::humanFileSize($size);
}, $userValues)); }, $userValues));
} else { } else {
$output->writeln('No per-user sizes configured'); $output->writeln('No per-user sizes configured');

@ -71,7 +71,7 @@ class PersonalInfo implements ISettings {
if ($storageInfo['quota'] === FileInfo::SPACE_UNLIMITED) { if ($storageInfo['quota'] === FileInfo::SPACE_UNLIMITED) {
$totalSpace = $this->l->t('Unlimited'); $totalSpace = $this->l->t('Unlimited');
} else { } else {
$totalSpace = \OC_Helper::humanFileSize($storageInfo['total']); $totalSpace = \OCP\Util::humanFileSize($storageInfo['total']);
} }
$messageParameters = $this->getMessageParameters($account); $messageParameters = $this->getMessageParameters($account);
@ -88,7 +88,7 @@ class PersonalInfo implements ISettings {
'groups' => $this->getGroups($user), 'groups' => $this->getGroups($user),
'quota' => $storageInfo['quota'], 'quota' => $storageInfo['quota'],
'totalSpace' => $totalSpace, 'totalSpace' => $totalSpace,
'usage' => \OC_Helper::humanFileSize($storageInfo['used']), 'usage' => \OCP\Util::humanFileSize($storageInfo['used']),
'usageRelative' => round($storageInfo['relative']), 'usageRelative' => round($storageInfo['relative']),
'displayName' => $this->getProperty($account, IAccountManager::PROPERTY_DISPLAYNAME), 'displayName' => $this->getProperty($account, IAccountManager::PROPERTY_DISPLAYNAME),
'emailMap' => $this->getEmailMap($account), 'emailMap' => $this->getEmailMap($account),

@ -515,7 +515,7 @@ class User {
* fetch all the user's attributes in one call and use the fetched values in this function. * fetch all the user's attributes in one call and use the fetched values in this function.
* The expected value for that parameter is a string describing the quota for the user. Valid * The expected value for that parameter is a string describing the quota for the user. Valid
* values are 'none' (unlimited), 'default' (the Nextcloud's default quota), '1234' (quota in * values are 'none' (unlimited), 'default' (the Nextcloud's default quota), '1234' (quota in
* bytes), '1234 MB' (quota in MB - check the \OC_Helper::computerFileSize method for more info) * bytes), '1234 MB' (quota in MB - check the \OCP\Util::computerFileSize method for more info)
* *
* fetches the quota from LDAP and stores it as Nextcloud user value * fetches the quota from LDAP and stores it as Nextcloud user value
* @param ?string $valueFromLDAP the quota attribute's value can be passed, * @param ?string $valueFromLDAP the quota attribute's value can be passed,
@ -563,7 +563,7 @@ class User {
} }
private function verifyQuotaValue(string $quotaValue): bool { private function verifyQuotaValue(string $quotaValue): bool {
return $quotaValue === 'none' || $quotaValue === 'default' || \OC_Helper::computerFileSize($quotaValue) !== false; return $quotaValue === 'none' || $quotaValue === 'default' || \OCP\Util::computerFileSize($quotaValue) !== false;
} }
/** /**

@ -11,7 +11,6 @@ use InvalidArgumentException;
use OC\Accounts\AccountManager; use OC\Accounts\AccountManager;
use OC\Avatar\AvatarManager; use OC\Avatar\AvatarManager;
use OC\Hooks\Emitter; use OC\Hooks\Emitter;
use OC_Helper;
use OCP\Accounts\IAccountManager; use OCP\Accounts\IAccountManager;
use OCP\Comments\ICommentsManager; use OCP\Comments\ICommentsManager;
use OCP\EventDispatcher\IEventDispatcher; use OCP\EventDispatcher\IEventDispatcher;
@ -570,11 +569,11 @@ class User implements IUser {
public function setQuota($quota) { public function setQuota($quota) {
$oldQuota = $this->config->getUserValue($this->uid, 'files', 'quota', ''); $oldQuota = $this->config->getUserValue($this->uid, 'files', 'quota', '');
if ($quota !== 'none' and $quota !== 'default') { if ($quota !== 'none' and $quota !== 'default') {
$bytesQuota = OC_Helper::computerFileSize($quota); $bytesQuota = \OCP\Util::computerFileSize($quota);
if ($bytesQuota === false) { if ($bytesQuota === false) {
throw new InvalidArgumentException('Failed to set quota to invalid value ' . $quota); throw new InvalidArgumentException('Failed to set quota to invalid value ' . $quota);
} }
$quota = OC_Helper::humanFileSize($bytesQuota); $quota = \OCP\Util::humanFileSize($bytesQuota);
} }
if ($quota !== $oldQuota) { if ($quota !== $oldQuota) {
$this->config->setUserValue($this->uid, 'files', 'quota', $quota); $this->config->setUserValue($this->uid, 'files', 'quota', $quota);

@ -39,75 +39,26 @@ class OC_Helper {
* Make a human file size * Make a human file size
* @param int|float $bytes file size in bytes * @param int|float $bytes file size in bytes
* @return string a human readable file size * @return string a human readable file size
* @deprecated 4.0.0 replaced with \OCP\Util::humanFileSize
* *
* Makes 2048 to 2 kB. * Makes 2048 to 2 kB.
*/ */
public static function humanFileSize(int|float $bytes): string { public static function humanFileSize(int|float $bytes): string {
if ($bytes < 0) { return \OCP\Util::humanFileSize($bytes);
return '?';
}
if ($bytes < 1024) {
return "$bytes B";
}
$bytes = round($bytes / 1024, 0);
if ($bytes < 1024) {
return "$bytes KB";
}
$bytes = round($bytes / 1024, 1);
if ($bytes < 1024) {
return "$bytes MB";
}
$bytes = round($bytes / 1024, 1);
if ($bytes < 1024) {
return "$bytes GB";
}
$bytes = round($bytes / 1024, 1);
if ($bytes < 1024) {
return "$bytes TB";
}
$bytes = round($bytes / 1024, 1);
return "$bytes PB";
} }
/** /**
* Make a computer file size * Make a computer file size
* @param string $str file size in human readable format * @param string $str file size in human readable format
* @return false|int|float a file size in bytes * @return false|int|float a file size in bytes
* @deprecated 4.0.0 Use \OCP\Util::computerFileSize
* *
* Makes 2kB to 2048. * Makes 2kB to 2048.
* *
* Inspired by: https://www.php.net/manual/en/function.filesize.php#92418 * Inspired by: https://www.php.net/manual/en/function.filesize.php#92418
*/ */
public static function computerFileSize(string $str): false|int|float { public static function computerFileSize(string $str): false|int|float {
$str = strtolower($str); return \OCP\Util::computerFileSize($str);
if (is_numeric($str)) {
return Util::numericToNumber($str);
}
$bytes_array = [
'b' => 1,
'k' => 1024,
'kb' => 1024,
'mb' => 1024 * 1024,
'm' => 1024 * 1024,
'gb' => 1024 * 1024 * 1024,
'g' => 1024 * 1024 * 1024,
'tb' => 1024 * 1024 * 1024 * 1024,
't' => 1024 * 1024 * 1024 * 1024,
'pb' => 1024 * 1024 * 1024 * 1024 * 1024,
'p' => 1024 * 1024 * 1024 * 1024 * 1024,
];
$bytes = (float)$str;
if (preg_match('#([kmgtp]?b?)$#si', $str, $matches) && isset($bytes_array[$matches[1]])) {
$bytes *= $bytes_array[$matches[1]];
} else {
return false;
}
return Util::numericToNumber(round($bytes));
} }
/** /**

@ -107,7 +107,7 @@ class OC_Util {
if ($userQuota === 'none') { if ($userQuota === 'none') {
return \OCP\Files\FileInfo::SPACE_UNLIMITED; return \OCP\Files\FileInfo::SPACE_UNLIMITED;
} }
return OC_Helper::computerFileSize($userQuota); return \OCP\Util::computerFileSize($userQuota);
} }
/** /**

@ -77,7 +77,7 @@ class Template extends \OC_Template implements ITemplate {
} }
/** /**
* Make OC_Helper::humanFileSize available as a simple function * Make \OCP\Util::humanFileSize available as a simple function
* Example: 2048 to 2 kB. * Example: 2048 to 2 kB.
* *
* @param int $bytes in bytes * @param int $bytes in bytes

@ -332,19 +332,70 @@ class Util {
* @since 4.0.0 * @since 4.0.0
*/ */
public static function humanFileSize(int|float $bytes): string { public static function humanFileSize(int|float $bytes): string {
return \OC_Helper::humanFileSize($bytes); if ($bytes < 0) {
return '?';
}
if ($bytes < 1024) {
return "$bytes B";
}
$bytes = round($bytes / 1024, 0);
if ($bytes < 1024) {
return "$bytes KB";
}
$bytes = round($bytes / 1024, 1);
if ($bytes < 1024) {
return "$bytes MB";
}
$bytes = round($bytes / 1024, 1);
if ($bytes < 1024) {
return "$bytes GB";
}
$bytes = round($bytes / 1024, 1);
if ($bytes < 1024) {
return "$bytes TB";
}
$bytes = round($bytes / 1024, 1);
return "$bytes PB";
} }
/** /**
* Make a computer file size (2 kB to 2048) * Make a computer file size (2 kB to 2048)
* Inspired by: https://www.php.net/manual/en/function.filesize.php#92418
*
* @param string $str file size in a fancy format * @param string $str file size in a fancy format
* @return false|int|float a file size in bytes * @return false|int|float a file size in bytes
*
* Inspired by: https://www.php.net/manual/en/function.filesize.php#92418
* @since 4.0.0 * @since 4.0.0
*/ */
public static function computerFileSize(string $str): false|int|float { public static function computerFileSize(string $str): false|int|float {
return \OC_Helper::computerFileSize($str); $str = strtolower($str);
if (is_numeric($str)) {
return Util::numericToNumber($str);
}
$bytes_array = [
'b' => 1,
'k' => 1024,
'kb' => 1024,
'mb' => 1024 * 1024,
'm' => 1024 * 1024,
'gb' => 1024 * 1024 * 1024,
'g' => 1024 * 1024 * 1024,
'tb' => 1024 * 1024 * 1024 * 1024,
't' => 1024 * 1024 * 1024 * 1024,
'pb' => 1024 * 1024 * 1024 * 1024 * 1024,
'p' => 1024 * 1024 * 1024 * 1024 * 1024,
];
$bytes = (float)$str;
if (preg_match('#([kmgtp]?b?)$#si', $str, $matches) && isset($bytes_array[$matches[1]])) {
$bytes *= $bytes_array[$matches[1]];
} else {
return false;
}
return Util::numericToNumber(round($bytes));
} }
/** /**