From db1944cc90a710e3415f64ffb871cc184357b1bc Mon Sep 17 00:00:00 2001 From: zmagod Date: Tue, 19 Jun 2018 14:15:14 +0200 Subject: [PATCH] move maximum file size limit to an ENV variable [fixes SCI-2415] --- .../javascripts/my_modules/protocols.js.erb | 2 +- .../protocols/import_export/import.js.erb | 2 +- .../javascripts/sitewide/drag_n_drop.js.erb | 16 ++++++++-------- .../javascripts/sitewide/form_validators.js.erb | 6 +++--- app/controllers/protocols_controller.rb | 2 +- app/controllers/repositories_controller.rb | 2 +- app/controllers/teams_controller.rb | 4 ++-- .../users/registrations_controller.rb | 2 +- app/models/asset.rb | 2 +- app/models/tiny_mce_asset.rb | 2 +- .../import_repository/parse_repository.rb | 2 +- .../protocols/protocolsio_import_create.js.erb | 2 +- config/application.rb | 3 +++ config/initializers/constants.rb | 2 -- 14 files changed, 25 insertions(+), 24 deletions(-) diff --git a/app/assets/javascripts/my_modules/protocols.js.erb b/app/assets/javascripts/my_modules/protocols.js.erb index 470d7826d..bfacb2224 100644 --- a/app/assets/javascripts/my_modules/protocols.js.erb +++ b/app/assets/javascripts/my_modules/protocols.js.erb @@ -470,7 +470,7 @@ function initImport() { } else { if (data.status === 'size_too_large') { alert('<%= I18n.t('my_modules.protocols.load_from_file_size_error', - size: Constants::FILE_MAX_SIZE_MB ) %>'); + size: Rails.configuration.x.file_max_size_mb ) %>'); } else { alert(I18n.t("my_modules.protocols.load_from_file_error")); } diff --git a/app/assets/javascripts/protocols/import_export/import.js.erb b/app/assets/javascripts/protocols/import_export/import.js.erb index c76a0155c..baf0081f6 100644 --- a/app/assets/javascripts/protocols/import_export/import.js.erb +++ b/app/assets/javascripts/protocols/import_export/import.js.erb @@ -557,7 +557,7 @@ function importProtocolFromFile( $.extend(data_json, params); var rough_size = roughSizeOfObject(data_json); - if (rough_size > <%= Constants::FILE_MAX_SIZE_MB.megabytes %>) { + if (rough_size > <%= Rails.configuration.x.file_max_size_mb.megabytes %>) { // Call the callback function resultCallback({ name: protocolJson["name"], new_name: null, status: "size_too_large" }); return; diff --git a/app/assets/javascripts/sitewide/drag_n_drop.js.erb b/app/assets/javascripts/sitewide/drag_n_drop.js.erb index 4c513c6dd..cefc51dd7 100644 --- a/app/assets/javascripts/sitewide/drag_n_drop.js.erb +++ b/app/assets/javascripts/sitewide/drag_n_drop.js.erb @@ -235,23 +235,23 @@ function _validateFilesSize(file) { var fileSize = file.size; totalSize += parseInt(fileSize); - if(fileSize > <%= Constants::FILE_MAX_SIZE_MB.megabyte %>) { + if(fileSize > <%= Rails.configuration.x.file_max_size_mb.megabyte %>) { file.isValid = false; _disableSubmitButton(); - return "

<%= I18n.t 'general.file.size_exceeded', file_size: Constants::FILE_MAX_SIZE_MB %>

"; + return "

<%= I18n.t 'general.file.size_exceeded', file_size: Rails.configuration.x.file_max_size_mb %>

"; } return ''; } function _validateTotalSize() { - if(totalSize > <%= Constants::FILE_MAX_SIZE_MB.megabyte %>) { + if(totalSize > <%= Rails.configuration.x.file_max_size_mb.megabyte %>) { filesValid = false; _disableSubmitButton(); $.each($('.panel-step-attachment-new'), function() { if(!$(this).find('p').hasClass('dnd-total-error')) { $(this) .find('.panel-body') - .append("

<%= I18n.t('general.file.total_size', size: Constants::FILE_MAX_SIZE_MB) %>

"); + .append("

<%= I18n.t('general.file.total_size', size: Rails.configuration.x.file_max_size_mb) %>

"); } }); } else { @@ -392,23 +392,23 @@ function _validateFilesSize(file) { var fileSize = file.size; totalSize += parseInt(fileSize); - if(fileSize > <%= Constants::FILE_MAX_SIZE_MB.megabyte %>) { + if(fileSize > <%= Rails.configuration.x.file_max_size_mb.megabyte %>) { file.isValid = false; _disableSubmitButton(); - return "

<%= I18n.t 'general.file.size_exceeded', file_size: Constants::FILE_MAX_SIZE_MB %>

"; + return "

<%= I18n.t 'general.file.size_exceeded', file_size: Rails.configuration.x.file_max_size_mb %>

"; } return ''; } function _validateTotalSize() { - if(totalSize > <%= Constants::FILE_MAX_SIZE_MB.megabyte %>) { + if(totalSize > <%= Rails.configuration.x.file_max_size_mb.megabyte %>) { isValid = false; _disableSubmitButton(); $.each($('.panel-result-attachment-new'), function() { if(!$(this).find('p').hasClass('dnd-total-error')) { $(this) .find('.panel-body') - .append("

<%= I18n.t('general.file.total_size', size: Constants::FILE_MAX_SIZE_MB) %>

"); + .append("

<%= I18n.t('general.file.total_size', size: Rails.configuration.x.file_max_size_mb) %>

"); } }); } else { diff --git a/app/assets/javascripts/sitewide/form_validators.js.erb b/app/assets/javascripts/sitewide/form_validators.js.erb index 42b724412..6817111cb 100644 --- a/app/assets/javascripts/sitewide/form_validators.js.erb +++ b/app/assets/javascripts/sitewide/form_validators.js.erb @@ -102,7 +102,7 @@ function checklistsValidator(ev, checklists, editMode) { } var FileTypeEnum = Object.freeze({ - FILE: <%= Constants::FILE_MAX_SIZE_MB.megabytes %>, + FILE: <%= Rails.configuration.x.file_max_size_mb.megabytes %>, AVATAR: <%= Constants::AVATAR_MAX_SIZE_MB.megabytes %> }); @@ -140,7 +140,7 @@ function filesSizeValidator(ev, fileInputs, fileTypeEnum) { if (file.size > fileTypeEnum) { switch (fileTypeEnum) { case FileTypeEnum.FILE: - return "<%= I18n.t 'general.file.size_exceeded', file_size: Constants::FILE_MAX_SIZE_MB %>".strToErrorFormat(); + return "<%= I18n.t 'general.file.size_exceeded', file_size: Rails.configuration.x.file_max_size_mb %>".strToErrorFormat(); case FileTypeEnum.AVATAR: return "<%= I18n.t 'general.file.size_exceeded', file_size: Constants::AVATAR_MAX_SIZE_MB %>".strToErrorFormat(); } @@ -161,7 +161,7 @@ function filesSizeValidator(ev, fileInputs, fileTypeEnum) { if (size > fileTypeEnum) { switch (fileTypeEnum) { case FileTypeEnum.FILE: - return "<%= I18n.t('general.file.total_size', size: Constants::FILE_MAX_SIZE_MB) %>".strToErrorFormat(); + return "<%= I18n.t('general.file.total_size', size: Rails.configuration.x.file_max_size_mb) %>".strToErrorFormat(); case FileTypeEnum.AVATAR: return "<%= I18n.t('users.registrations.edit.avatar_total_size', size: Constants::AVATAR_MAX_SIZE_MB) %>".strToErrorFormat(); } diff --git a/app/controllers/protocols_controller.rb b/app/controllers/protocols_controller.rb index 52c2476ed..219cb4a97 100644 --- a/app/controllers/protocols_controller.rb +++ b/app/controllers/protocols_controller.rb @@ -618,7 +618,7 @@ class ProtocolsController < ApplicationController end return 0 # return 0 stops the rest of the controller code from executing end - if file_size > Constants::FILE_MAX_SIZE_MB.megabytes + if file_size > Rails.configuration.x.file_max_size_mb.megabytes @protocolsio_too_big = true respond_to do |format| format.js {} diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 49d854c41..a4d3822cb 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -191,7 +191,7 @@ class RepositoriesController < ApplicationController ) if parsed_file.too_large? repository_response(t('general.file.size_exceeded', - file_size: Constants::FILE_MAX_SIZE_MB)) + file_size: Rails.configuration.x.file_max_size_mb)) elsif parsed_file.has_too_many_rows? repository_response( t('repositories.import_records.error_message.items_limit', diff --git a/app/controllers/teams_controller.rb b/app/controllers/teams_controller.rb index a910df32a..925ca8f10 100644 --- a/app/controllers/teams_controller.rb +++ b/app/controllers/teams_controller.rb @@ -11,9 +11,9 @@ class TeamsController < ApplicationController unless import_params[:file] return parse_sheet_error(t('teams.parse_sheet.errors.no_file_selected')) end - if import_params[:file].size > Constants::FILE_MAX_SIZE_MB.megabytes + if import_params[:file].size > Rails.configuration.x.file_max_size_mb.megabytes error = t('general.file.size_exceeded', - file_size: Constants::FILE_MAX_SIZE_MB) + file_size: Rails.configuration.x.file_max_size_mb) return parse_sheet_error(error) end diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb index b50f8b9f3..58ac7bb8f 100644 --- a/app/controllers/users/registrations_controller.rb +++ b/app/controllers/users/registrations_controller.rb @@ -276,7 +276,7 @@ class Users::RegistrationsController < Devise::RegistrationsController success_action_status: '201', acl: 'public-read', storage_class: "REDUCED_REDUNDANCY", - content_length_range: 1..Constants::FILE_MAX_SIZE_MB.megabytes, + content_length_range: 1..Rails.configuration.x.file_max_size_mb.megabytes, content_type: content_type ) posts.push({ diff --git a/app/models/asset.rb b/app/models/asset.rb index 8aa8d2e1a..02d8dc52a 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb @@ -19,7 +19,7 @@ class Asset < ApplicationRecord validates_attachment :file, presence: true, size: { - less_than: Constants::FILE_MAX_SIZE_MB.megabytes + less_than: Rails.configuration.x.file_max_size_mb.megabytes } validates :estimated_size, presence: true validates :file_present, inclusion: { in: [true, false] } diff --git a/app/models/tiny_mce_asset.rb b/app/models/tiny_mce_asset.rb index d975a3f03..b8e966d62 100644 --- a/app/models/tiny_mce_asset.rb +++ b/app/models/tiny_mce_asset.rb @@ -18,7 +18,7 @@ class TinyMceAsset < ApplicationRecord validates_attachment :image, presence: true, size: { - less_than: Constants::FILE_MAX_SIZE_MB.megabytes + less_than: Rails.configuration.x.file_max_size_mb.megabytes } validates :estimated_size, presence: true diff --git a/app/services/import_repository/parse_repository.rb b/app/services/import_repository/parse_repository.rb index 8fd410f12..24604da63 100644 --- a/app/services/import_repository/parse_repository.rb +++ b/app/services/import_repository/parse_repository.rb @@ -21,7 +21,7 @@ module ImportRepository end def too_large? - @file.size > Constants::FILE_MAX_SIZE_MB.megabytes + @file.size > Rails.configuration.x.file_max_size_mb.megabytes end def has_too_many_rows? diff --git a/app/views/protocols/protocolsio_import_create.js.erb b/app/views/protocols/protocolsio_import_create.js.erb index ce19440a6..f9371dff3 100644 --- a/app/views/protocols/protocolsio_import_create.js.erb +++ b/app/views/protocols/protocolsio_import_create.js.erb @@ -4,7 +4,7 @@ $('#modal-import-json-protocol').on('hidden.bs.modal', function () { <% if @protocolsio_too_big %> $('#modal-import-json-protocol').modal('hide'); HelperModule.flashAlertMsg(' <%= t('my_modules.protocols.load_from_file_size_error', - size: Constants::FILE_MAX_SIZE_MB ) %>','danger'); + size: Rails.configuration.x.file_max_size_mb ) %>','danger'); <% elsif @protocolsio_invalid_file %> $('#modal-import-json-protocol').modal('hide'); HelperModule.flashAlertMsg(' <%= t('my_modules.protocols.load_from_file_invalid_error') %>','danger'); diff --git a/config/application.rb b/config/application.rb index 9f665e861..901fb0806 100644 --- a/config/application.rb +++ b/config/application.rb @@ -26,6 +26,9 @@ module Scinote config.active_job.queue_adapter = :delayed_job + # Max uploaded file size in MB + config.x.file_max_size_mb = (ENV['FILE_MAX_SIZE_MB'] || 50).to_i + # Logging config.log_formatter = proc do |severity, datetime, progname, msg| "[#{datetime}] #{severity}: #{msg}\n" diff --git a/config/initializers/constants.rb b/config/initializers/constants.rb index f7577b42c..67f0496da 100644 --- a/config/initializers/constants.rb +++ b/config/initializers/constants.rb @@ -64,8 +64,6 @@ class Constants # Max table JSON size in MB TABLE_JSON_MAX_SIZE_MB = 20 - # Max uploaded file size in MB - FILE_MAX_SIZE_MB = 50 # Max uploaded user picture avatar size in MB AVATAR_MAX_SIZE_MB = 0.2