From fc8704b520a09315f1e1b9002af75a85a8cdc6a1 Mon Sep 17 00:00:00 2001 From: Urban Rotnik Date: Wed, 30 Sep 2020 12:10:49 +0200 Subject: [PATCH] Add fileUtils for sizeCheck, add url to textarea data attribute --- app/assets/javascripts/sitewide/file_utils.js | 14 +++++++++++ .../sitewide/tiny_mce_file_upload_plugin.js | 24 +++++++------------ app/helpers/bootstrap_form_helper.rb | 7 +++++- 3 files changed, 29 insertions(+), 16 deletions(-) create mode 100644 app/assets/javascripts/sitewide/file_utils.js diff --git a/app/assets/javascripts/sitewide/file_utils.js b/app/assets/javascripts/sitewide/file_utils.js new file mode 100644 index 000000000..0b3c91c15 --- /dev/null +++ b/app/assets/javascripts/sitewide/file_utils.js @@ -0,0 +1,14 @@ +/* eslint no-unused-vars: "off" */ +/* global I18n GLOBAL_CONSTANTS HelperModule*/ + +function validateFileSize(file, showErrorMessage = false) { + var validSize = true; + if (file.size > GLOBAL_CONSTANTS.FILE_MAX_SIZE_MB * 1024 * 1024) { + validSize = false; + } + + if (!validSize && showErrorMessage) { + HelperModule.flashAlertMsg(I18n.t('general.file.size_exceeded', { file_size: GLOBAL_CONSTANTS.FILE_MAX_SIZE_MB }), 'danger'); + } + return validSize; +} diff --git a/app/assets/javascripts/sitewide/tiny_mce_file_upload_plugin.js b/app/assets/javascripts/sitewide/tiny_mce_file_upload_plugin.js index 000b1884e..e841d4c30 100644 --- a/app/assets/javascripts/sitewide/tiny_mce_file_upload_plugin.js +++ b/app/assets/javascripts/sitewide/tiny_mce_file_upload_plugin.js @@ -1,7 +1,7 @@ /* eslint no-underscore-dangle: "off" */ /* eslint no-use-before-define: "off" */ /* eslint no-restricted-syntax: ["off", "BinaryExpression[operator='in']"] */ -/* global tinymce I18n GLOBAL_CONSTANTS HelperModule*/ +/* global tinymce I18n HelperModule validateFileSize */ (function() { 'use strict'; @@ -25,23 +25,18 @@ let files = $('#tinymce_current_upload')[0].files; Array.from(files).forEach(file => formData.append('files[]', file, file.name)); - $.post({ - url: '/tiny_mce_assets', + url: textAreaElement.data('tinymce-asset-path'), data: formData, processData: false, contentType: false, beforeSend: function(xhr) { - let sizeLimit = false; - Array.from(files).forEach(file => { - if (file.size > GLOBAL_CONSTANTS.FILE_MAX_SIZE_MB * 1024 * 1024) { - sizeLimit = true; + Array.from(files).every(file => { + if (!validateFileSize(file, true)) { + xhr.abort(); + return false; } }); - if (sizeLimit) { - HelperModule.flashAlertMsg(I18n.t('general.file.size_exceeded', { file_size: GLOBAL_CONSTANTS.FILE_MAX_SIZE_MB }), 'danger'); - xhr.abort(); - } }, success: function(data) { handleResponse(data); @@ -69,10 +64,9 @@ } function buildHTML(image) { - var imgstr = ""; - return imgstr; + return `description-${image.token}`; } // Create hidden field for images diff --git a/app/helpers/bootstrap_form_helper.rb b/app/helpers/bootstrap_form_helper.rb index debb53bdb..a8a1d467a 100644 --- a/app/helpers/bootstrap_form_helper.rb +++ b/app/helpers/bootstrap_form_helper.rb @@ -254,7 +254,12 @@ module BootstrapFormHelper # Returns