-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
361 lines (336 loc) · 10.3 KB
/
index.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Install body-parser and Express
const express = require('express');
const app = express();
const mongoClient = require('mongodb').MongoClient;
const formidable = require('formidable');
const md5File = require('md5-file');
const fs = require('fs');
const bCrypt = require('bcrypt');
const crypto = require('crypto');
const jimp = require('jimp');
var mongoUrl = "mongodb://localhost:27017/";
var bodyParser = require('body-parser');
// Use req.query to read values!!
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use("/public", express.static("pages/public"));
app.get('/', (req, res) => {
res.sendFile(__dirname + "/pages/index.html")
});
app.get('/certifications', (req, res) => {
res.sendFile(__dirname + "/pages/certifications.html")
});
app.get('/profile', (req, res) => {
res.sendFile(__dirname + "/pages/profile.html")
});
app.get('/edit_profile', (req, res) => {
res.sendFile(__dirname + "/pages/edit_profile.html")
});
app.get('/admin.html', (req, res) => {
res.sendFile(__dirname + "/pages/admin.html")
});
app.get('/sign_in', (req, res) => {
res.sendFile(__dirname + "/pages/sign_in.html")
});
app.get('/sign_up', (req, res) => {
res.sendFile(__dirname + "/pages/sign_up.html")
});
app.get('/test', (req, res) => {
res.sendFile(__dirname + "/pages/test.html")
});
app.get('/course/:course', (req, res) => {
res.sendFile(__dirname + "/pages/course_template.html")
});
app.get('/dataInput', (req, res) => {
res.sendFile(__dirname + "/pages/dataInput.html");
});
app.get('/loadProfile.js', (req, res) => {
res.sendFile(__dirname + "/pages/public/loadProfile.js")
});
app.get('/database.js', (req, res) => {
res.sendFile(__dirname + "/pages/public/database.js")
});
app.get('/logo', (req, res) => {
res.sendFile(__dirname + "/pages/public/logo.png")
});
app.get('/courses.json', (req, res) => {
res.sendFile(__dirname + "/pages/public/courses.json");
})
app.get('/api/getUser', (req, res) => {
mongoClient.connect(mongoUrl, { useNewUrlParser: true }, function (err, db) {
if (err) throw err;
var dbo = db.db("apps4rva");
dbo.collection("users").findOne({ uid: req.query.uid }, function (err, result) {
if (err) throw err;
res.send(result);
db.close();
});
});
})
app.get('/courses/:course', (req, res) => {
fs.readFile("courses.json", (err, json) => {
var courses = JSON.parse(json);
var course = courses.find(obj => {
return obj.jobID == req.params.course;
})
if (course == undefined) {
res.send('404');
} else {
res.sendFile(__dirname + '/pages/course_template.html');
}
});
})
app.get('/courses/:course/data', (req, res) => {
fs.readFile("courses.json", (err, json) => {
var courses = JSON.parse(json);
console.log(req.params.course);
var course = courses.find(obj => {
return obj.jobID == req.params.course;
})
if (course == undefined) {
res.send('404');
} else {
res.send(course);
}
});
})
app.get('/api/getDB', (req, res) => {
mongoClient.connect(mongoUrl, function (err, db) {
if (err) throw err;
var dbo = db.db('apps4rva');
dbo.collection('users').find({}).toArray(function (err, result) {
if (err) throw err;
res.end(JSON.stringify(result));
db.close();
});
});
});
app.get('/api/makeUser', (req, res) => {
mongoClient.connect(mongoUrl, function (err, db) {
if (err) throw err;
var dbo = db.db('apps4rva');
dbo.collection('users').insertOne({tag: req.query.tag, firstName: req.query.firstName, lastName: req.query.lastName, dob: req.query.dob, email: req.query.email, location: req.query.location, study: req.query.study, admin: req.query.admin }, function (err, db) {
if (err) throw err;
});
});
});
app.get('/api/removeUser', (req, res) => {
mongoClient.connect(mongoUrl, function (err, db) {
if (err) throw err;
var dbo = db.db('apps4rva');
dbo.collection('users').deleteOne({ email: req.query.email, tag: parseInt(req.query.tag) }, function (err, db) {
if (err) throw err;
});
});
});
// app.post('/dataInput', (req, res) => {
// var data = {}
// var form = new formidable.IncomingForm();
// form.parse(req)
// .on('field', (name, value) => {
// data[name] = value;
// })
// .on('end', () => {
// fs.appendFile(__dirname + "/courses.json", JSON.stringify(data) + ",\n\t", (err) => {
// if (err) throw err;
// })
// });
// });
app.post('/api/upload/cert', (req, res) => {
var uid;
var files = [];
var form = new formidable.IncomingForm();
try {
form.parse(req)
.on('fileBegin', (name, file) => {
console.log("a file has appeared!")
file.path = __dirname + "/temp/" + file.name;
})
.on('file', (name, file) => {
md5File(file.path, (err, hash) => {
if (err) throw err;
jimp.read(file.path, (err, lenna) => {
try {
lenna.quality(60).write(__dirname + "/uploads/" + hash + ".jpg");
files.push(hash + ".jpg");
} catch {
}
})
})
})
.on('field', (name, value) => {
console.log(name, value);
uid = value;
})
.on('end', () => {
mongoClient.connect(mongoUrl, (err, db) => {
if (err) throw err;
var dbo = db.db("apps4rva");
dbo.collection("users").findOne({ uid: uid }).then((json) => {
var newList = [...new Set(files.concat(json.certs))];
if (json["certs"]) {
dbo.collection("users").updateOne({ uid: uid }, { $set: { certs: newList } });
} else {
dbo.collection("users").updateOne({ uid: uid }, { $set: { certs: files } });
}
res.send("success");
})
})
})
} catch {}
})
app.post('/edit_profile', (req, res) => {
var userData = {};
var pathToDelete;
var form = new formidable.IncomingForm();
form.parse(req)
.on('fileBegin', (name, file) => {
if (file.name != "") {
file.path = __dirname + "/temp/" + file.name;
pathToDelete = file.path;
}
})
.on('file', (name, file) => {
if (file.name != "") {
md5File(file.path, (err, hash) => {
jimp.read(file.path, (err, lenna) => {
if (err) throw err;
var path = __dirname + "/uploads/" + hash + ".jpg";
lenna
.resize(512, 512)
.quality(60)
.write(path);
userData["pfp"] = hash + ".jpg";
fs.unlinkSync(pathToDelete);
})
})
}
})
.on('field', (name, value) => {
userData[name] = value;
})
.on('end', () => {
mongoClient.connect(mongoUrl, { useNewUrlParser: true }, (err, db) => {
if (err) throw err;
var dbo = db.db("apps4rva");
if (userData["pfp"] == undefined) dbo.collection("users").updateOne({ uid: userData["uid"] }, { $set: { firstName: userData["name"].split(" ")[0], lastName: userData["name"].split(" ")[1], dob: userData["dob"], email: userData["email"], location: userData["location"], study: userData["study"] } });
else dbo.collection("users").updateOne({ uid: userData["uid"] }, { $set: { firstName: userData["name"].split(" ")[0], lastName: userData["name"].split(" ")[1], dob: userData["dob"], email: userData["email"], location: userData["location"], study: userData["study"], pfp: userData["pfp"] } });
res.send("success");
})
});
})
app.get("/api/pfp", (req, res) => {
res.sendFile(__dirname + "/uploads/" + req.query.name);
})
async function upload(req) {
return new Promise((resolve, reject) => {
var form = new formidable.IncomingForm();
var filePath;
form.parse(req)
.on('fileBegin', (name, file) => {
file.path = __dirname + "/uploads/" + file.name;
})
.on('file', (name, file) => {
md5File(file.path, (err, hash) => {
if (err) throw err;
filePath = __dirname + "/uploads/" + hash + ".png";
console.log(filePath);
fs.rename(file.path, __dirname + "/uploads/" + hash + ".png", function (err) {
if (err) throw err;
});
});
});
})
}
app.post('/api/make_user', (req, res) => {
var form = new formidable.IncomingForm();
var userData = { "admin": 0, "pfp": "user-placeholder.svg", "certs": null };
form.parse(req)
.on('field', function (name, value) {
userData[name] = value;
})
.on('end', function () {
register(userData);
res.sendFile(__dirname + "/pages/sign_in.html");
})
});
app.post('/api/login', (req, res) => {
var form = new formidable.IncomingForm();
form.parse(req, (err, fields) => {
if (err) throw err;
login(fields["email"], fields["password"]).then((h) => {
res.send(h);
});
})
})
async function login(email, password) {
return new Promise((resolve, reject) => {
mongoClient.connect(mongoUrl, { useNewUrlParser: true }, function (err, db) {
if (err) throw err;
var dbo = db.db("apps4rva");
dbo.collection("users").findOne({ email: email }).then(function (json) {
if (json == null) {
resolve({ status: "No user exists with that email." });
} else {
bCrypt.compare(password, json["password"], (err, result) => {
if (err) throw err;
if (result) {
var token = crypto.randomBytes(32).toString('hex');
dbo.collection("users").updateOne({ _id: json["_id"] }, { $set: { token: token } });
resolve({ status: "logged in", uid: json["uid"], token: token });
} else {
resolve({ status: "Incorrect email or password." });
}
})
}
})
})
});
}
async function register(userData) {
return new Promise((resolve, reject) => {
mongoClient.connect(mongoUrl, { useNewUrlParser: true }, function (err, db) {
if (err) throw err;
var dbo = db.db("apps4rva");
userData["uid"] = crypto.randomBytes(32).toString('hex');
console.log(userData);
bCrypt.hash(userData["password"], 10, (err, hash) => {
if (err) throw err;
userData["password"] = hash;
dbo.collection("users").insertOne(userData);
resolve("User created!");
});
})
})
}
app.get('/api/test', (req, res) => {
mongoClient.connect(mongoUrl, { useNewUrlParser: true }, (err, db) => {
if (err) throw err;
var userData = { name: "Te st" };
var dbo = db.db("apps4rva");
dbo.collection("users").updateOne(
{ uid: "8eb0440b44a35ada09876662f18363497aaea2fc43de3dac33f511c83440246d" },
{
$set:
{
firstName: userData["name"].split(" ")[0],
lastName: userData["name"].split(" ")[1]
}
}
)
res.send('success');
})
});
app.post('/api/test', (req, res) => {
var userData = {};
var form = new formidable.IncomingForm();
form.parse(req)
.on("file", (name, file) => {
console.log(file.path);
res.sendFile(file.path);
})
.on("end", () => {
//res.send(userData);
})
})
app.listen(3000, () => console.log('App listening on port 3000!'));