Skip to content

Commit c8a6696

Browse files
author
Gaurav SinghaRoy
committed
showing an error message now for generic upload
1 parent 50ad685 commit c8a6696

3 files changed

Lines changed: 53 additions & 19 deletions

File tree

app/assets/javascripts/resources/common.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ function get_random_int(min, max) {
7474
return Math.floor(Math.random() * (max - min + 1)) + min;
7575
}
7676

77+
function get_file_extension(file_name){
78+
return file_name.split(".")[file_name.split(".").length-1];
79+
}
80+
7781
String.prototype.splice = function( idx, rem, s ) {
7882
return (this.slice(0,idx) + s + this.slice(idx + Math.abs(rem)));
7983
};

app/assets/javascripts/resources/querybuilder.js

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -611,41 +611,67 @@ QueryBuilder = {
611611
},
612612
convert : {
613613
configured : {
614+
check_validity_of_file_content : function(file_data){
615+
var result = { valid : true, description: ""};
616+
return result;
617+
},
618+
handle_error_output : function(valid,error_description){
619+
if(valid == true){
620+
$(".configured-download-file-ok").show("fast");
621+
}
622+
else{
623+
$("#configured_download_error_message").html(error_description);
624+
$(".configured-download-file-error").show("fast");
625+
}
626+
},
614627
handle_file_upload : function(evt) {
615-
var files = evt.target.files; // FileList object
616-
617-
// files is a FileList of File objects. List some properties.
618-
var output = [];
619-
for (var i = 0, f; f = files[i]; i++) {
628+
$(".configured-download-file-ok").hide();
629+
$(".configured-download-file-error").hide();
630+
var files = evt.target.files; // FileList object
631+
var valid_file = false;
632+
var error_description = "";
633+
var output = [];
634+
var f = files[0];
620635

621-
output.push('<li><strong>', escape(f.name), '</strong> (', f.type || 'n/a', ') - ',
636+
if(get_file_extension(f.name) != "txt"){
637+
QueryBuilder.convert.configured.handle_error_output(false,"The file uploaded is not <strong>.txt</strong> file");
638+
}
639+
else{
640+
output.push('<li><strong>', escape(f.name), '</strong> (', f.type || 'n/a', ') - ',
622641
f.size, ' bytes, last modified: ',
623642
f.lastModifiedDate ? f.lastModifiedDate.toLocaleDateString() : 'n/a',
624643
'</li>');
625644
var reader = new FileReader();
626645
var file_data = "";
627646
reader.onload = function(e){
628-
var blocks = QueryBuilder.convert.configured.get_string_blocks(reader.result);
629-
configured_convert.header = QueryBuilder.convert.configured.get_block_string_from_blocks(blocks,"header");
630-
configured_convert.body = QueryBuilder.convert.configured.get_block_string_from_blocks(blocks,"body");
631-
configured_convert.footer = QueryBuilder.convert.configured.get_block_string_from_blocks(blocks,"footer");
632-
str_variable_dictionary = QueryBuilder.convert.configured.get_block_string_from_blocks(blocks,"variable_dictionary");
633-
arr_variable_dictionary = str_variable_dictionary.split("\n");
634-
configured_convert.variable_dictionary = [];
635-
for(i=0;i<arr_variable_dictionary.length;i++){
636-
arr_var = arr_variable_dictionary[i].split("=");
637-
if(arr_var.length == 2){
638-
configured_convert.variable_dictionary.push({variable : arr_var[0].trim(), value: arr_var[1].trim()});
647+
var file_validity = QueryBuilder.convert.configured.check_validity_of_file_content(reader.result);
648+
if(file_validity.valid == true){
649+
var blocks = QueryBuilder.convert.configured.get_string_blocks(reader.result);
650+
configured_convert.header = QueryBuilder.convert.configured.get_block_string_from_blocks(blocks,"header");
651+
configured_convert.body = QueryBuilder.convert.configured.get_block_string_from_blocks(blocks,"body");
652+
configured_convert.footer = QueryBuilder.convert.configured.get_block_string_from_blocks(blocks,"footer");
653+
str_variable_dictionary = QueryBuilder.convert.configured.get_block_string_from_blocks(blocks,"variable_dictionary");
654+
arr_variable_dictionary = str_variable_dictionary.split("\n");
655+
configured_convert.variable_dictionary = [];
656+
for(i=0;i<arr_variable_dictionary.length;i++){
657+
arr_var = arr_variable_dictionary[i].split("=");
658+
if(arr_var.length == 2){
659+
configured_convert.variable_dictionary.push({variable : arr_var[0].trim(), value: arr_var[1].trim()});
660+
}
639661
}
640662
}
663+
QueryBuilder.convert.configured.handle_error_output(file_validity.valid,file_validity.description);
641664
};
642665
reader.readAsText(f);
643-
}
666+
}
667+
644668

645669
//document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>';
646-
$(".configured-download-file-ok").show("fast");
670+
647671

648672
},
673+
674+
649675

650676
get_block_string_from_blocks : function(blocks, block_type){
651677
var result = "";
@@ -711,6 +737,7 @@ QueryBuilder = {
711737
$("#btn_group_download").hide("fast");
712738
$("#btn_download_configured_convert_template").attr("href","/query/configured_convert_template?selected_properties="+QueryBuilder.properties.get_checked_properties());
713739
$(".configured-download-file-ok").hide();
740+
$(".configured-download-file-error").hide();
714741
},
715742
hide_download : function(motion){
716743
if(motion != undefined && motion != ""){

app/views/query/_search_results.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
<input type="file" id="configured_template_files" name="files[]" multiple class="btn btn-warning btn-xs col-md-12"/>
6666
<output id="list"></output>
6767
</p>
68+
<p class="configured-download-file-error">
69+
<span class="badge alert-danger"><span class='glyphicon glyphicon-remove'></span></span>&nbsp;<span id="configured_download_error_message"></span>
70+
</p>
6871
<p class="configured-download-file-ok">
6972
<span class="badge alert-success"><span class='glyphicon glyphicon-ok'></span></span> The uploaded template file is a valid template. You can now proceed to download your final serialization output :)
7073
</p>

0 commit comments

Comments
 (0)