Commit e0826445 by pupi1985

Refactored some byte handling operations

parent 02f8514d
......@@ -33,22 +33,11 @@
{
if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
$mindb = 16777215; // from MEDIUMBLOB column type
$minphp = trim(ini_get('upload_max_filesize'));
switch (strtolower(substr($minphp, -1))) {
case 'g':
$minphp *= 1024;
// fall-through
case 'm':
$minphp *= 1024;
// fall-through
case 'k':
$minphp *= 1024;
}
$minphp = convert_to_bytes(substr($minphp, -1), $minphp);
return min($mindb, $minphp);
return min(16777215, $minphp); // 16777215 from MEDIUMBLOB column type
}
......
......@@ -1064,21 +1064,35 @@
if (!is_numeric($unit)) {
$postmaxsize = substr($postmaxsize, 0, -1);
}
switch (strtoupper($unit)) { // Gets an integer value that can be compared against the size of the HTTP request
case 'G':
$postmaxsize *= 1024;
// fall-through
case 'M':
$postmaxsize *= 1024;
// fall-through
case 'K':
$postmaxsize *= 1024;
}
// Gets an integer value that can be compared against the size of the HTTP request
$postmaxsize = convert_to_bytes($unit, $postmaxsize);
return $_SERVER['CONTENT_LENGTH'] > $postmaxsize;
}
}
/**
* Turns a numeric value and a unit (g/m/k) into bytes
* @param string $unit One of 'g', 'm', 'k'. It is case insensitive
* @param int $value The value to turn into bytes
* @return int The amount of bytes the unit and the value represent. If the unit is not one of 'g', 'm' or 'k' then
* the original value is returned
*/
function convert_to_bytes($unit, $value)
{
switch (strtolower($unit)) {
case 'g':
return $value * 1073741824;
case 'm':
return $value * 1048576;
case 'k':
return $value * 1024;
default:
return $value;
}
}
function qa_is_http_post()
/*
Return true if we are responding to an HTTP POST request
......
......@@ -45,16 +45,7 @@
if (function_exists('memory_get_usage')) {
$gotbytes=trim(@ini_get('memory_limit'));
switch (strtolower(substr($gotbytes, -1))) {
case 'g':
$gotbytes *= 1024;
// fall-through
case 'm':
$gotbytes *= 1024;
// fall-through
case 'k':
$gotbytes *= 1024;
}
$gotbytes = convert_to_bytes(substr($gotbytes, -1), $gotbytes);
if ($gotbytes>0) { // otherwise we clearly don't know our limit
$gotbytes=($gotbytes-memory_get_usage())*0.9; // safety margin of 10%
......
......@@ -22,4 +22,32 @@ class BaseTest extends PHPUnit_Framework_TestCase
$test = qa_js(true, true);
$this->assertSame("'true'", $test);
}
public function test__convert_to_bytes()
{
$test = convert_to_bytes('k', 100);
$this->assertSame(102400, $test);
$test = convert_to_bytes('m', 100);
$this->assertSame(104857600, $test);
$test = convert_to_bytes('g', 100);
$this->assertSame(107374182400, $test);
$test = convert_to_bytes('K', 100);
$this->assertSame(102400, $test);
$test = convert_to_bytes('M', 100);
$this->assertSame(104857600, $test);
$test = convert_to_bytes('G', 100);
$this->assertSame(107374182400, $test);
$test = convert_to_bytes('', 100);
$this->assertSame(100, $test);
$test = convert_to_bytes('k', 1024);
$this->assertSame(1048576, $test);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment