diff --git a/app/controllers/assets_controller.rb b/app/controllers/assets_controller.rb index 4e6a9e527..baf0f83d2 100644 --- a/app/controllers/assets_controller.rb +++ b/app/controllers/assets_controller.rb @@ -191,11 +191,25 @@ class AssetsController < ApplicationController render_403 && return unless %w(docx xlsx pptx).include?(params[:file_type]) # Asset validation + original_filename = "#{params[:file_name]}.#{params[:file_type]}" file = Paperclip.io_adapters.for(StringIO.new) - file.original_filename = "#{params[:file_name]}.#{params[:file_type]}" + file.original_filename = original_filename file.content_type = wopi_content_type(params[:file_type]) asset = Asset.new(file: file, created_by: current_user, file_present: true) + # Filename length validation (this cannot be checked by Paperclip, + # as it depends on OS) + if original_filename.length > Constants::FILENAME_MAX_LENGTH + render json: { + message: { + file: I18n.t( + 'assets.create_wopi_file.errors.file_name_too_long', + limit: Constants::FILENAME_MAX_LENGTH + ) + } + }, status: 400 and return + end + unless asset.valid?(:wopi_file_creation) render json: { message: asset.errors diff --git a/config/initializers/constants.rb b/config/initializers/constants.rb index 02aafe179..a019b23d9 100644 --- a/config/initializers/constants.rb +++ b/config/initializers/constants.rb @@ -23,6 +23,9 @@ class Constants COLOR_MAX_LENGTH = 7 # Max characters for text in dropdown list element DROPDOWN_TEXT_MAX_LENGTH = 15 + # Max characters limit for (on most operating systems, it's ~255 characters, + # but this is with a bit more safety margin) + FILENAME_MAX_LENGTH = 100 # Max characters for filenames, after which they get truncated FILENAME_TRUNCATION_LENGTH = 50 # Max characters for names of exported files and folders, after which they get diff --git a/config/locales/en.yml b/config/locales/en.yml index bc1fad71c..d2224cbeb 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1915,6 +1915,7 @@ en: errors: forbidden: 'You do not have permission to add files.' not_found: 'Element not found.' + file_name_too_long: 'is too long (maximum is %{limit} characters, with extension)' atwho: no_results: "No results found" users: