Merge branch 'MDL-69718-display-size' of https://github.com/brendanheywood/moodle into master

This commit is contained in:
Eloy Lafuente (stronk7) 2020-09-24 16:39:16 +02:00
commit b583ce1826
5 changed files with 90 additions and 33 deletions

View File

@ -1936,6 +1936,8 @@ $string['sizeb'] = 'bytes';
$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';

View File

@ -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 (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 >= 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 ($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;
}

View File

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

View File

@ -4776,4 +4776,45 @@ class core_moodlelib_testcase extends advanced_testcase {
$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);
}
}

View File

@ -450,17 +450,23 @@ class core_setuplib_testcase extends advanced_testcase {
*/
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),
);
}