-
Notifications
You must be signed in to change notification settings - Fork 2
/
fileIO.js
166 lines (127 loc) · 5.28 KB
/
fileIO.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
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
var fs = require('fs');
var crypto = require('crypto')
const { log } = require('./logger')
var jsonfile = require('jsonfile');
const config = jsonfile.readFileSync('./config.json')
class fileIO{
//Method for creating directories
static makeDirectory(path){
if(fs.existsSync(path) === false){
fs.mkdirSync(`${path}`, function(err){
if(err){
//Log error if occurs
log.error("Error generating directory")
throw err;
}
});
}
}
//Method for writing to files
static writeFile(path, text){
//
//Append user log on Non-Blocking stream
var stream = fs.createWriteStream(path,{'flags':'a+'})
stream.once('open', (fd) =>{
stream.write(`${text}\n`)
//Always close stream when done
stream.end();
});
}
//Method for encryption
static encryptText(text){
//Cast algo and pass
const algorithm = config.encryption.algorithm;
const password = config.encryption.password;
const iv_length = 16;
//Generate encryption object
let iv = crypto.randomBytes(iv_length);
var cipher = crypto.createCipheriv(algorithm,password,iv);
//Encrypt text
let crypted = cipher.update(text,'utf8','hex');
var result = [];
result += crypted;
//Return encrypted text
return result;
}
//Method for decryption
static decryptText(text){
//Cast algo and pass
const algorithm = config.encryption.algorithm;
const password = config.encryption.password;
const iv_length = 16;
//Generate decryption Object
let iv = crypto.randomBytes(iv_length);
var cipher = crypto.createDecipheriv(algorithm,password,iv);
//Decrypt text
let decrypted = cipher.update(text, 'hex', 'utf8');
var result = [];
result += decrypted;
//Return decrypted text
return result;
}
//Method for building embedded database
static buildDB(classNumber, teamNumber){
const tableObject = {};
const messageObject = {"messages":[]};
let profilePath = `Resources/Classes/${classNumber}/profiles.json`;
let messagePath = `Resources/Classes/${classNumber}/messages.json`;
let voteTablePath = `Resources/Classes/${classNumber}/Teams/${teamNumber}/votes.json`;
let minutesTablePath = `Resources/Classes/${classNumber}/Teams/${teamNumber}/minutes.json`;
fileIO.makeDirectory(`Resources/Classes/${classNumber}`);
fileIO.makeDirectory(`Resources/Classes/${classNumber}/Teams/`);
fileIO.makeDirectory(`Resources/Classes/${classNumber}/Teams/${teamNumber}/`);
if(fs.existsSync(profilePath) === false){
jsonfile.writeFileSync(profilePath, tableObject, {flag: 'w'});
}
if(fs.existsSync(messagePath) === false){
jsonfile.writeFileSync(messagePath, messageObject, {flag: 'w'});
}
if(fs.existsSync(voteTablePath) === false){
jsonfile.writeFileSync(voteTablePath, tableObject, {flag: 'w'});
}
if(fs.existsSync(minutesTablePath) === false){
jsonfile.writeFileSync(minutesTablePath, tableObject, {flag: 'w'});
}
}
//Method for inserting profile into table
static insertProfile(profile){
let profilePath = `Resources/Classes/${profile.class}/profiles.json`;
var contents = jsonfile.readFileSync(profilePath);
contents[profile.name] = profile;
jsonfile.writeFileSync(profilePath, contents, {flags:'w'});
}
//Method for setting dialog id for a channel
static setDialog(channel, dialogId){
let dialogIdPath = 'dialogID.json';
let dialogObject = {"dialogID":dialogId};
var contents = jsonfile.readFileSync(dialogIdPath);
contents[`${channel}`] = dialogObject;
jsonfile.writeFileSync(dialogIdPath, contents, {flags:'w'});
}
//Method for checking dialogID in between prompts
static checkDialog(channel, userID){
let contents = jsonfile.readFileSync('dialogID.json');
let currentID = contents[`${channel}`].dialogID;
if(currentID != userID){
return false
} else{
return true
}
}
//Method for logging context data
static logContext(turnContext,user){
let channelID = turnContext.activity.channelId;
let clientID = turnContext.activity.channelData.clientActivityId;
let time = turnContext.activity.timestamp;
let text = turnContext.activity.text;
let name = turnContext.activity.from.name;
let userID = turnContext.activity.from.id;
let messagePath = `Resources/Classes/${user.class}/messages.json`;
var contents = jsonfile.readFileSync(messagePath);
//Rebuild Data
var data = { "time": time, "channelID": channelID, "clientID": clientID, "text": text, "user": name, "userID": userID, "team": user.team, "nick": user.nick };
contents.messages.push(data);
jsonfile.writeFileSync(messagePath, contents, {flags:'w'});
}
}
module.exports.fileIO = fileIO;