Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/files_trashbin/lib/Command/CleanUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected function removeDeletedFiles(string $uid, OutputInterface $output, bool
$node = $this->rootFolder->get($path);

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();
if ($this->rootFolder->nodeExists($path)) {
Expand Down
8 changes: 4 additions & 4 deletions apps/files_trashbin/lib/Command/Size.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$size = $input->getArgument('size');

if ($size) {
$parsedSize = \OC_Helper::computerFileSize($size);
$parsedSize = \OCP\Util::computerFileSize($size);
if ($parsedSize === false) {
$output->writeln('<error>Failed to parse input size</error>');
return -1;
Expand All @@ -70,7 +70,7 @@ private function printTrashbinSize(InputInterface $input, OutputInterface $outpu
if ($globalSize < 0) {
$globalHumanSize = 'default (50% of available space)';
} else {
$globalHumanSize = \OC_Helper::humanFileSize($globalSize);
$globalHumanSize = \OCP\Util::humanFileSize($globalSize);
}

if ($user) {
Expand All @@ -79,7 +79,7 @@ private function printTrashbinSize(InputInterface $input, OutputInterface $outpu
if ($userSize < 0) {
$userHumanSize = ($globalSize < 0) ? $globalHumanSize : "default($globalHumanSize)";
} else {
$userHumanSize = \OC_Helper::humanFileSize($userSize);
$userHumanSize = \OCP\Util::humanFileSize($userSize);
}

if ($input->getOption('output') == self::OUTPUT_FORMAT_PLAIN) {
Expand All @@ -106,7 +106,7 @@ private function printTrashbinSize(InputInterface $input, OutputInterface $outpu
if (count($userValues)) {
$output->writeln('Per-user sizes:');
$this->writeArrayInOutputFormat($input, $output, array_map(function ($size) {
return \OC_Helper::humanFileSize($size);
return \OCP\Util::humanFileSize($size);
}, $userValues));
} else {
$output->writeln('No per-user sizes configured');
Expand Down
4 changes: 2 additions & 2 deletions apps/settings/lib/Settings/Personal/PersonalInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function getForm(): TemplateResponse {
if ($storageInfo['quota'] === FileInfo::SPACE_UNLIMITED) {
$totalSpace = $this->l->t('Unlimited');
} else {
$totalSpace = \OC_Helper::humanFileSize($storageInfo['total']);
$totalSpace = \OCP\Util::humanFileSize($storageInfo['total']);
}

$messageParameters = $this->getMessageParameters($account);
Expand All @@ -88,7 +88,7 @@ public function getForm(): TemplateResponse {
'groups' => $this->getGroups($user),
'quota' => $storageInfo['quota'],
'totalSpace' => $totalSpace,
'usage' => \OC_Helper::humanFileSize($storageInfo['used']),
'usage' => \OCP\Util::humanFileSize($storageInfo['used']),
'usageRelative' => round($storageInfo['relative']),
'displayName' => $this->getProperty($account, IAccountManager::PROPERTY_DISPLAYNAME),
'emailMap' => $this->getEmailMap($account),
Expand Down
4 changes: 2 additions & 2 deletions apps/user_ldap/lib/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ public function updateEmail(?string $valueFromLDAP = null): void {
* 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
* 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
* @param ?string $valueFromLDAP the quota attribute's value can be passed,
Expand Down Expand Up @@ -563,7 +563,7 @@ public function updateQuota(?string $valueFromLDAP = null): void {
}

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;
}

/**
Expand Down
5 changes: 2 additions & 3 deletions lib/private/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use OC\Accounts\AccountManager;
use OC\Avatar\AvatarManager;
use OC\Hooks\Emitter;
use OC_Helper;
use OCP\Accounts\IAccountManager;
use OCP\Comments\ICommentsManager;
use OCP\EventDispatcher\IEventDispatcher;
Expand Down Expand Up @@ -570,11 +569,11 @@ public function getQuota() {
public function setQuota($quota) {
$oldQuota = $this->config->getUserValue($this->uid, 'files', 'quota', '');
if ($quota !== 'none' and $quota !== 'default') {
$bytesQuota = OC_Helper::computerFileSize($quota);
$bytesQuota = \OCP\Util::computerFileSize($quota);
if ($bytesQuota === false) {
throw new InvalidArgumentException('Failed to set quota to invalid value ' . $quota);
}
$quota = OC_Helper::humanFileSize($bytesQuota);
$quota = \OCP\Util::humanFileSize($bytesQuota);
}
if ($quota !== $oldQuota) {
$this->config->setUserValue($this->uid, 'files', 'quota', $quota);
Expand Down
57 changes: 4 additions & 53 deletions lib/private/legacy/OC_Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,75 +39,26 @@ class OC_Helper {
* Make a human file size
* @param int|float $bytes file size in bytes
* @return string a human readable file size
* @deprecated 4.0.0 replaced with \OCP\Util::humanFileSize
*
* Makes 2048 to 2 kB.
*/
public static function humanFileSize(int|float $bytes): string {
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";
return \OCP\Util::humanFileSize($bytes);
}

/**
* Make a computer file size
* @param string $str file size in human readable format
* @return false|int|float a file size in bytes
* @deprecated 4.0.0 Use \OCP\Util::computerFileSize
*
* Makes 2kB to 2048.
*
* Inspired by: https://www.php.net/manual/en/function.filesize.php#92418
*/
public static function computerFileSize(string $str): false|int|float {
$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));
return \OCP\Util::computerFileSize($str);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/private/legacy/OC_Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static function getUserQuota(?IUser $user) {
if ($userQuota === 'none') {
return \OCP\Files\FileInfo::SPACE_UNLIMITED;
}
return OC_Helper::computerFileSize($userQuota);
return \OCP\Util::computerFileSize($userQuota);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/public/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static function publicPreview_icon($path, $token) {
}

/**
* Make OC_Helper::humanFileSize available as a simple function
* Make \OCP\Util::humanFileSize available as a simple function
* Example: 2048 to 2 kB.
*
* @param int $bytes in bytes
Expand Down
59 changes: 55 additions & 4 deletions lib/public/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,19 +332,70 @@ public static function numericToNumber(string|float|int $number): int|float {
* @since 4.0.0
*/
public static function humanFileSize(int|float $bytes): string {
Copy link
Member

@solracsf solracsf May 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a suggestion:

public static function humanFileSize(int|float $bytes, int $precision = 1): string
	const KB = 1024;
	const UNITS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];

	if (!is_numeric($bytes) || $bytes < 0) {
		return '?';
	}

	$value = $bytes;
	$unitIndex = 0;
	while ($value >= KB && $unitIndex < count(UNITS) - 1) {
		$value /= KB;
		$unitIndex++;
	}

	return sprintf("%.${precision}f %s", $value, UNITS[$unitIndex]);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to be good but that would be better in a follow up PR.

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)
* Inspired by: https://www.php.net/manual/en/function.filesize.php#92418
*
* @param string $str file size in a fancy format
* @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
*/
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));
}

/**
Expand Down
Loading