Merge pull request #6430 from CartoDB/6421-check_in2csv_input

Raise exception when CSV file with XLS extension is detected
pull/6517/head
Carla 9 years ago
commit 6914c14b77

@ -96,6 +96,7 @@ WORKING_SPECS_2 = \
services/importer/spec/unit/csv_normalizer_spec.rb \
services/importer/spec/unit/shp_normalizer_spec.rb \
services/importer/spec/unit/shp_helper_spec.rb \
services/importer/spec/unit/excel2csv_spec.rb \
services/importer/spec/unit/downloader_spec.rb \
services/importer/spec/unit/georeferencer_spec.rb \
services/importer/spec/unit/json2csv_spec.rb \

@ -32,13 +32,12 @@ module CartoDB
job.log "Converting #{@format.upcase} to CSV"
Open3.popen3("file -b --mime-type #{filepath}") do |stdin, stdout, stderr, process|
@file_mime_type = stdout.read.delete("\n")
file_mime_type = stdout.read.delete("\n")
job.log "Can't get the mime type of the file" unless process.value.to_s =~ /exit 0/
# CSV files with XLS extensions are considered malformed files
raise CartoDB::Importer2::MalformedXLSException.new if file_mime_type == "text/plain"
end
# Take into account that here should come or csv files with xls extensions or xls documents
file_format = (@file_mime_type == "text/plain") ? "-f csv" : ""
in2csv_command_line = %Q[in2csv #{filepath} #{file_format} | #{in2csv_warning_filter} | #{newline_remover_path} > #{converted_filepath}]
in2csv_command_line = %Q[in2csv #{filepath} | #{in2csv_warning_filter} | #{newline_remover_path} > #{converted_filepath}]
job.log "About to execute in2csv: " + in2csv_command_line
Open3.popen3(in2csv_command_line) do |stdin, stdout, stderr, process|
raise CartoDB::Importer2::MalformedXLSException.new if stderr.read =~ /Unsupported format, or corrupt file:/

@ -31,17 +31,17 @@ describe CartoDB::Importer2::Excel2Csv do
@excel2csv.stubs(:converted_filepath).returns("/tmp")
@excel2csv.run
end
it "if a csv file is passed as xls should parse it" do
it "raise if a csv file is passed as xls" do
filepath = path_to('csv_as_xls.xls')
@excel2csv = CartoDB::Importer2::Excel2Csv.new("xls", filepath, @job, @csv_normalizer)
@excel2csv.stubs(:converted_filepath).returns("/tmp")
@excel2csv.run
expect { @excel2csv.run }.to raise_error CartoDB::Importer2::MalformedXLSException
end
it "if a csv file is passed as xlsx should parse it" do
filepath = path_to('csv_as_xls.xlsx')
it "raise if a csv file is passed as xlsx" do
filepath = path_to('csv_as_xlsx.xlsx')
@excel2csv = CartoDB::Importer2::Excel2Csv.new("xlsx", filepath, @job, @csv_normalizer)
@excel2csv.stubs(:converted_filepath).returns("/tmp")
@excel2csv.run
expect { @excel2csv.run }.to raise_error CartoDB::Importer2::MalformedXLSException
end
end
end

Loading…
Cancel
Save