diff options
author | ArthurHoaro <arthur@hoa.ro> | 2017-04-10 20:01:10 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2017-04-10 20:01:10 +0200 |
commit | 6a19124a0970dfd5744c4a193b5d907ba85b323e (patch) | |
tree | ad886ea19ba4d38cea43ad52faf264da0383db58 | |
parent | c8cb5c282402e3f19497ba9e57ac967a4bdefa1b (diff) | |
download | Shaarli-6a19124a0970dfd5744c4a193b5d907ba85b323e.tar.gz Shaarli-6a19124a0970dfd5744c4a193b5d907ba85b323e.tar.zst Shaarli-6a19124a0970dfd5744c4a193b5d907ba85b323e.zip |
Use raw bytes for upload size hidden input
-rw-r--r-- | application/Utils.php | 11 | ||||
-rw-r--r-- | index.php | 17 | ||||
-rw-r--r-- | tests/UtilsTest.php | 13 | ||||
-rw-r--r-- | tpl/default/import.html | 1 |
4 files changed, 35 insertions, 7 deletions
diff --git a/application/Utils.php b/application/Utils.php index 3ef2a7e2..ab463af9 100644 --- a/application/Utils.php +++ b/application/Utils.php | |||
@@ -414,23 +414,24 @@ function human_bytes($bytes) | |||
414 | $bytes /= 1024; | 414 | $bytes /= 1024; |
415 | } | 415 | } |
416 | 416 | ||
417 | return $bytes . $units[$i]; | 417 | return round($bytes) . $units[$i]; |
418 | } | 418 | } |
419 | 419 | ||
420 | /** | 420 | /** |
421 | * Try to determine max file size for uploads (POST). | 421 | * Try to determine max file size for uploads (POST). |
422 | * Returns an integer (in bytes) | 422 | * Returns an integer (in bytes) or formatted depending on $format. |
423 | * | 423 | * |
424 | * @param mixed $limitPost post_max_size PHP setting | 424 | * @param mixed $limitPost post_max_size PHP setting |
425 | * @param mixed $limitUpload upload_max_filesize PHP setting | 425 | * @param mixed $limitUpload upload_max_filesize PHP setting |
426 | * @param bool $format Format max upload size to human readable size | ||
426 | * | 427 | * |
427 | * @return int max upload file size in bytes. | 428 | * @return int|string max upload file size |
428 | */ | 429 | */ |
429 | function get_max_upload_size($limitPost, $limitUpload) | 430 | function get_max_upload_size($limitPost, $limitUpload, $format = true) |
430 | { | 431 | { |
431 | $size1 = return_bytes($limitPost); | 432 | $size1 = return_bytes($limitPost); |
432 | $size2 = return_bytes($limitUpload); | 433 | $size2 = return_bytes($limitUpload); |
433 | // Return the smaller of two: | 434 | // Return the smaller of two: |
434 | $maxsize = min($size1, $size2); | 435 | $maxsize = min($size1, $size2); |
435 | return human_bytes($maxsize); | 436 | return $format ? human_bytes($maxsize) : $maxsize; |
436 | } | 437 | } |
@@ -1489,7 +1489,22 @@ function renderPage($conf, $pluginManager, $LINKSDB) | |||
1489 | 1489 | ||
1490 | if (! isset($_POST['token']) || ! isset($_FILES['filetoupload'])) { | 1490 | if (! isset($_POST['token']) || ! isset($_FILES['filetoupload'])) { |
1491 | // Show import dialog | 1491 | // Show import dialog |
1492 | $PAGE->assign('maxfilesize', get_max_upload_size(ini_get('post_max_size'), ini_get('upload_max_filesize'))); | 1492 | $PAGE->assign( |
1493 | 'maxfilesize', | ||
1494 | get_max_upload_size( | ||
1495 | ini_get('post_max_size'), | ||
1496 | ini_get('upload_max_filesize'), | ||
1497 | false | ||
1498 | ) | ||
1499 | ); | ||
1500 | $PAGE->assign( | ||
1501 | 'maxfilesizeHuman', | ||
1502 | get_max_upload_size( | ||
1503 | ini_get('post_max_size'), | ||
1504 | ini_get('upload_max_filesize'), | ||
1505 | true | ||
1506 | ) | ||
1507 | ); | ||
1493 | $PAGE->renderPage('import'); | 1508 | $PAGE->renderPage('import'); |
1494 | exit; | 1509 | exit; |
1495 | } | 1510 | } |
diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php index e5ff01e6..d6a0aad5 100644 --- a/tests/UtilsTest.php +++ b/tests/UtilsTest.php | |||
@@ -392,13 +392,14 @@ class UtilsTest extends PHPUnit_Framework_TestCase | |||
392 | $this->assertEquals('2GiB', human_bytes(strval(2 * (pow(1024, 3))))); | 392 | $this->assertEquals('2GiB', human_bytes(strval(2 * (pow(1024, 3))))); |
393 | $this->assertEquals('374B', human_bytes(374)); | 393 | $this->assertEquals('374B', human_bytes(374)); |
394 | $this->assertEquals('374B', human_bytes('374')); | 394 | $this->assertEquals('374B', human_bytes('374')); |
395 | $this->assertEquals('232kiB', human_bytes(237481)); | ||
395 | $this->assertEquals('Unlimited', human_bytes('0')); | 396 | $this->assertEquals('Unlimited', human_bytes('0')); |
396 | $this->assertEquals('Unlimited', human_bytes(0)); | 397 | $this->assertEquals('Unlimited', human_bytes(0)); |
397 | $this->assertEquals('Setting not set', human_bytes('')); | 398 | $this->assertEquals('Setting not set', human_bytes('')); |
398 | } | 399 | } |
399 | 400 | ||
400 | /** | 401 | /** |
401 | * Test get_max_upload_size | 402 | * Test get_max_upload_size with formatting |
402 | */ | 403 | */ |
403 | public function testGetMaxUploadSize() | 404 | public function testGetMaxUploadSize() |
404 | { | 405 | { |
@@ -406,4 +407,14 @@ class UtilsTest extends PHPUnit_Framework_TestCase | |||
406 | $this->assertEquals('1MiB', get_max_upload_size('1m', '2m')); | 407 | $this->assertEquals('1MiB', get_max_upload_size('1m', '2m')); |
407 | $this->assertEquals('100B', get_max_upload_size(100, 100)); | 408 | $this->assertEquals('100B', get_max_upload_size(100, 100)); |
408 | } | 409 | } |
410 | |||
411 | /** | ||
412 | * Test get_max_upload_size without formatting | ||
413 | */ | ||
414 | public function testGetMaxUploadSizeRaw() | ||
415 | { | ||
416 | $this->assertEquals('1048576', get_max_upload_size(2097152, '1024k', false)); | ||
417 | $this->assertEquals('1048576', get_max_upload_size('1m', '2m', false)); | ||
418 | $this->assertEquals('100', get_max_upload_size(100, 100, false)); | ||
419 | } | ||
409 | } | 420 | } |
diff --git a/tpl/default/import.html b/tpl/default/import.html index e6e521e8..1f040685 100644 --- a/tpl/default/import.html +++ b/tpl/default/import.html | |||
@@ -18,6 +18,7 @@ | |||
18 | <div class="center" id="import-field"> | 18 | <div class="center" id="import-field"> |
19 | <input type="hidden" name="MAX_FILE_SIZE" value="{$maxfilesize}"> | 19 | <input type="hidden" name="MAX_FILE_SIZE" value="{$maxfilesize}"> |
20 | <input type="file" name="filetoupload"> | 20 | <input type="file" name="filetoupload"> |
21 | <p><br>Maximum size allowed: <strong>{$maxfilesizeHuman}</strong></p> | ||
21 | </div> | 22 | </div> |
22 | 23 | ||
23 | <div class="pure-g"> | 24 | <div class="pure-g"> |