Add file name validation to create new Office file modal window

This commit is contained in:
Luka Murn 2019-05-10 18:18:26 +02:00
parent 584cf4b93b
commit 890df8803e
3 changed files with 19 additions and 1 deletions

View file

@ -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

View file

@ -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

View file

@ -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: