Passed
Push — master ( a2212a...a2212a )
by Matthias
01:47
created

js/utils.js   A

Complexity

Total Complexity 29
Complexity/F 5.8

Size

Lines of Code 220
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 29
eloc 139
mnd 24
bc 24
fnc 5
dl 0
loc 220
rs 10
bpm 4.8
cpm 5.8
noi 1
c 0
b 0
f 0
1
/**
2
 * @copyright Copyright (c) 2018 Matthias Held <[email protected]>
3
 *
4
 * @author Matthias Held <[email protected]>
5
 *
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
/** global: OC */
23
 (function() {
24
25
     /**
26
      * @class OCA.RansomwareDetection.FileList
27
      */
28
     var Utils = function() {
29
     };
30
31
     /**
32
      * @memberof OCA.RansomwareDetection
33
      */
34
     Utils.prototype = {
35
         colors: {red: 'red', yellow: 'yellow', green: 'green'},
36
         colorsText: {red: 'red-text', yellow: 'yellow-text', green: 'green-text'},
37
38
         /**
39
          * Creates a new row in the table.
40
          */
41
         _createRow: function(fileData) {
42
             var self = this;
43
             var td, tr = $('<tr data-id="' + fileData.id + '" data-sequence="' + fileData.sequence + '"></tr>'),
44
                 mtime = parseInt(fileData.timestamp, 10) * 1000,
45
                 basename, extension, simpleSize, filename;
46
47
             if (isNaN(mtime)) {
48
     			mtime = new Date().getTime();
49
     		}
50
51
             td = $('<td class="selection"></td>');
52
             td.append(
53
 				'<input id="select-' + this.id + '-' + fileData.id +
54
 				'" type="checkbox" class="selectCheckBox checkbox"/><label for="select-' + this.id + '-' + fileData.id + '">' +
55
 				'<span class="hidden-visually">' + t('ransomware_detection', 'Select') + '</span>' +
56
 				'</label>'
57
 			);
58
             tr.append(td);
59
60
             var nameWrapper = $('<div class="name-wrapper"></div>');
61
             nameWrapper.append('<div class="thumbnail-wrapper"><div class="thumbnail" style="background-image:url(' + OC.MimeType.getIconUrl(fileData.type) + ');"></div></div>');
62
63
             // file name
64
             filename = fileData.originalName;
65
             if (fileData.command === 2) {
66
                 // file was renamed use new name
67
                 filename = fileData.newName
68
             }
69
70
             var nameSpan, innernameSpan;
71
72
             if (filename !== null) {
73
                 if (fileData.type === 'file') {
74
                     if (filename.indexOf('.') === 0) {
75
         				 basename = '';
76
         				 extension = filename;
77
                     } else {
78
                         basename = filename.substr(0, filename.lastIndexOf('.'));
79
         				 extension = filename.substr(filename.lastIndexOf('.'));
80
                     }
81
82
                     nameSpan = $('<span></span>').addClass('name-text');
83
         			 innernameSpan = $('<span></span>').addClass('inner-name-text').text(basename);
84
85
                     nameSpan.append(innernameSpan);
86
87
                     if (extension) {
88
         				nameSpan.append($('<span></span>').addClass('extension').text(extension));
89
         			}
90
                } else {
91
                    nameSpan = $('<span></span>').addClass('name-text');
92
                    innernameSpan = $('<span></span>').addClass('inner-name-text').text(filename);
93
94
                    nameSpan.append(innernameSpan);
95
                }
96
             } else {
97
                nameSpan = $('<span></span>').addClass('name-text');
98
                innernameSpan = $('<span></span>').addClass('inner-name-text').text(t('ransomware_detection', 'Not found.'));
99
100
                nameSpan.append(innernameSpan);
101
            }
102
103
            nameWrapper.append(nameSpan);
104
105
             td = $('<td class="file-name"></td>');
106
             td.append(nameWrapper);
107
             tr.append(td);
108
109
             if (fileData.command === 1) {
110
                 // delete
111
                 td = $('<td></td>').append($('<p></p>').attr({"title": "DELETE"}).tooltip({placement: 'top'}).prepend('<span class="fas fa-trash-alt fa-fw"></span>'));
112
             } else if (fileData.command === 2) {
113
                 // rename
114
                     td = $('<td></td>').append($('<p></p>').attr({"title": "RENAME"}).tooltip({placement: 'top'}).prepend('<span class="fas fa-font fa-fw"></span>'));
115
             } else if (fileData.command === 3) {
116
                 // write
117
                 td = $('<td></td>').append($('<p></p>').attr({"title": "WRITE"}).tooltip({placement: 'top'}).prepend('<span class="fas fa-pencil-alt fa-fw"></span>'));
118
             } else if (fileData.command === 4) {
119
                 // read
120
                 td = $('<td></td>').append($('<p></p>').attr({"title": "READ"}).tooltip({placement: 'top'}).prepend('<span class="fas fa-book fa-fw"></span>'));
121
             } else if (fileData.command === 5) {
122
                 // create
123
                 td = $('<td></td>').append($('<p></p>').attr({"title": "CREATE"}).tooltip({placement: 'top'}).prepend('<span class="fas fa-plus-circle fa-fw"></span>'));
124
             } else {
125
                 // error
126
                 td = $('<td></td>').append($('<p></p>').attr({"title": "ERROR"}).tooltip({placement: 'top'}).prepend('<span class="fas fa-times fa-fw"></span>'));
127
             }
128
             tr.append(td);
129
130
             // size
131
             if (typeof(fileData.size) !== 'undefined' && fileData.size >= 0) {
132
                 simpleSize = filesize(parseInt(fileData.size, 10), true);
133
             } else {
134
                 simpleSize = t('ransomware_detection', 'Pending');
135
             }
136
137
             td = $('<td></td>').append($('<p></p>').attr({
138
 				"class": "filesize"
139
 			}).text(simpleSize));
140
             tr.append(td);
141
142
             if (fileData.fileClass === 1) {
143
                 // encrypted
144
                 td = $('<td></td>').append($('<p></p>').attr({"title": "ENCRYPTED"}).tooltip({placement: 'top'}).prepend('<span class="fas fa-lock fa-fw"></span>'));
145
             } else if (fileData.fileClass === 2) {
146
                 // compressed
147
                     td = $('<td></td>').append($('<p></p>').attr({"title": "COMPRESSED"}).tooltip({placement: 'top'}).prepend('<span class="fas fa-file-archive fa-fw"></span>'));
148
             } else if (fileData.fileClass === 3) {
149
                 // normal
150
                 td = $('<td></td>').append($('<p></p>').attr({"title": "NORMAL"}).tooltip({placement: 'top'}).prepend('<span class="fas fa-file fa-fw"></span>'));
151
             } else {
152
                 // error
153
                 td = $('<td></td>').append($('<p></p>').attr({"title": "ERROR"}).tooltip({placement: 'top'}).prepend('<span class="fas fa-times fa-fw"></span>'));
154
             }
155
             tr.append(td);
156
157
             if (fileData.fileExtensionClass === 0) {
158
                 // normal
159
                 td = $('<td></td>').append($('<p></p>').attr({"title": "NOT SUSPICIOUS"}).tooltip({placement: 'top'}).prepend('<span class="fas fa-check-circle fa-fw"></span>'));
160
             } else if (fileData.fileExtensionClass === 1) {
161
                 // suspicious
162
                 td = $('<td></td>').append($('<p></p>').attr({"title": "SUSPICIOUS"}).tooltip({placement: 'top'}).prepend('<span class="fas fa-exclamation-triangle fa-fw"></span>'));
163
             } else {
164
                 // error
165
                 td = $('<td></td>').append($('<p></p>').attr({"title": "ERROR"}).tooltip({placement: 'top'}).prepend('<span class="fas fa-times fa-fw"></span>'));
166
             }
167
             tr.append(td);
168
169
             // date column (1000 milliseconds to seconds, 60 seconds, 60 minutes, 24 hours)
170
 			// difference in days multiplied by 5 - brightest shade for files older than 32 days (160/5)
171
 			var modifiedColor = Math.round(((new Date()).getTime() - mtime )/1000/60/60/24*5 );
172
 			// ensure that the brightest color is still readable
173
 			if (modifiedColor >= '160') {
174
 				modifiedColor = 160;
175
 			}
176
             var formatted;
177
 			var text;
178
 			if (mtime > 0) {
179
 				formatted = OC.Util.formatDate(mtime);
180
 				text = OC.Util.relativeModifiedDate(mtime);
181
 			} else {
182
 				formatted = t('ransomware_detection', 'Unable to determine date');
183
 				text = '?';
184
 			}
185
186
             td = $('<td></td>').attr({ "class": "date" });
187
             td.append($('<span></span>').attr({
188
 				"class": "modified live-relative-timestamp",
189
 				"title": formatted,
190
 				"data-timestamp": mtime,
191
 				"style": 'color:rgb('+modifiedColor+','+modifiedColor+','+modifiedColor+')'
192
 			}).text(text)
193
 			  .tooltip({placement: 'top'})
194
 			);
195
             tr.append(td);
196
197
             // Color row according to suspicion level
198
             if (fileData.suspicionClass === 3) {
199
                 tr.attr({ 'class': self.colors.red});
200
             } else if (fileData.suspicionClass === 2) {
201
                 tr.attr({ 'class': self.colors.yellow});
202
             } else if (fileData.suspicionClass === 1) {
203
                 tr.attr({ 'class': self.colors.green});
204
             }
205
206
             return tr;
207
         },
208
209
         /**
210
          * Creates a new table skeleton.
211
          */
212
         _createTableSkeleton: function(sequence, suspicionScore) {
213
             var color = this.colors.green;
214
             if (suspicionScore > 4) {
215
                 color = this.colors.red;
216
             } else if (suspicionScore > 2) {
217
                 color = this.colors.yellow;
218
             }
219
             var table =
220
                 $('<div class="row">' +
221
                     '<div class="sequence-color"><div class="color-box ' + color + '"></div></div>' +
222
                     '<div class="sequence-table"><table class="ransomware-files" data-sequence="' + sequence + '"><thead>' +
223
                     '<th><input type="checkbox" data-sequence="' + sequence + '" id="select_all_files_' + sequence + '" class="select-all checkbox"/>' +
224
                     '<label for="select_all_files_' + sequence + '"><span class="hidden-visually">' + t('ransomware_detection', 'Select all') + '</span></label></th>' +
225
                     '<th><a class="column-title name">' + t('ransomware_detection', 'Name') + '</a></th>' +
226
                     '<th><a class="column-title hide-selected"><p>' + t('ransomware_detection', 'Operation') + '</p></a></th>' +
227
                     '<th><a class="column-title hide-selected"><p>' + t('ransomware_detection', 'Size') + '</p></a></th>' +
228
                     '<th><a class="column-title hide-selected"><p>' + t('ransomware_detection', 'File class') + '</p></a></th>' +
229
                     '<th><a class="column-title hide-selected"><p>' + t('ransomware_detection', 'File name class') + '</p></a></th>' +
230
                     '<th class="controls"><a class="column-title detected">' + t('ransomware_detection', 'Time') + '</a><span class="column-title selected-actions"><a class="recover-selected" data-sequence="' + sequence + '"><span class="icon icon-history"></span><span>' + t('ransomware_detection', 'Recover') + '</span></a></span></th> ' +
231
                     '</thead><tbody class="file-list"></tbody><tfoot></tfoot></table></div>' +
232
                 '</div>');
233
             return table;
234
         },
235
     };
236
237
     OCA.RansomwareDetection.Utils = Utils;
0 ignored issues
show
Bug introduced by
The variable OCA seems to be never declared. If this is a global, consider adding a /** global: OCA */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
238
 })();
239
240
 $(document).ready(function() {
241
242
 });
243