-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
358 lines (316 loc) · 11.6 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
const server = "http://185.47.13.9:5000/";
const fileFormat = /^(txt|pdf|xls|docx|xlsx|xlsm|csv)$/;
const urlRegex = /^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/;
let id = ""
let idUrl = ""
let fileType = ""
let fileTypeUrl = ""
$(document).ready(function () {
$("#spinner").hide();
$("#spinner_web").hide();
$("#getDocumentButton").hide();
$("#getUrlButton").hide();
function getTypeOfPersonalData() {
/**
* Gets the data type from the checkbox
* returns String
*/
if ($('#names').is(":checked") && $('#idCards').is(":checked")) {
return "all";
}
if($('#names').is(":checked")) return "names";
if($('#idCards').is(":checked")) return "idCards";
return null;
}
function getUrlServer(personalData) {
/**
* Obtains the type of operation to be carried out
* return String
*/
let url = "";
switch ($("#sel").val()) {
case "1":
url = server+'/search/file/extract-data/json-file?personalData='+personalData;
break;
case "2":
url = server+'/search/file/encode?personalData='+personalData;
break;
case "3":
url = server+'/search/file/extract-data/csv?personalData='+personalData;
break;
case "4":
url = server+'/search/file/disintegration?personalData='+personalData;
break;
case "5":
url = server+'/search/file/obfuscation?personalData='+personalData;
break;
}
return url;
}
$(".custom-file-input").on("change", function() {
// Analyzes the file path and displays only its name in the form
let fileName = $(this).val().split("\\").pop();
$(this).siblings(".custom-file-label").addClass("selected").html(fileName);
});
function createModalError(jqXHR,exception) {
/**
* Creates a window to display the error
*/
error = getError(jqXHR,exception);
$('#titleModalError').html("OOps!!, algo salio mal ⛔️ ");
$('.modal-body').html(
"<p>" + error.msg + "</p>" +
"<p>Código de Error: " + error.code + "</p>"
);
$('#modalError').modal('show');
}
$("#getDocumentButton").on('click', function () {
/**
* Send an ajax request to download the file just created on the server.
* Due to type conflicts, if the file is json, its request must be evaluated separately
*/
if (fileType == "json") {
$.ajax({
url: server+'/search/file/download?id='+id,
type: 'GET',
success: function(data){
let a = document.createElement('a');
let url = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(data));
a.href = url;
a.download = 'doc.json';
document.body.append(a);
a.click();
a.remove();
$('.file_upload').prop('disabled', false);
$("#getDocumentButton").hide();
},
error: function(jqXHR,exception) {
alert(getError(jqXHR,exception))
$('.file_upload').prop('disabled', false);
$("#getDocumentButton").hide();
}
});
} else {
$.ajax({
url: server+'/search/file/download?id='+id,
type: 'GET',
xhrFields: {
responseType: 'blob'
},
success: function(data){
let a = document.createElement('a');
let url = window.URL.createObjectURL(data);
a.href = url;
a.download = 'doc.' + fileType;
document.body.append(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url);
$('.file_upload').prop('disabled', false);
$("#getDocumentButton").hide();
},
error: function(jqXHR,exception) {
createModalError(jqXHR,exception);
$('.file_upload').prop('disabled', false);
$("#getDocumentButton").hide();
}
});
}
});
$("#getUrlButton").on('click', function () {
/**
* Send an ajax request to download the html file just created on the server.
* Due to type conflicts, if the file is json, csv or html, its request must be evaluated separately
*/
if (fileTypeUrl == "json") {
$.ajax({
url: server+'/search/file/download?id='+idUrl,
type: 'GET',
success: function(data){
let a = document.createElement('a');
let url = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(data));
a.href = url;
a.download = 'doc.json';
document.body.append(a);
a.click();
a.remove();
$('#html_upload').prop('disabled', false);
$("#getUrlButton").hide();
},
error: function(jqXHR,exception) {
createModalError(jqXHR,exception);
$('#html_upload').prop('disabled', false);
$("#getUrlButton").hide();
}
});
} else if(fileTypeUrl == "csv"){
$.ajax({
url: server+'/search/file/download?id='+idUrl,
type: 'GET',
xhrFields: {
responseType: 'blob'
},
success: function(data){
let a = document.createElement('a');
let url = window.URL.createObjectURL(data);
a.href = url;
a.download = 'doc.' + fileTypeUrl;
document.body.append(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url);
$('#html_upload').prop('disabled', false);
$("#getUrlButton").hide();
},
error: function(jqXHR,exception) {
createModalError(jqXHR,exception);
$('#html_upload').prop('disabled', false);
$("#getUrlButton").hide();
}
});
} else {
$.ajax({
url: server+'/search/file/download?id='+idUrl,
type: 'GET',
success: function(data){
let a = document.createElement('a');
let url = "data:text/html;charset=utf-8," + encodeURIComponent(data);
a.href = url;
a.download = 'doc.html';
document.body.append(a);
a.click();
a.remove();
$('#html_upload').prop('disabled', false);
$("#getUrlButton").hide();
},
error: function(jqXHR,exception) {
createModalError(jqXHR,exception);
$('#html_upload').prop('disabled', false);
$("#getUrlButton").hide();
}
});
}
});
$("#formId").submit(function(e) {
// Send an ajax request to analyze the sent document.
e.preventDefault();
if (!checkFileExtension()) {
return;
}
let personalData = getTypeOfPersonalData();
if (personalData == null) {
$('#alert').text("* Elige uno de los tipos de datos presentados");
return;
} else {
$('#alert').text("");
}
$("#spinner").show();
$('.file_upload').prop('disabled', true);
let url = getUrlServer(personalData);
let formData = new FormData(this);
$.ajax({
type: "POST",
url: url,
data: formData,
success: function(data)
{
id = data.id;
fileType = data.fileType;
$("#getDocumentButton").show();
$("#spinner").hide();
},
error: function(jqXHR,exception){
createModalError(jqXHR,exception);
$("#spinner").hide();
$('.file_upload').prop('disabled', false);
},
cache: false,
contentType: false,
processData: false
});
});
$("#form_web").submit(function(e) {
// Send an ajax request to analyze the html document sent.
e.preventDefault();
if (!validateUrlForm()) {
return;
}
let personalData = getTypeOfPersonalData();
if (personalData == null) {
$('#alert').text("* Elige uno de los tipos de datos presentados");
return;
} else {
$('#alert').text("");
}
$("#spinner_web").show();
$('#html_upload').prop('disabled', true);
let url = server + '/search/file/operation-web?url=' +
$("#url_web").val() + "&op=" + $("#op").val() + "&personalData=" + personalData;
$.ajax({
type: "GET",
url: url,
success: function(data)
{
idUrl = data.id;
fileTypeUrl = data.fileType;
$("#getUrlButton").show();
$("#spinner_web").hide();
},
error: function(jqXHR,exception){
createModalError(jqXHR,exception);
$("#spinner_web").hide();
$('#html_upload').prop('disabled', false);
}
});
});
});
function validateUrlForm() {
/**
* Validate the form to send web pages
*/
let web = $("#url_web").val();
if (web.match(urlRegex)) {
$('#url_web').removeClass('error');
return true;
} else {
$('#url_web').addClass('error');
return false;
}
}
function checkFileExtension() {
/**
* Analyzes the extension of the file
*/
let ext = $("#customFile").val().split('.').pop();
if (ext.match(fileFormat)) {
$('#alert').text("");
return true;
} else {
$('#alert').text("* Solo estan permitidos los siguientes formatos: txt, pdf, xls, docx, xlsx, xlsm y csv");
return false;
}
}
function getError(jqXHR,exception){
/**
* Analyzes the error in the request
* returns error code and message
*/
let error = {"code":-1, "msg":""} ;
if (jqXHR.status === 0) {
error.msg = 'No estas concetado al servidor.\nVerifica tu conexión.';
} else if (jqXHR.status == 404) {
error.msg = 'Petición no procesada. [404]';
} else if (jqXHR.status == 500) {
error.msg = 'Error en servidor interno [500].';
} else if (exception === 'parsererror') {
error.msg = 'El análisis JSON solicitado ha fallado.';
} else if (exception === 'timeout') {
error.msg = 'Error de tiempo de espera.';
} else if (exception === 'abort') {
error.msg = 'Solicitud abortada.';
} else {
error.msg = 'Error no detectado.Vuelva ha intentarlo.';
}
error.code = jqXHR.status;
return error;
}