Passed
Push — master ( b5b832...6c55c5 )
by Eduardo
02:11
created

ipcRenderer.bwa:fileinput.confirmation   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 67
Code Lines 46

Duplication

Lines 67
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 46
c 1
b 0
f 0
nc 5
dl 67
loc 67
rs 7.8339
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/** global: appSettings */
2
var whois = require('../../common/whoiswrapper.js'),
3
  conversions = require('../../common/conversions.js'),
4
  fs = require('fs'),
5
  bwaFileContents;
0 ignored issues
show
Unused Code introduced by
The variable bwaFileContents seems to be never used. Consider removing it.
Loading history...
6
7
8
require('../../common/stringformat.js');
9
10
const {
11
  ipcRenderer
12
} = require('electron');
13
14
// File input, path and information confirmation container
15 View Code Duplication
ipcRenderer.on('bwa: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...
16
  const {
17
    misc,
18
    lookup
19
  } = appSettings;
20
  var bwFileStats; // File stats, size, last changed, etc
21
22
  //console.log(filePath);
23
  if (filePath === undefined || filePath == '' || filePath === null) {
24
    //console.log(filePath);
25
    $('#bwaFileInputLoading').addClass('is-hidden');
26
    $('#bwaEntry').removeClass('is-hidden');
27
  } else {
28
    $('#bwLoadingInfo').text('Loading file stats...');
29
    if (isDragDrop === true) {
30
      $('#bwEntry').addClass('is-hidden');
31
      $('#bwFileInputLoading').removeClass('is-hidden');
32
      bwFileStats = fs.statSync(filePath);
33
      bwFileStats['filename'] = filePath.replace(/^.*[\\\/]/, '');
34
      bwFileStats['humansize'] = conversions.byteToHumanFileSize(bwFileStats['size'], misc.usestandardsize);
35
      $('#bwfLoadingInfo').text('Loading file contents...');
36
      bwFileContents = fs.readFileSync(filePath);
0 ignored issues
show
Bug introduced by
The variable bwFileContents seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.bwFileContents.
Loading history...
37
    } else {
38
      bwFileStats = fs.statSync(filePath[0]);
39
      bwFileStats['filename'] = filePath[0].replace(/^.*[\\\/]/, '');
40
      bwFileStats['humansize'] = conversions.byteToHumanFileSize(bwFileStats['size'], misc.usestandardsize);
41
      $('#bwfLoadingInfo').text('Loading file contents...');
42
      bwFileContents = fs.readFileSync(filePath[0]);
43
    }
44
    $('#bwfLoadingInfo').text('Getting line count...');
45
    bwFileStats['linecount'] = bwFileContents.toString().split('\n').length;
46
47
    if (lookup.randomize.timebetween === true) {
48
      bwFileStats['minestimate'] = conversions.msToHumanTime(bwFileStats['linecount'] * lookup.randomize.timebetweenmin);
49
      bwFileStats['maxestimate'] = conversions.msToHumanTime(bwFileStats['linecount'] * lookup.randomize.timebetweenmax);
50
      $('#bwftimebetweenmin').text('{0}ms '.format(lookup.randomize.timebetweenmin));
51
      $('#bwftimebetweenmax').text('/ {0}ms'.format(lookup.randomize.timebetweenmax));
52
      $('#bwfTableMinMaxEstimate').text('{0} to {1}'.format(bwFileStats['minestimate'], bwFileStats['maxestimate']));
53
    } else {
54
      bwFileStats['minestimate'] = conversions.msToHumanTime(bwFileStats['linecount'] * lookup.timebetween);
55
      $('#bwftimebetweenmaxtext').addClass('is-hidden');
56
      $('#bwftimebetweenmin').text(lookup.timebetween + 'ms');
57
      $('#bwfTableMinMaxEstimate').text('> {0}'.format(bwFileStats['minestimate']));
58
    }
59
60
61
62
    bwFileStats['filepreview'] = bwFileContents.toString().substring(0, 50);
63
    //console.log(readLines(filePath[0]));
64
    //console.log(bwFileStats['filepreview']);
65
66
    //console.log(lineCount(bwFileContents));
67
    $('#bwFileInputLoading').addClass('is-hidden');
68
    $('#bwFileInputConfirm').removeClass('is-hidden');
69
70
    // stats
71
    $('#bwfTableFilename').text(bwFileStats['filename']);
72
    $('#bwfTableFileLastModified').text(bwFileStats['mtime']);
73
    $('#bwfTableFileLastAccessed').text(bwFileStats['atime']);
74
    $('#bwfTableFileSize').text(bwFileStats['humansize'] + ' ({0} line(s))'.format(bwFileStats['linecount']));
75
    $('#bwfTableFilePreview').text(bwFileStats['filepreview'] + '...');
76
    //$('#bwTableMaxEstimate').text(bwFileStats['maxestimate']);
77
    //console.log('cont:'+ bwFileContents);
78
79
    //console.log(bwFileStats['linecount']);
80
  }
81
});
82
83
// File Input, Entry container button
84
$('#bwaeButtonFileInput').click(function() {
85
  $('#bwaEntry').addClass('is-hidden');
86
  $.when($('#bwaFileInputLoading').removeClass('is-hidden').delay(10)).done(() => {
87
    ipcRenderer.send("bwa:input.file");
88
  });
89
});
90
91
/*
92
// File Input, cancel file confirmation
93
$('#bwafButtonCancel').click(function() {
94
  $('#bwFileInputConfirm').addClass('is-hidden');
95
  $('#bwEntry').removeClass('is-hidden');
96
});
97
98
// File Input, proceed to bulk whois
99
$('#bwafButtonConfirm').click(function() {
100
  var bwDomainArray = bwFileContents.toString().split('\n').map(Function.prototype.call, String.prototype.trim);
101
  var bwTldsArray = $('#bwfSearchTlds').val().toString().split(',');
102
103
  $('#bwFileInputConfirm').addClass('is-hidden');
104
  $('#bwProcessing').removeClass('is-hidden');
105
106
  ipcRenderer.send("bulkwhois:lookup", bwDomainArray, bwTldsArray);
107
});
108
109
// Bulk whois file input by drag and drop
110
(function() {
111
  var holder = document.getElementById('bwaMainContainer');
112
  holder.ondragover = function() {
113
    return false;
114
  };
115
116
  holder.ondragleave = function() {
117
    return false;
118
  };
119
120
  holder.ondragend = function() {
121
    return false;
122
  };
123
124
  holder.ondrop = function(event) {
125
    event.preventDefault();
126
    for (let f of event.dataTransfer.files) {
127
      ipcRenderer.send('File(s) you dragged here: {0}'.format(f.path));
128
      ipcRenderer.send('ondragstart', f.path);
129
    }
130
    return false;
131
  };
132
})();
133
134
// Enter when confirming file input bulk whois
135
document.getElementById('bwfSearchTlds').addEventListener("keyup", function(event) {
136
  // Cancel the default action, if needed
137
  event.preventDefault();
138
  // Number 13 is the "Enter" key on the keyboard
139
  if (event.keyCode === 13) {
140
    // Trigger the button element with a click
141
    $('#bwfButtonConfirm').click();
142
  }
143
});
144
*/
145