-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
324 lines (278 loc) · 9.61 KB
/
server.js
File metadata and controls
324 lines (278 loc) · 9.61 KB
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
/**
* Created by jamiecho on 7/8/16.
*/
var express = require('express');
var formidable = require('formidable');
var bodyparser = require('body-parser');
var cookieParser = require('cookie-parser');
var path = require('path');
var fs = require('fs');
var aws = require('aws-sdk');
var session = require('express-session');
var bucket = 'hitag-fs';
var s3 = new aws.S3({Bucket: bucket});
var async = require('async');
var db = require('./db');
var app = express();
/* TMP_DIR */
var tmp_dir = path.join(__dirname, 'tmp');
var public_dir = path.join(__dirname, 'public');
if (!fs.existsSync(tmp_dir))
fs.mkdirSync(tmp_dir);
//boilerplate initialization
app.use(bodyparser.json());
app.use(cookieParser());
app.use(express.static(tmp_dir)); //statically served files
app.use(express.static(public_dir));
//this will be the location of the files
app.use(session({
secret: 'TunaDr3ams',
resave: false,
saveUninitialized: true,
}));
app.set('view engine', 'jade');
function checkLogin(req, res, next) {
if (!req.session.userid) {
res.redirect('/login?dest=' + req.url);
} else {
next();
}
}
function handleWebview(err,entries,res){
if (err) {
res.end("ERROR RETRIEVING ENTRIES FROM DATABASE");
} else {
async.each(entries,
function (entry, done) {
var key = entry.photo;
var params = {
Bucket: bucket,
Key: key
};
var filepath = path.join(tmp_dir, key);
if (fs.existsSync(filepath)) {
console.log("already exists");
// no need to create in the filesystem again.
done();
} else {
var ofs = fs.createWriteStream(filepath);
s3.getObject(params).createReadStream().pipe(ofs).on('finish', function (err) {
if (err) {
console.log("ERROR CREATING OBJECT", key, "FROM FILESYSTEM");
}
done();
});
}
},
function (err, cb) {
if (err) {
res.end("ERROR GETTING OBJECTS FROM FILESYSTEM");
} else {
res.render('index', {'entries': entries});
}
});
}
}
app.get('/', checkLogin, function (req, res) {
// REAL CODE
if(req.session.userid == -1){ //admin
db.con.query('SELECT * FROM webview', function(err,entries){
handleWebview(err,entries,res);
})
}else{
db.con.query('SELECT * FROM webview WHERE p_id = ?', req.session.userid, function (err, entries) {
handleWebview(err,entries,res);
});
}
});
function createEntry(entry, req, res){
var ifs = fs.createReadStream(path.join(tmp_dir, entry.photo));
var options = {partSize: 10 * 1024 * 1024, queueSize: 1};
var params = {
Bucket: bucket,
Key: entry.photo,
Body: ifs
};
db.con.query('INSERT INTO entries SET ?', entry, function (err, result) {
console.log('INSERTED ...');
if (err) {
res.end("ERROR UPLOADING ENTRY TO DATABASE");
} else {
s3.upload(params, options, function (err, data) {
console.log("PUT OBJECT ...");
if (err) {
console.log(err);
res.end("ERROR UPLOADING FILE TO FILESYSTEM");
} else {
console.log("successfully put object!!");
//file is up in the filesystem, all is good
res.redirect('/');
}
});
}
});
}
app.get('/upload', checkLogin, function(req,res){
res.render('upload');
});
app.post('/upload', function (req, res) {
//repurpose for schema
var form = new formidable.IncomingForm();
form.uploadDir = tmp_dir;
form.keepExtensions = true;
form.on('file', function(field, file) {
//rename the incoming file to the file's name
fs.rename(file.path, path.join(tmp_dir, file.name));
});
form.parse(req, function (err, fields, files) {
var entry = {
photo : files.photo.name,
location : fields.location,
time : fields.time,
comments : fields.comments
};
if (!err) {
db.con.query("SELECT * from persons where username = ? and passcode = AES_ENCRYPT(?,'TunaDr3ams')", [fields.username, fields.passcode], function(err,result){
//check if user exists ...
if(!err && result != undefined && result.length > 0){
entry.p_id = result[0].id;
//search existing tag
db.con.query('SELECT id from tags where nationalID = ?', fields.nationalID, function(err,result){
if(!err && result != undefined && result.length > 0){
//tag exists
entry.t_id = result[0].id;
//create entry
createEntry(entry, req,res);
}else{
var tag = {
countryID : fields.countryID,
nationalID : fields.nationalID,
comments : fields.comments
};
//tag does not exist, first create tag...
db.con.query('INSERT INTO tags SET ?',tag,function(err,result){
entry.t_id = result.insertId;
//then create entry
createEntry(entry,req,res);
});
}
});
}else{
res.end('AUTHORIZATION FAILED');
}
});
} else {
//set header, error handling, etc.
res.end('ERROR PARSING FORM');
}
});
});
function quotewith(s, q){
return q + s + q;
}
function quote(s,q){
if(!q){
q = "'"; // default single quote
}
return q + s + q;
}
function buildQuery(o){ // only works for string fields
var s = "";
for(k in o) {
s += quote(k, '`') + ' = ' + quote(o[k], "'") + ",";
}
return s.substring(0, s.length - 1); // get rid of last comma
}
function createUser(user, cb) {
delete user.passcode_confirm;
delete user.signup;
db.con.query('SELECT * from persons WHERE username = ?', user.username, function(err,res){
if(res != undefined && res.length > 0){
cb(err,-1);
//username already exists
// possibly take them to "modification" page?
}else{
var pk = "AES_ENCRYPT('"+ user.passcode + "','TunaDr3ams')";
delete user.passcode;
var qstring = "INSERT INTO persons SET ";
qstring += buildQuery(user);
qstring += ", `passcode` = " + pk;
console.log(qstring);
db.con.query(qstring, function(err,res){
console.log(err);
console.log(res.insertId);
cb(err, res.insertId);
});
}
});
}
function findUser(username, passcode, cb) {
db.con.query('SELECT EXISTS(SELECT * from persons WHERE username = ?) as `exists`', username, function(err,res){
console.log(res);
var unok = false,
pcok = false,
id = null;
if(res != undefined && res.length > 0 && res[0].exists){
unok = true; //username exists
console.log("????");
db.con.query("SELECT id from persons WHERE username = ? AND passcode = AES_ENCRYPT(?,'TunaDr3ams')", [username, passcode], function(err,res){
console.log(err,res);
if(!err && res != undefined && res.length > 0){
pcok = true;
id = res[0].id;
}
console.log(unok, pcok, id);
cb(unok, pcok, id);
});
}
});
}
app.get('/login',function(req,res){
res.render('login');
});
app.post('/login', function (req, res) {
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
console.log(fields);
findUser(fields.username, fields.passcode, function (unok, pcok, id) {
console.log(unok, pcok, id);
if(unok){
if(pcok){
req.session.userid = id;
if(req.query.dest){
res.redirect(req.query.dest);
}else{
res.redirect('/');
}
}else{
res.render('login',{wrong:true}); //todo: special handling about 'wrong'
}
}else{
res.redirect('/signup');
}
});
});
});
app.get('/signup',function(req,res){
res.render('signup');
});
app.post('/signup',function(req,res){
var form = new formidable.IncomingForm();
form.parse(req,function(err,fields,files){
createUser(fields,function(err,id){
if(err){
res.end("ERROR : SIGNUP FAILED");
}else{
if(id == -1){
res.end("ERROR : USERNAME ALREADY EXISTS");
}else{
req.session.userid = id;
res.redirect('/');
}
}
});
});
//TO IMPLEMENT -- send credentials to database
});
var port = process.env.PORT || 8000;
app.listen(port);