Skip to content
Navigation Menu
{{ message }}
forked from PhantomBot/PhantomBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileSystem.js
More file actions
345 lines (310 loc) · 9.09 KB
/
Copy pathfileSystem.js
File metadata and controls
345 lines (310 loc) · 9.09 KB
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
/*
* Copyright (C) 2016-2021 phantombot.github.io/PhantomBot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* fileSystem.js
*
* Export general file management to th $ API
*/
(function() {
var JFile = java.io.File,
JFileInputStream = java.io.FileInputStream,
JFileOutputStream = java.io.FileOutputStream,
Paths = Packages.java.nio.file.Paths,
executionPath = Packages.tv.phantombot.PhantomBot.GetExecutionPath(),
fileHandles = [],
validPaths = [
'./addons',
'./config/audio-hooks',
'./config/gif-alerts',
'./logs',
'./scripts'
];
/**
* @function readFile
* @export $
* @param {string} path
* @returns {Array}
*/
function readFile(path) {
var lines = [];
if (!fileExists(path)) {
return lines;
}
if (invalidLocation(path)) {
$.consoleLn('Blocked readFile() target outside of validPaths:' + path);
return lines;
}
try {
var fis = new JFileInputStream(path),
scan = new java.util.Scanner(fis);
for (var i = 0; scan.hasNextLine(); ++i) {
lines[i] = scan.nextLine();
}
fis.close();
} catch (e) {
$.log.error('Failed to open \'' + path + '\': ' + e);
}
return lines;
}
/**
* @function mkDir
* @export $
* @param {string} path
* @returns {boolean}
*/
function mkDir(path) {
if (invalidLocation(path)){
$.consoleLn('Blocked mkDir() target outside of validPaths:' + path);
return false;
}
var dir = new JFile(path);
return dir.mkdir();
}
/**
* @function moveFile
* @export $
* @param {string} file
* @param {string} path
*/
function moveFile(file, path) {
var fileO = new JFile(file),
pathO = new JFile(path);
if (invalidLocation(file) || invalidLocation(path)) {
$.consoleLn('Blocked moveFile() source or target outside of validPaths:' + file + ' to ' + path);
return;
}
if ((fileO != null && pathO != null) || (file != "" && path != "")) {
try {
org.apache.commons.io.FileUtils.moveFileToDirectory(fileO, pathO, true);
} catch (ex) {
$.log.error("moveFile(" + file + ", " + path + ") failed: " + ex);
}
}
}
/**
* @function saveArray
* @export $
* @param {Array} array
* @param {string} path
* @param {boolean} append
*/
function saveArray(array, path, append) {
if (invalidLocation(path)) {
$.consoleLn('Blocked saveArray() target outside of validPaths:' + path);
return;
}
try {
var fos = new JFileOutputStream(path, append),
ps = new java.io.PrintStream(fos),
l = array.length;
for (var i = 0; i < l; ++i) {
ps.println(array[i]);
}
fos.close();
} catch (e) {
$.log.error('Failed to write to \'' + path + '\': ' + e);
}
}
/**
* @function closeOpenFiles
*/
function closeOpenFiles() {
var dateFormat = new java.text.SimpleDateFormat('MM-dd-yyyy'),
date = dateFormat.format(new java.util.Date());
for (var key in fileHandles) {
if (!fileHandles[key].startDate.equals(date)) {
fileHandles[key].fos.close();
delete fileHandles[key];
}
}
}
/**
* @function writeToFile
* @export $
* @param {string} line
* @param {string} path
* @param {boolean} append
*/
function writeToFile(line, path, append) {
var dateFormat = new java.text.SimpleDateFormat('MM-dd-yyyy'),
date = dateFormat.format(new java.util.Date()),
fos,
ps;
if (invalidLocation(path)){
$.consoleLn('Blocked writeToFile() target outside of validPaths:' + path);
return;
}
closeOpenFiles();
if (fileHandles[path] !== undefined && append) {
fos = fileHandles[path].fos;
ps = fileHandles[path].ps;
} else {
fos = new JFileOutputStream(path, append);
ps = new java.io.PrintStream(fos);
fileHandles[path] = {
fos: fos,
ps: ps,
startDate: date
};
}
try {
ps.println(line);
fos.flush();
} catch (e) {
$.log.error('Failed to write to \'' + path + '\': ' + e);
}
}
/**
* @function touchFile
* @export $
* @param {string} path
*/
function touchFile(path) {
if (invalidLocation(path)) {
$.consoleLn('Blocked touchFile() target outside of validPaths:' + path);
return;
}
try {
var fos = new JFileOutputStream(path, true);
fos.close();
} catch (e) {
$.log.error('Failed to touch \'' + path + '\': ' + e);
}
}
/**
* @function deleteFile
* @export $
* @param {string} path
* @param {boolean} now
*/
function deleteFile(path, now) {
if (invalidLocation(path)) {
$.consoleLn('Blocked deleteFile() target outside of validPaths:' + path);
return;
}
try {
var f = new JFile(path);
if (now) {
f['delete']();
} else {
f.deleteOnExit();
}
} catch (e) {
$.log.error('Failed to delete \'' + path + '\': ' + e);
}
}
/**
* @function fileExists
* @export $
* @param {string} path
* @returns {boolean}
*/
function fileExists(path) {
if (invalidLocation(path)) {
$.consoleLn('Blocked fileExists() target outside of validPaths:' + path);
return false;
}
try {
var f = new JFile(path);
return f.exists();
} catch (e) {
return false;
}
}
/**
* @function findFiles
* @export $
* @param {string} directory
* @param {string} pattern
* @returns {Array}
*/
function findFiles(directory, pattern) {
if (invalidLocation(directory)) {
$.consoleLn('Blocked findFiles() target outside of validPaths:' + directory);
return [];
}
try {
var f = new JFile(directory),
ret = [];
if (f.isDirectory()) {
var files = f.list();
for (var i = 0; i < files.length; i++) {
if (files[i].indexOf(pattern) != -1) {
ret.push(files[i]);
}
}
return ret;
}
} catch (e) {
$.log.error('Failed to search in \'' + directory + '\': ' + e);
}
return [];
}
/**
* @function isDirectory
* @export $
* @param {string} path
* @returns {boolean}
*/
function isDirectory(path) {
if (invalidLocation(path)) {
$.consoleLn('Blocked isDirectory() target outside of validPaths:' + path);
return false;
}
try {
var f = new JFile(path);
return f.isDirectory();
} catch (e) {
return false;
}
}
/**
* @function findSize
* @export $
* @param {string} file
* @returns {Number}
*/
function findSize(file) {
if (invalidLocation(file)) {
$.consoleLn('Blocked findSize() target outside of validPaths:' + file);
return 0;
}
var fileO = new JFile(file);
return fileO.length();
}
function invalidLocation(path) {
var p = Paths.get(path);
for (var x in validPaths) {
if (p.toAbsolutePath().startsWith(Paths.get(executionPath, validPaths[x]))) {
return false;
}
}
return true;
}
/** Export functions to API */
$.deleteFile = deleteFile;
$.fileExists = fileExists;
$.findFiles = findFiles;
$.findSize = findSize;
$.isDirectory = isDirectory;
$.mkDir = mkDir;
$.moveFile = moveFile;
$.readFile = readFile;
$.saveArray = saveArray;
$.touchFile = touchFile;
$.writeToFile = writeToFile;
})();
You can’t perform that action at this time.
