-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.js
77 lines (73 loc) · 2.26 KB
/
storage.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
var jsonfile = require("jsonfile");
const expor = module.exports = {};
expor.fileScanN = 0;
expor.indexStorage = function () {
const util = require('./vars.js');
console.log("Indexing storage...");
var fs = require('fs');
var dir = './sessions';
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
const mutil = require('./main.js');
fs.readdir(dir, (err, files) => {
console.log("Indexing " + fs);
for(var i = 0; i <files.length; i++){
var file = files[i];
console.log(file);
const jsonFile = require('jsonfile');
jsonfile.readFile("./sessions/" + file, function(err, obj) {
console.log(obj);
if (err) {
return console.log(err);
}
util.configureArray.push(obj);
util.startSocket(obj);
});
}
console.log("Started all json searches.");
function recursive(){
if(expor.fileScanN < files.length && mutil.mainWindow != null){
setTimeout(recursive, 1000);
}
else{
setTimeout(load, 3000);
}
}
function load(){
const url = require('url');
const path = require('path');
mutil.mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}));
}
recursive();
});
};
expor.createSession = function (name, ip, port, password, func) {
const jsonFile = require('jsonfile');
const util = require('./vars.js');
var fs = require('fs');
var dir = "sessions/" + name + ".json";
if(!fs.existsSync(dir)){
fs.openSync(dir, 'a');
}
var json = {'name': name, 'ip': ip, 'port': port, 'password': password};
jsonFile.writeFile(dir, json, function (err) {
if(err){
console.error(err)
}
});
util.configureArray.push(json);
util.startSocket(json);
if(func != undefined) func();
};
expor.deleteSession = function(name, func){
var fs = require('fs');
fs.unlink('sessions/' + name + ".json", function() {
func();
});
};
global.store = expor;