Skip to content

Commit

Permalink
MDL-69718 core: Added support for TB and PT to display_size
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanheywood committed Sep 21, 2020
1 parent f852aa8 commit 6e935cc
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 35 deletions.
2 changes: 2 additions & 0 deletions lang/en/moodle.php
Expand Up @@ -1936,6 +1936,8 @@
$string['sizegb'] = 'GB';
$string['sizekb'] = 'KB';
$string['sizemb'] = 'MB';
$string['sizepb'] = 'PB';
$string['sizetb'] = 'TB';
$string['skipped'] = 'Skipped';
$string['skiptocategorylisting'] = 'Skip to the category listings';
$string['skiptocourselisting'] = 'Skip to the course listings';
Expand Down
36 changes: 21 additions & 15 deletions lib/moodlelib.php
Expand Up @@ -7104,27 +7104,33 @@ function get_directory_size($rootdir, $excludefile='') {
*/
function display_size($size) {

static $gb, $mb, $kb, $b;
static $units;

if ($size === USER_CAN_IGNORE_FILE_SIZE_LIMITS) {
return get_string('unlimited');
}

if (empty($gb)) {
$gb = get_string('sizegb');
$mb = get_string('sizemb');
$kb = get_string('sizekb');
$b = get_string('sizeb');
}

if ($size >= 1073741824) {
$size = round($size / 1073741824 * 10) / 10 . $gb;
} else if ($size >= 1048576) {
$size = round($size / 1048576 * 10) / 10 . $mb;
} else if ($size >= 1024) {
$size = round($size / 1024 * 10) / 10 . $kb;
if (empty($units)) {
$units[] = get_string('sizeb');
$units[] = get_string('sizekb');
$units[] = get_string('sizemb');
$units[] = get_string('sizegb');
$units[] = get_string('sizetb');
$units[] = get_string('sizepb');
}

if ($size >= 1024 ** 5) {
$size = round($size / 1024 ** 5 * 10) / 10 . $units[5];
} else if ($size >= 1024 ** 4) {
$size = round($size / 1024 ** 4 * 10) / 10 . $units[4];
} else if ($size >= 1024 ** 3) {
$size = round($size / 1024 ** 3 * 10) / 10 . $units[3];
} else if ($size >= 1024 ** 2) {
$size = round($size / 1024 ** 2 * 10) / 10 . $units[2];
} else if ($size >= 1024 ** 1) {
$size = round($size / 1024 ** 1 * 10) / 10 . $units[1];
} else {
$size = intval($size) .' '. $b; // File sizes over 2GB can not work in 32bit PHP anyway.
$size = intval($size) .' '. $units[0]; // File sizes over 2GB can not work in 32bit PHP anyway.
}
return $size;
}
Expand Down
20 changes: 11 additions & 9 deletions lib/setuplib.php
Expand Up @@ -1325,17 +1325,19 @@ function get_real_size($size = 0) {
}

static $binaryprefixes = array(
'K' => 1024,
'k' => 1024,
'M' => 1048576,
'm' => 1048576,
'G' => 1073741824,
'g' => 1073741824,
'T' => 1099511627776,
't' => 1099511627776,
'K' => 1024 ** 1,
'k' => 1024 ** 1,
'M' => 1024 ** 2,
'm' => 1024 ** 2,
'G' => 1024 ** 3,
'g' => 1024 ** 3,
'T' => 1024 ** 4,
't' => 1024 ** 4,
'P' => 1024 ** 5,
'p' => 1024 ** 5,
);

if (preg_match('/^([0-9]+)([KMGT])/i', $size, $matches)) {
if (preg_match('/^([0-9]+)([KMGTP])/i', $size, $matches)) {
return $matches[1] * $binaryprefixes[$matches[2]];
}

Expand Down
41 changes: 41 additions & 0 deletions lib/tests/moodlelib_test.php
Expand Up @@ -4776,4 +4776,45 @@ public function test_rename_to_unused_name_failure() {
$file = $CFG->dataroot . '/argh.txt';
$this->assertFalse(rename_to_unused_name($file));
}

/**
* Provider for display_size
*
* @return array of ($size, $expected)
*/
public function display_size_provider() {

return [
[0, '0 bytes' ],
[1, '1 bytes' ],
[1023, '1023 bytes' ],
[1024, '1KB' ],
[2222, '2.2KB' ],
[33333, '32.6KB' ],
[444444, '434KB' ],
[5555555, '5.3MB' ],
[66666666, '63.6MB' ],
[777777777, '741.7MB'],
[8888888888, '8.3GB' ],
[99999999999, '93.1GB' ],
[111111111111, '103.5GB'],
[2222222222222, '2TB' ],
[33333333333333, '30.3TB' ],
[444444444444444, '404.2TB'],
[5555555555555555, '4.9PB' ],
[66666666666666666, '59.2PB' ],
[777777777777777777, '690.8PB'],
];
}

/**
* Test display_size
* @dataProvider display_size_provider
* @param int $size the size in bytes
* @param string $expected the expected string.
*/
public function test_display_size($size, $expected) {
$result = display_size($size);
$this->assertEquals($expected, $result);
}
}
28 changes: 17 additions & 11 deletions lib/tests/setuplib_test.php
Expand Up @@ -450,17 +450,23 @@ public function get_exception_info($ex) {
*/
public function data_for_test_get_real_size() {
return array(
array('8KB', 8192),
array('8Kb', 8192),
array('8K', 8192),
array('8k', 8192),
array('50MB', 52428800),
array('50Mb', 52428800),
array('50M', 52428800),
array('50m', 52428800),
array('8Gb', 8589934592),
array('8GB', 8589934592),
array('8G', 8589934592),
array('8KB', 8192),
array('8Kb', 8192),
array('8K', 8192),
array('8k', 8192),
array('50MB', 52428800),
array('50Mb', 52428800),
array('50M', 52428800),
array('50m', 52428800),
array('8GB', 8589934592),
array('8Gb', 8589934592),
array('8G', 8589934592),
array('7T', 7696581394432),
array('7TB', 7696581394432),
array('7Tb', 7696581394432),
array('6P', 6755399441055744),
array('6PB', 6755399441055744),
array('6Pb', 6755399441055744),
);
}

Expand Down

0 comments on commit 6e935cc

Please sign in to comment.