Merge pull request #876 from Zanz2/valid_format_1791

Made validations for JSON format and .txt and .json files [SCI-1791]
This commit is contained in:
Zanz2 2017-11-14 19:12:16 +01:00 committed by GitHub
commit 6cd2c6f518
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 1 deletions

View file

@ -598,7 +598,16 @@ class ProtocolsController < ApplicationController
def protocolsio_import_create
@protocolsio_too_big = false
@protocolsio_invalid_file = false
extension = File.extname(params[:json_file].path)
file_size = File.size(params[:json_file].path)
if extension != '.txt' && extension != '.json'
@protocolsio_invalid_file = true
respond_to do |format|
format.js {}
end
return 0 # return 0 stops the rest of the controller code from executing
end
if file_size / 1000 > Constants::FILE_MAX_SIZE_MB
@protocolsio_too_big = true
respond_to do |format|
@ -607,11 +616,19 @@ class ProtocolsController < ApplicationController
# named the same as this controller
# where a javascript alert is called
end
return 0 # return 0 stops the rest of the controller code from executing
end
json_file_contents = File.read(params[:json_file].path)
json_file_contents.gsub! '\"', "'"
# escaped double quotes too stressfull, html works with single quotes too
# json double quotes dont get escaped since they dont match \"
unless valid_protocol_json(json_file_contents)
@protocolsio_invalid_file = true
respond_to do |format|
format.js {}
end
return 0 # return 0 stops the rest of the controller code from executing
end
@json_object = JSON.parse(json_file_contents)
@protocol = Protocol.new
respond_to do |format|
@ -917,6 +934,13 @@ class ProtocolsController < ApplicationController
private
def valid_protocol_json(json)
JSON.parse(json)
return true
rescue JSON::ParserError => e
return false
end
# pio_stp_x means protocols io step (id of component) parser
def pio_stp_1(iterating_key) # protocols io description parser
br = '<br>'

View file

@ -3,6 +3,9 @@
$('#modal-import-json-protocol').modal('hide');
HelperModule.flashAlertMsg(' <%= t('my_modules.protocols.load_from_file_size_error',
size: Constants::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');
<% else %>
$('#modal-import-json-protocol').modal('hide');
<% if remotipart_submitted? %> <%# a workaround to a bug with remotipart, that caused alot of headache, courtesy of github.com/dhampik %>

View file

@ -603,6 +603,7 @@ en:
load_from_file_error: "Failed to load the protocol from file."
load_from_file_error_locked: "Failed to load the protocol from file. One or more files are currently being edited."
load_from_file_size_error: "Failed to load the protocol from file. Limit is %{size}Mb."
load_from_file_invalid_error: "The file you provided is invalid or has an invalid extension."
load_from_file_protocol_general_error: "Failed to load the protocol from file. It is likely that certain fields (protocol and individual step titles and names) contain too many or too few characters.(max is %{max} and min is %{min})"
results:
head_title: "%{project} | %{module} | Results"