From dbc55bdb6fc25f3c437150a8f804e3e520b768ad Mon Sep 17 00:00:00 2001 From: Sergio Brighenti Date: Thu, 24 Jan 2019 11:49:46 +0100 Subject: [PATCH] Fixed inaccurate message in case of file too large (fixes #15) Minor fix to the mobile layout --- CHANGELOG.md | 4 +++ app/Controllers/UploadController.php | 10 +++++++ app/helpers.php | 28 +++++++++++++++++++ composer.json | 2 +- .../templates/dashboard/pager_header.twig | 2 +- 5 files changed, 44 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 18f0afd3..ddae5c15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## v2.4.1 ++ Fixed error message when the file is too large. (#15) ++ Fixed button alignment. + ## v2.4 + Added function to remove orphaned files. + Switch between tab and gallery mode using an admin account. diff --git a/app/Controllers/UploadController.php b/app/Controllers/UploadController.php index b443b6fc..d1a43eb7 100644 --- a/app/Controllers/UploadController.php +++ b/app/Controllers/UploadController.php @@ -26,6 +26,16 @@ public function upload(Request $request, Response $response): Response $json = ['message' => null]; + if ($request->getServerParam('CONTENT_LENGTH') > stringToBytes(ini_get('post_max_size'))) { + $json['message'] = 'File too large (post_max_size too low).'; + return $response->withJson($json, 400); + } + + if ($request->getUploadedFiles()['upload']->getError() === UPLOAD_ERR_INI_SIZE) { + $json['message'] = 'File too large (upload_max_filesize too low).'; + return $response->withJson($json, 400); + } + if ($request->getParam('token') === null) { $json['message'] = 'Token not specified.'; return $response->withJson($json, 400); diff --git a/app/helpers.php b/app/helpers.php index f467da17..f421db5e 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -34,6 +34,34 @@ function humanFileSize($size, $precision = 2): string } } +if (!function_exists('stringToBytes')) { + /** + * @param $str + * @return int|string + */ + function stringToBytes(string $str): int + { + $val = trim($str); + if (is_numeric($val)) { + return (int)$val; + } + + $last = strtolower($val[strlen($val) - 1]); + $val = substr($val, 0, -1); + + $val = (int)$val; + switch ($last) { + case 'g': + $val *= 1024; + case 'm': + $val *= 1024; + case 'k': + $val *= 1024; + } + return $val; + } +} + if (!function_exists('removeDirectory')) { /** * Remove a directory and it's content diff --git a/composer.json b/composer.json index 4e43609d..adb76b94 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "sergix44/xbackbone", - "version": "2.4", + "version": "2.4.1", "description": "A lightweight ShareX PHP backend", "type": "project", "require": { diff --git a/resources/templates/dashboard/pager_header.twig b/resources/templates/dashboard/pager_header.twig index ac49f268..954c1827 100644 --- a/resources/templates/dashboard/pager_header.twig +++ b/resources/templates/dashboard/pager_header.twig @@ -12,7 +12,7 @@
{% include 'comp/pager.twig' %}
-
+