Passed
Push — master ( 1b6984...937909 )
by Eduardo
02:28
created

$(ꞌ#bwEntryButtonFileꞌ).click   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
dl 0
loc 3
rs 10
nop 0
1
/** global: appSettings */
2
var whois = require('../../common/whoiswrapper.js'),
3
  conversions = require('../../common/conversions.js'),
4
  fs = require('fs'),
5
  bwFileContents;
6
7
8
require('../../common/stringformat.js');
9
10
const {
11
  ipcRenderer
12
} = require('electron');
13
14
const {
15
  tableReset
16
} = require('./auxiliary.js');
17
18
// File input, path and information confirmation container
19 View Code Duplication
ipcRenderer.on('bw:fileinput.confirmation', function(event, filePath = null, isDragDrop = false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
20
  const {
21
    misc,
22
    lookup
23
  } = appSettings;
24
  var bwFileStats; // File stats, size, last changed, etc
25
26
  //console.log(filePath);
27
  if (filePath === undefined || filePath == '' || filePath === null) {
28
    //console.log(filePath);
29
    $('#bwFileinputloading').addClass('is-hidden');
30
    $('#bwEntry').removeClass('is-hidden');
31
  } else {
32
    $('#bwLoadingInfo').text('Loading file stats...');
33
    if (isDragDrop === true) {
34
      $('#bwEntry').addClass('is-hidden');
35
      $('#bwFileinputloading').removeClass('is-hidden');
36
      bwFileStats = fs.statSync(filePath);
37
      bwFileStats['filename'] = filePath.replace(/^.*[\\\/]/, '');
38
      bwFileStats['humansize'] = conversions.byteToHumanFileSize(bwFileStats['size'], misc.usestandardsize);
39
      $('#bwFileSpanInfo').text('Loading file contents...');
40
      bwFileContents = fs.readFileSync(filePath);
41
    } else {
42
      bwFileStats = fs.statSync(filePath[0]);
43
      bwFileStats['filename'] = filePath[0].replace(/^.*[\\\/]/, '');
44
      bwFileStats['humansize'] = conversions.byteToHumanFileSize(bwFileStats['size'], misc.usestandardsize);
45
      $('#bwFileSpanInfo').text('Loading file contents...');
46
      bwFileContents = fs.readFileSync(filePath[0]);
47
    }
48
    $('#bwFileSpanInfo').text('Getting line count...');
49
    bwFileStats['linecount'] = bwFileContents.toString().split('\n').length;
50
51
    if (lookup.randomize.timebetween === true) {
52
      bwFileStats['minestimate'] = conversions.msToHumanTime(bwFileStats['linecount'] * lookup.randomize.timebetweenmin);
53
      bwFileStats['maxestimate'] = conversions.msToHumanTime(bwFileStats['linecount'] * lookup.randomize.timebetweenmax);
54
55
      $('#bwFileSpanTimebetweenmin').text('{0}ms '.format(lookup.randomize.timebetweenmin));
56
      $('#bwFileSpanTimebetweenmax').text('/ {0}ms'.format(lookup.randomize.timebetweenmax));
57
      $('#bwFileTdEstimate').text('{0} to {1}'.format(bwFileStats['minestimate'], bwFileStats['maxestimate']));
58
    } else {
59
      bwFileStats['minestimate'] = conversions.msToHumanTime(bwFileStats['linecount'] * lookup.timebetween);
60
      $('#bwFileSpanTimebetweenminmax').addClass('is-hidden');
61
      $('#bwFileSpanTimebetweenmin').text(lookup.timebetween + 'ms');
62
      $('#bwFileTdEstimate').text('> {0}'.format(bwFileStats['minestimate']));
63
    }
64
65
66
67
    bwFileStats['filepreview'] = bwFileContents.toString().substring(0, 50);
68
    //console.log(readLines(filePath[0]));
69
    //console.log(bwFileStats['filepreview']);
70
71
    //console.log(lineCount(bwFileContents));
72
    $('#bwFileinputloading').addClass('is-hidden');
73
    $('#bwFileinputconfirm').removeClass('is-hidden');
74
75
    // stats
76
    $('#bwFileTdName').text(bwFileStats['filename']);
77
    $('#bwFileTdLastmodified').text(bwFileStats['mtime']);
78
    $('#bwFileTdLastaccess').text(bwFileStats['atime']);
79
    $('#bwFileTdFileSize').text(bwFileStats['humansize'] + ' ({0} line(s))'.format(bwFileStats['linecount']));
80
    $('#bwFileTdFilepreview').text(bwFileStats['filepreview'] + '...');
81
    //$('#bwTableMaxEstimate').text(bwFileStats['maxestimate']);
82
    //console.log('cont:'+ bwFileContents);
83
84
    //console.log(bwFileStats['linecount']);
85
  }
86
});
87
88
// File Input, Entry container button
89
$('#bwEntryButtonFile').click(function() {
90
  $('#bwEntry').addClass('is-hidden');
91
  $.when($('#bwFileinputloading').removeClass('is-hidden').delay(10)).done(() => {
92
    ipcRenderer.send("bw:input.file");
93
  });
94
});
95
96
// File Input, cancel file confirmation
97
$('#bwFileButtonCancel').click(function() {
98
  $('#bwFileinputconfirm').addClass('is-hidden');
99
  $('#bwEntry').removeClass('is-hidden');
100
});
101
102
// File Input, proceed to bulk whois
103
$('#bwFileButtonConfirm').click(function() {
104
  var bwDomainArray = bwFileContents.toString().split('\n').map(Function.prototype.call, String.prototype.trim);
105
  var bwTldsArray = $('#bwFileInputTlds').val().toString().split(',');
106
107
  tableReset(bwDomainArray.length, bwTldsArray.length);
108
  $('#bwFileinputconfirm').addClass('is-hidden');
109
  $('#bwProcessing').removeClass('is-hidden');
110
111
  ipcRenderer.send("bw:lookup", bwDomainArray, bwTldsArray);
112
});
113
114
// Bulk whois file input by drag and drop
115
(function() {
116
  var holder = $('#bwMainContainer');
117
  holder.ondragover = function() {
118
    return false;
119
  };
120
121
  holder.ondragleave = function() {
122
    return false;
123
  };
124
125
  holder.ondragend = function() {
126
    return false;
127
  };
128
129
  holder.ondrop = function(event) {
130
    event.preventDefault();
131
    for (let f of event.dataTransfer.files) {
132
      ipcRenderer.send('File(s) you dragged here: {0}'.format(f.path));
133
      ipcRenderer.send('ondragstart', f.path);
134
    }
135
    return false;
136
  };
137
})();
138
139
/*
140
$("html").on("dragover", function(event) {
141
    event.preventDefault();
142
    event.stopPropagation();
143
  console.log('dragging');
144
});
145
146
$("html").on("dragleave", function(event) {
147
    event.preventDefault();
148
    event.stopPropagation();
149
    console.log('dragging');
150
});
151
152
$("html").on("drop", function(event) {
153
    event.preventDefault();
154
    event.stopPropagation();
155
    alert("Dropped!");
156
});*/
157
158
$('#bwMainContainer').on('drop', function(event) {
159
  event.preventDefault();
160
  for (let f of event.dataTransfer.files) {
161
    ipcRenderer.send('File(s) you dragged here: {0}'.format(f.path));
162
    ipcRenderer.send('ondragstart', f.path);
163
  }
164
  return false;
165
});
166
167
$('#bwFileInputTlds').keyup(function(event) {
168
  // Cancel the default action, if needed
169
  event.preventDefault();
170
  // Number 13 is the "Enter" key on the keyboard
171
  if (event.keyCode === 13) {
172
    // Trigger the button element with a click
173
    $('#bwFileButtonConfirm').click();
174
  }
175
});
176