-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
233 lines (204 loc) · 7.15 KB
/
app.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
const express = require('express')
const path = require('path')
const { get } = require('request')
const https = require('https')
const fs = require('fs')
const mysql = require('mysql')
const nodemailer = require('nodemailer')
const bodyParser = require('body-parser')
const nunjucks = require('nunjucks')
const date = require('date-and-time')
const multiparty = require('multiparty')
const session = require('express-session')
const multer = require('multer')
const app = express()
const viewsDir = path.join(__dirname, 'views')
app.use(express.static(viewsDir))
app.use(express.static(path.join(__dirname, './public')))
app.use(express.static(path.join(__dirname, 'weights')))
app.use(express.static(path.join(__dirname, 'dist')))
app.use(express.static(path.join(__dirname, 'layouts')))
//app.use(express.static(path.join(__dirname, 'node_modules/nunjucks/browser')))
const upload = multer();
var htmlEmail = fs.createReadStream('EmailTemplate.html');
app.set('view engine', 'njk');
nunjucks.configure('views', {
autoescape: true,
express: app
});
app.use(bodyParser.json({ limit: '10mb' }));
app.use(bodyParser.urlencoded({ extended: true, limit: '10mb' }));
app.use(session({
secret: 'N12uUF6cVT',
resave: true,
saveUninitialized: true,
secure: false
}))
//Mysql DB setting
//Local Db
localDBSettings = mysql.createConnection({
host: "localhost",
user: "admin",
database: "recognitioncam"
})
//Heroku ClearDb
remoteDBSettings = mysql.createConnection({
host: "eu-cdbr-west-02.cleardb.net",
user: "b5a0621d59583a",
password: "98978855",
database: "heroku_8d1151a458eb2e9"
})
var con = localDBSettings
con.connect(function(err) {
if (err) throw err;
console.log("+++DB Connected!+++");
});
//mail send setting
var transporter = nodemailer.createTransport({
host: "smtp.sendgrid.net",
port: "465",
auth: {
user: "apikey",
pass: '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'
}
});
const port = 3000
var key = fs.readFileSync(__dirname + '/certs/selfsigned.key');
var cert = fs.readFileSync(__dirname + '/certs/selfsigned.crt');
var options = {
key: key,
cert: cert
};
app.get('/', function(req, res) {
console.log(req.session.username)
res.render('landing.njk', { session: req.session, name: 'Main page' });
});
//app.get('/', (req, res) => res.sendFile(path.join(viewsDir, 'index.njk')))
//app.get('/dataset', (req, res) => res.sendFile(path.join(viewsDir, 'dataset.njk')))
app.get('/landing', function(req, res) {
console.log(req.session.username)
res.render('landing.njk', { session: req.session });
});
app.get('/login', function(req, res) {
res.render('login.njk', {});
});
app.get('/admin', function(req, res) {
res.render('admin.njk', {});
});
app.get('/camera', function(req, res) {
res.render('camera.njk', { session: req.session });
});
app.get('/dataset', function(req, res) {
res.render('dataset2.njk', { session: req.session });
});
app.get('/signUp', function(req, res) {
res.render('signUp.njk', {});
});
app.get('/profilo', function(req, res) {
res.render('profilo2.njk', { session: req.session })
})
app.post('/intruder', upload.any(), (req, res) => {
var emailUt
var now = Date();
console.log(now)
con.query("SELECT email FROM user where username= ?", [req.session.username], function(err, result, fields) {
if (err) throw err
emailUt = result[0]
});
console.log("intruso")
var mailOptions = {
from: 'info@recognitioncam.com',
to: emailUt,
subject: now,
text: 'Attenzione! Una persona, con volto non riconosciuto, è appena entrata in casa.',
html: htmlEmail,
attachments: [{
filename: now + ".webm",
content: req.files[0].buffer,
},
{
filename: now + ".png",
content: req.files[1].buffer,
cid: 'image1'
},
]
};
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
console.log(error)
} else {
console.log('Email sent: ' + info.response)
}
});
})
app.post('/aggiornaAccount', (req, res) => {
selectQuery = "UPDATE user SET premium = 1 WHERE user.username = ?"
con.query(selectQuery, [req.session.username], function(err, result, fields) {
console.log("premium!")
req.session.premium = 1
})
res.end('{"success" : "Successfully", "status" : 200}');
})
app.post('/dataset', (req, res) => {
fs.mkdirSync(__dirname + "/public/images/faces/" + req.session.username + "/" + req.body.name);
var i;
for (i = 0; i < 5; i++)
fs.writeFileSync(__dirname + "/public/images/faces/" + req.session.username + "/" + req.body.name + "/" + req.body.name + i + ".png", req.body.images[i].replace(/^data:image\/png;base64,/, ""), { encoding: 'base64' })
selectQuery = "UPDATE user SET numVolti = ? WHERE user.username = ?"
con.query(selectQuery, [req.session.numVolti++, req.session.username], function(err, result, fields) {
console.log(req.session.numVolti)
})
console.log("photo taken!")
})
app.post('/login1', (req, res) => {
var username = req.body.name
var password = req.body.pass
var isEmpty = true
selectQuery = "SELECT * FROM user WHERE username = ? AND password = ?"
console.log(password)
con.query(selectQuery, [username, password], function(err, result, fields) {
if (result[0] != null) {
isEmpty = false;
console.log("+++++++++++++++++++++++++")
console.log(isEmpty)
}
console.log("dwadwad " + result[0]["numVolti"])
req.session.loggato = 1
req.session.username = username
req.session.numVolti = result[0]["numVolti"]
req.session.premium = result[0]["premium"]
req.session.email = result[0]["email"]
console.log("Login Done " + req.session.username)
if (!isEmpty) {
res.end('{"success" : "Successfully", "status" : 200}');
console.log("wafnwjadnjwand")
} else
res.sendStatus(400)
// res.end("error");
})
// con.query(aziendaQuery, [username], function(err, result, fields) {
// req.session.azienda = result[0]
// })
})
app.post('/registration', (req, res) => {
var username = req.body.name
var password = req.body.pass
var email = req.body.email
var user = [username, password, email, 1, 1]
var insertionQuery = "INSERT INTO user(username,password,email,admin,numVolti,premium,plus) VALUES (?,?,?,?,?,?,?)"
con.query(insertionQuery, [username, password, email, 0, 0, 0, 0],
function(err, result, fields) {
// if (err) throw err;
console.log("INSERITO")
console.log(result)
fs.mkdirSync(__dirname + "/public/images/faces/" + username)
res.end('{"success" : "Successfully", "status" : 200}');
});
// con.end()
})
app.get('/logout', (req, res) => {
req.session.destroy()
res.redirect('/')
})
var server = https.createServer(options, app)
server.listen(port, () => { console.log("listening on port: " + port) });