mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-02-06 23:15:34 +08:00
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:
commit
6cd2c6f518
3 changed files with 29 additions and 1 deletions
|
@ -598,7 +598,16 @@ class ProtocolsController < ApplicationController
|
||||||
|
|
||||||
def protocolsio_import_create
|
def protocolsio_import_create
|
||||||
@protocolsio_too_big = false
|
@protocolsio_too_big = false
|
||||||
|
@protocolsio_invalid_file = false
|
||||||
|
extension = File.extname(params[:json_file].path)
|
||||||
file_size = File.size(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
|
if file_size / 1000 > Constants::FILE_MAX_SIZE_MB
|
||||||
@protocolsio_too_big = true
|
@protocolsio_too_big = true
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
|
@ -607,11 +616,19 @@ class ProtocolsController < ApplicationController
|
||||||
# named the same as this controller
|
# named the same as this controller
|
||||||
# where a javascript alert is called
|
# where a javascript alert is called
|
||||||
end
|
end
|
||||||
|
return 0 # return 0 stops the rest of the controller code from executing
|
||||||
end
|
end
|
||||||
json_file_contents = File.read(params[:json_file].path)
|
json_file_contents = File.read(params[:json_file].path)
|
||||||
json_file_contents.gsub! '\"', "'"
|
json_file_contents.gsub! '\"', "'"
|
||||||
# escaped double quotes too stressfull, html works with single quotes too
|
# escaped double quotes too stressfull, html works with single quotes too
|
||||||
# json double quotes dont get escaped since they dont match \"
|
# 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)
|
@json_object = JSON.parse(json_file_contents)
|
||||||
@protocol = Protocol.new
|
@protocol = Protocol.new
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
|
@ -917,6 +934,13 @@ class ProtocolsController < ApplicationController
|
||||||
|
|
||||||
private
|
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
|
# pio_stp_x means protocols io step (id of component) parser
|
||||||
def pio_stp_1(iterating_key) # protocols io description parser
|
def pio_stp_1(iterating_key) # protocols io description parser
|
||||||
br = '<br>'
|
br = '<br>'
|
||||||
|
|
|
@ -3,6 +3,9 @@
|
||||||
$('#modal-import-json-protocol').modal('hide');
|
$('#modal-import-json-protocol').modal('hide');
|
||||||
HelperModule.flashAlertMsg(' <%= t('my_modules.protocols.load_from_file_size_error',
|
HelperModule.flashAlertMsg(' <%= t('my_modules.protocols.load_from_file_size_error',
|
||||||
size: Constants::FILE_MAX_SIZE_MB ) %>','danger');
|
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 %>
|
<% else %>
|
||||||
$('#modal-import-json-protocol').modal('hide');
|
$('#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 %>
|
<% if remotipart_submitted? %> <%# a workaround to a bug with remotipart, that caused alot of headache, courtesy of github.com/dhampik %>
|
||||||
|
|
|
@ -603,6 +603,7 @@ en:
|
||||||
load_from_file_error: "Failed to load the protocol from file."
|
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_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_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})"
|
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:
|
results:
|
||||||
head_title: "%{project} | %{module} | Results"
|
head_title: "%{project} | %{module} | Results"
|
||||||
|
|
Loading…
Reference in a new issue