-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
181 lines (134 loc) · 4.35 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
const express = require('express'),
bodyParser = require('body-parser'),
fs = require('fs'),
path = require('path'),
client = require('./db.js'),
cookieParser = require("cookie-parser");
const app = express();
let users = false;
app.set('views', path.join(__dirname, '/public'));
app.use(bodyParser.json({limit: '10mb', extended: true}))
.use(cookieParser());
app.get('/', (req, res) => {
if (req.cookies.id) res.redirect('/viewer');
else res.sendFile('index.html', {root : __dirname + '/public/pages'});
});
app.use(express.static(path.join(__dirname , '/public')));
app.get('/viewer', (req, res) => {
if (req.cookies.id){
return res.sendFile('viewer.html', {root : __dirname + '/public/pages'});
}
else res.redirect('/');
});
app.get('/dashboard', (req, res) => {
res.sendFile('dashboard.html', {root : __dirname + '/public/pages'});
});
app.get('/calendar', (req, res) => {
res.sendFile('calendar.html', {root : __dirname + '/public/pages'});
});
app.get('/analytics', (req, res) => {
res.sendFile('analytics.html', {root : __dirname + '/public/pages'});
});
app.get('/settings', (req, res) => {
res.sendFile('settings.html', {root : __dirname + '/public/pages'});
});
app.get('/about', (req, res) => {
res.sendFile('about.html', {root : __dirname + '/public/pages'});
});
app.get('/monitor', (req, res) => {
res.sendFile('monitor.html', {root : __dirname + '/public/pages'});
});
app.get('/logout', (req, res) => {
if (req.cookies.id) res.clearCookie('id');
res.redirect('/');
});
///////////////////////// POST ROUTES /////////////////////////
app.post('/login', async (req, res) => {
let id = req.body.id;
if (!users) users = await client.db('BayHax').collection('Users');
if (await docExists(id)){
res.cookie('id', id, {httpOnly: true});
res.send({success: '/viewer'});
}
else{
console.log('error', id);
res.send({error: "doc not found"})
};
});
app.post('/image', async (req, res) => {
let id = req.cookies.id;
let imageId = req.body.id;
if (!users) users = await client.db('BayHax').collection('Users');
if (await docExists(id)){
let query = {};
query['images.' + imageId] = 1;
let doc = await users.findOne({_id: id}, {projection: query});
res.send({image: doc.images[imageId]});
}
else{
console.log('error', id);
res.send({error: "doc not found"})
};
});
app.post('/get-data', async (req, res) => {
var id = req.cookies['id'];
if (!id) return res.send({error: 'no id'});
if (!users) users = await client.db('BayHax').collection('Users');
var options = {projection: {}};
options.projection[req.body.part] = 1;
var doc = await users.findOne({_id: id}, options);
if (!doc) return res.send({error: 'invalid id'});
res.send(doc);
});
app.post('/update', (req, res) => {
let {setting, change} = req.body;
let id = req.cookies.id;
res.send(JSON.stringify({message: "received"}));
if (!id) return;
let update = {$set: {}};
update['$set']['settings.' + setting] = change;
users.updateOne({_id: id}, update);
});
app.post('/rpi', async (req, res) => {
try{
let {mood, picture, id, date, time} = req.body;
if (!docExists(id)) return res.send({error: "user does not exist"});
var options = {projection: {}};
options.projection[req.body.part] = 1;
var doc = await users.findOne({_id: id});
res.send(doc.settings);
// alter db
if (mood){
let months = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'];
let dateparts = date.split('/');
let year = parseInt(dateparts[0]);
let month = months[parseInt(dateparts[1])-1];
let day = parseInt(dateparts[2]);
let path = 'moods.' + year + '.' + month + '.' + day;
let entry = time + ',' + mood;
if (picture){
let newId = Math.floor(Math.random()*Math.pow(10, 15));
entry += ',' + newId;
let image = 'data:image/jpeg;base64, ' + picture;
let up = {$set: {}};
up['$set']['images.' + newId] = image;
users.updateOne({_id: id}, up);
}
let update = {$push: {}};
update['$push'][path] = {};
update['$push'][path]['$each'] = [entry];
users.updateOne({_id: id}, update);
}
}
catch(e){
console.log(e);
res.send({error: "server crashed"});
}
});
app.listen(3000, () => {
console.log('server started');
});
async function docExists(id){
let exists = await users.find({_id: id}).limit(1).count();
return exists > 0;
}