From c342aef031c018ff89654c91696fd281878e61b9 Mon Sep 17 00:00:00 2001 From: Lucas Bickel Date: Tue, 26 Dec 2017 12:46:17 +0100 Subject: [PATCH] Use max upload size from php config in frontend plupload js --- .../controllers/PluploadController.php | 58 +++++++++++++++++++ .../views/scripts/plupload/index.phtml | 3 + .../public/js/airtime/library/plupload.js | 2 +- 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/airtime_mvc/application/controllers/PluploadController.php b/airtime_mvc/application/controllers/PluploadController.php index 3b20f71e1..5515a2cb9 100644 --- a/airtime_mvc/application/controllers/PluploadController.php +++ b/airtime_mvc/application/controllers/PluploadController.php @@ -43,6 +43,15 @@ class PluploadController extends Zend_Controller_Action $csrf_form = new Zend_Form(); $csrf_form->addElement($csrf_element); $this->view->form = $csrf_form; + + // get max upload files size in MiB for plupload js. + $uploadMaxSize = $this->file_upload_max_size() / 1024 / 1024; + if ($uploadMaxSize === 0) { + // fall back to old default behaviour if unlimited uploads are + // configured on the server side. + $uploadMaxSize = 500; + } + $this->view->uploadMaxSize = $uploadMaxSize; } public function uploadAction() @@ -116,4 +125,53 @@ class PluploadController extends Zend_Controller_Action $this->view->iTotalRecords = $numTotalRecentUploads; $this->view->files = SecurityHelper::htmlescape_recursive($uploadsArray); } + + /** + * get configured upload max size from php + * + * Pinched from Drupal: https://github.com/drupal/drupal/blob/4204b0b29a7318008f10765cf88114bf3ed21c32/core/includes/file.inc#L1099 + * + * Drupal seems to be the only framework that does this somewhat the right + * way. I'm adding the method here since it's part of their core and I did + * not find an easy way to grab that thrrough composer in an isolated way. + */ + private function file_upload_max_size() { + static $max_size = -1; + if ($max_size < 0) { + + // Start with post_max_size. + $max_size = $this->bytes_to_int(ini_get('post_max_size')); + + // If upload_max_size is less, then reduce. Except if upload_max_size is + // zero, which indicates no limit. + $upload_max = $this->bytes_to_int(ini_get('upload_max_filesize')); + if ($upload_max > 0 && $upload_max < $max_size) { + $max_size = $upload_max; + } + } + return $max_size; + } + + /** + * Pinched from Drupal: https://github.com/drupal/drupal/blob/4204b0b29a7318008f10765cf88114bf3ed21c32/core/lib/Drupal/Component/Utility/Bytes.php#L27 + * + * This is the real point of importing the Drupal solution. They have done + * an implementation for figuring out what the user specified in the + * post_max_size and upload_max_size configuration. Sadly php does not + * support a nice way to get at the results of this config after it is + * parsed by the engine, hence the below hack. + */ + private function bytes_to_int($size) { + // Remove the non-unit characters from the size. + $unit = preg_replace('/[^bkmgtpezy]/i', '', $size); + // Remove the non-numeric characters from the size. + $size = preg_replace('/[^0-9\.]/', '', $size); + if ($unit) { + // Find the position of the unit in the ordered string which is the power + // of magnitude to multiply a kilobyte by. + return round($size * pow(1024, stripos('bkmgtpezy', $unit[0]))); + } else { + return round($size); + } + } } diff --git a/airtime_mvc/application/views/scripts/plupload/index.phtml b/airtime_mvc/application/views/scripts/plupload/index.phtml index 4f1d37a64..0af1d63b7 100644 --- a/airtime_mvc/application/views/scripts/plupload/index.phtml +++ b/airtime_mvc/application/views/scripts/plupload/index.phtml @@ -3,6 +3,9 @@ font-size: 200px !important; } + quotaLimitReached) { ?>
diff --git a/airtime_mvc/public/js/airtime/library/plupload.js b/airtime_mvc/public/js/airtime/library/plupload.js index 670f21b3a..60c8da533 100644 --- a/airtime_mvc/public/js/airtime/library/plupload.js +++ b/airtime_mvc/public/js/airtime/library/plupload.js @@ -20,7 +20,7 @@ $(document).ready(function () { acceptedFiles: acceptedMimeTypes.join(), addRemoveLinks: true, dictRemoveFile: $.i18n._("Remove"), - maxFilesize: 500, //Megabytes + maxFilesize:LIBRETIME_PLUPLOAD_MAX_FILE_SIZE, //Megabytes init: function () { this.on("sending", function (file, xhr, data) { data.append("csrf_token", $("#csrf").val());