Skip to content

Commit

Permalink
Fix 32bits bigint support in Util/OC_Helper
Browse files Browse the repository at this point in the history
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
  • Loading branch information
come-nc committed Jan 23, 2023
1 parent 2e8e20a commit 5cc4d14
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
25 changes: 12 additions & 13 deletions lib/private/legacy/OC_Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
use OCP\ICacheFactory;
use OCP\IBinaryFinder;
use OCP\IUser;
use OCP\Util;
use Psr\Log\LoggerInterface;

/**
Expand All @@ -59,12 +60,12 @@ class OC_Helper {

/**
* Make a human file size
* @param int $bytes file size in bytes
* @param int|float $bytes file size in bytes
* @return string a human readable file size
*
* Makes 2048 to 2 kB.
*/
public static function humanFileSize($bytes) {
public static function humanFileSize(int|float $bytes): string {
if ($bytes < 0) {
return "?";
}
Expand Down Expand Up @@ -126,9 +127,7 @@ public static function computerFileSize(string $str): false|int|float {
return false;
}

$bytes = round($bytes);

return $bytes;
return Util::numericToNumber(round($bytes));
}

/**
Expand Down Expand Up @@ -384,8 +383,8 @@ public static function recursiveArraySearch($haystack, $needle, $index = null) {
* calculates the maximum upload size respecting system settings, free space and user quota
*
* @param string $dir the current folder where the user currently operates
* @param int $freeSpace the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly
* @return int number of bytes representing
* @param int|float $freeSpace the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly
* @return int|float number of bytes representing
*/
public static function maxUploadFilesize($dir, $freeSpace = null) {
if (is_null($freeSpace) || $freeSpace < 0) {
Expand All @@ -398,7 +397,7 @@ public static function maxUploadFilesize($dir, $freeSpace = null) {
* Calculate free space left within user quota
*
* @param string $dir the current folder where the user currently operates
* @return int number of bytes representing
* @return int|float number of bytes representing
*/
public static function freeSpace($dir) {
$freeSpace = \OC\Files\Filesystem::free_space($dir);
Expand All @@ -413,12 +412,12 @@ public static function freeSpace($dir) {
/**
* Calculate PHP upload limit
*
* @return int PHP upload file size limit
* @return int|float PHP upload file size limit
*/
public static function uploadLimit() {
$ini = \OC::$server->get(IniGetWrapper::class);
$upload_max_filesize = (int)OCP\Util::computerFileSize($ini->get('upload_max_filesize'));
$post_max_size = (int)OCP\Util::computerFileSize($ini->get('post_max_size'));
$upload_max_filesize = Util::computerFileSize($ini->get('upload_max_filesize'));
$post_max_size = Util::computerFileSize($ini->get('post_max_size'));
if ($upload_max_filesize === 0 && $post_max_size === 0) {
return INF;
} elseif ($upload_max_filesize === 0 || $post_max_size === 0) {
Expand Down Expand Up @@ -579,8 +578,8 @@ public static function getStorageInfo($path, $rootInfo = null, $includeMountPoin
/**
* Get storage info including all mount points and quota
*/
private static function getGlobalStorageInfo(int $quota, IUser $user, IMountPoint $mount): array {
$rootInfo = \OC\Files\Filesystem::getFileInfo('', 'ext');
private static function getGlobalStorageInfo(int|float $quota, IUser $user, IMountPoint $mount): array {
$rootInfo = \OC\Files\Filesystem::getFileInfo('', true);
$used = $rootInfo['size'];
if ($used < 0) {
$used = 0;
Expand Down
4 changes: 2 additions & 2 deletions lib/private/legacy/OC_Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public static function isSharingDisabledForUser(IConfig $config, IGroupManager $
/**
* check if share API enforces a default expire date
*
* @return boolean
* @return bool
* @suppress PhanDeprecatedFunction
*/
public static function isDefaultExpireDateEnforced() {
Expand All @@ -159,7 +159,7 @@ public static function isDefaultExpireDateEnforced() {
* Get the quota of a user
*
* @param IUser|null $user
* @return int|\OCP\Files\FileInfo::SPACE_UNLIMITED|false Quota bytes
* @return int|\OCP\Files\FileInfo::SPACE_UNLIMITED|false|float Quota bytes
*/
public static function getUserQuota(?IUser $user) {
if (is_null($user)) {
Expand Down
10 changes: 5 additions & 5 deletions lib/public/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,22 +341,22 @@ public static function getDefaultEmailAddress(string $user_part): string {

/**
* Converts string to int of float depending if it fits an int
* @param numeric-string $number numeric string
* @param numeric-string|float|int $number numeric string
* @return int|float int if it fits, float if it is too big
* @since 26.0.0
*/
public static function numericToNumber(string $number): int|float {
public static function numericToNumber(string|float|int $number): int|float {
/* This is a hack to cast to (int|float) */
return 0 + $number;
}

/**
* Make a human file size (2048 to 2 kB)
* @param int $bytes file size in bytes
* @param int|float $bytes file size in bytes
* @return string a human readable file size
* @since 4.0.0
*/
public static function humanFileSize($bytes) {
public static function humanFileSize(int|float $bytes): string {
return \OC_Helper::humanFileSize($bytes);
}

Expand All @@ -368,7 +368,7 @@ public static function humanFileSize($bytes) {
* Inspired by: https://www.php.net/manual/en/function.filesize.php#92418
* @since 4.0.0
*/
public static function computerFileSize($str) {
public static function computerFileSize(string $str): false|int|float {
return \OC_Helper::computerFileSize($str);
}

Expand Down

0 comments on commit 5cc4d14

Please sign in to comment.