diff --git a/lib/private/files.php b/lib/private/files.php index 152595ba697e33075c7762123fb31795be060b03..3affcf10449d4d8c6ad2102801980d942f8efa27 100644 --- a/lib/private/files.php +++ b/lib/private/files.php @@ -279,9 +279,7 @@ class OC_Files { return false; $size -= 1; } else { - $size = OC_Helper::humanFileSize($size); - $size = substr($size, 0, -1); //strip the B - $size = str_replace(' ', '', $size); //remove the space between the size and the postfix + $size = OC_Helper::phpFileSize($size); } //don't allow user to break his config -- broken or malicious size input diff --git a/lib/private/helper.php b/lib/private/helper.php index ab1e0d38924209dc31e1944d66f9e3b32eecbdbd..4058ec199a7f34964523dff94ad2794439ae340a 100644 --- a/lib/private/helper.php +++ b/lib/private/helper.php @@ -305,6 +305,32 @@ class OC_Helper { return "$bytes PB"; } + /** + * @brief Make a php file size + * @param int $bytes file size in bytes + * @return string a php parseable file size + * + * Makes 2048 to 2k and 2^41 to 2048G + */ + public static function phpFileSize($bytes) { + if ($bytes < 0) { + return "?"; + } + if ($bytes < 1024) { + return $bytes . "B"; + } + $bytes = round($bytes / 1024, 1); + if ($bytes < 1024) { + return $bytes . "K"; + } + $bytes = round($bytes / 1024, 1); + if ($bytes < 1024) { + return $bytes . "M"; + } + $bytes = round($bytes / 1024, 1); + return $bytes . "G"; + } + /** * @brief Make a computer file size * @param string $str file size in human readable format diff --git a/tests/lib/helper.php b/tests/lib/helper.php index 5d319e40f02b180c4cccf05b91fadb21ac6a3798..4aef4669cb3c17024d0c50b52f6b247b17f126e9 100644 --- a/tests/lib/helper.php +++ b/tests/lib/helper.php @@ -30,6 +30,28 @@ class Test_Helper extends PHPUnit_Framework_TestCase { ); } + /** + * @dataProvider phpFileSizeProvider + */ + public function testPhpFileSize($expected, $input) + { + $result = OC_Helper::phpFileSize($input); + $this->assertEquals($expected, $result); + } + + public function phpFileSizeProvider() + { + return array( + array('0B', 0), + array('1K', 1024), + array('9.5M', 10000000), + array('1.3G', 1395864371), + array('465.7G', 500000000000), + array('465661.3G', 500000000000000), + array('465661287.3G', 500000000000000000), + ); + } + /** * @dataProvider computerFileSizeProvider */