/
utils.js
136 lines (123 loc) · 2.23 KB
/
utils.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
'use strict';
const byteUnits = [ 'kB','MB','GB','TB','PB','EB','ZB','YB' ];
const priorityAutoId = 100;
const priorityEnum = [
{
id: priorityAutoId,
name: 'Auto',
}, {
id: 0,
name: 'Paused (forced)',
}, {
id: 1,
name: 'Paused',
}, {
id: 2,
name: 'Lowest',
}, {
id: 3,
name: 'Low',
}, {
id: 4,
name: 'Normal',
}, {
id: 5,
name: 'High',
}, {
id: 6,
name: 'Highest',
}
];
const fileTypeEnum = [
{
id: 'any',
name: 'Any',
}, {
id: 'directory',
name: 'Directory',
}, {
id: 'file',
name: 'File',
}, {
id: 'audio',
name: 'Audio',
}, {
id: 'compressed',
name: 'Compressed',
}, {
id: 'document',
name: 'Document',
}, {
id: 'executable',
name: 'Executable',
}, {
id: 'picture',
name: 'Picture',
}, {
id: 'video',
name: 'Video',
}
];
module.exports = {
formatSize: function (fileSizeInBytes) {
const thresh = 1024;
if (Math.abs(fileSizeInBytes) < thresh) {
return fileSizeInBytes + ' B';
}
let u = -1;
do {
fileSizeInBytes /= thresh;
++u;
} while (Math.abs(fileSizeInBytes) >= thresh && u < byteUnits.length - 1);
return fileSizeInBytes.toFixed(2) + ' ' + byteUnits[u];
},
// Works only for directories
getLastDirectory: function (fullPath) {
const result = fullPath.match(/([^\\\/]+)[\\\/]$/);
return result ? result[1] : fullPath;
},
sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
},
parseSearchQuery(item) {
return {
pattern: item.pattern,
extensions: item.extensions.split(';'),
file_type: item.file_type,
min_size: item.min_size * 1024 * 1024,
};
},
searchQueryDefinitions: [
{
key: 'pattern',
title: 'Search string',
default_value: '',
type: 'string',
optional: true,
}, {
key: 'extensions',
title: 'File extensions',
default_value: '',
type: 'string',
help: 'Separate extensions with ; (example: exe;iso;img)',
optional: true,
}, {
key: 'file_type',
title: 'File type',
default_value: 'any',
type: 'string',
options: fileTypeEnum,
}, {
key: 'min_size',
title: 'Minimum size (MiB)',
default_value: 0,
type: 'number',
}
],
toApiPriority(id) {
return id === priorityAutoId ? null : id;
},
fileTypeEnum,
priorityEnum,
priorityAutoId,
};