-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·587 lines (512 loc) · 21.9 KB
/
server.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const crypto = require('crypto');
const bodyParser = require('body-parser');
const multer = require('multer');
const ffmpeg = require('fluent-ffmpeg');
const moment = require('moment');
const fs = require('fs');
const validator = require('validator');
var session = require('express-session');
const cookie = require('cookie');
const users = require('./routes/users');
const sources = require('./routes/sources');
const editing = require('./routes/editing')
const MongoClient = require('mongodb').MongoClient;
const oid = require('mongodb').ObjectID;
const url = "mongodb://dbAdmin:[email protected]:27017,ajacluster-shard-00-01-nmcvm.mongodb.net:27017,ajacluster-shard-00-02-nmcvm.mongodb.net:27017/test?ssl=true&replicaSet=AJACluster-shard-0&authSource=admin&retryWrites=true";
let db;
MongoClient.connect(url, { useNewUrlParser: true }).then(client => {
db = client.db('mydb');
startServer();
});
const startServer = () => (process.env.NODE_ENV === "production") ?
require("https").createServer({
cert: fs.readFileSync('/etc/letsencrypt/live/splycr.u4ium.org/cert.pem', 'utf8'),
key: fs.readFileSync('/etc/letsencrypt/live/splycr.u4ium.org/privkey.pem', 'utf8'),
ca: fs.readFileSync('/etc/letsencrypt/live/splycr.u4ium.org/chain.pem', 'utf8')
}, app).listen(443)
: require("http").createServer(app).listen(3001);
var app = express();
app.use(bodyParser.json());
var upload = multer({
dest: path.join(__dirname, 'uploads')
});
app.use(session({
secret: 'project-aja-splycr-3][.;./-o0;,l98uyjbhl87r6tcruy534a2s32afty6iu9y8-moi',
resave: false,
saveUninitialized: true,
}));
app.use(express.static(path.join(__dirname,
process.env.NODE_ENV === "production" ? 'splycr/build/' : 'public/')
));
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({
extended: false
}));
app.use(cookieParser());
var sanitizeContent = function (req, res, next) {
req.body.content = validator.escape(req.body.content);
next();
}
app.use(function (req, res, next) {
req.user = ('user' in req.session) ? req.session.user : null;
var username = (req.user) ? req.user : '';
res.setHeader('Set-Cookie', cookie.serialize('username', username, {
path: '/',
maxAge: 60 * 60 * 24 * 7 // 1 week in number of seconds
}));
next();
});
var isAuthenticated = function (req, res, next) {
if (!req.user) return res.status(401).end("Access Denied");
next();
};
var Project = (function () {
return function Project(userid, title, clips, effects, container, codec) {
this.owner = userid;
this.title = title;
this.clips = clips;
this.effects = effects;
this.container = container;
this.codec = codec;
};
}());
var Clip = (function () {
return function item(data) {
this.videoid = data.videoid;
this.owner = data.owner;
this.startTime = data.startTime;
this.stopTime = data.stopTime;
this.tags = data.tags;
};
}());
let outp = path.join(__dirname, "/clips/");
var clipper = (function (path, clipItem, id, duration, res) {
ffmpeg(path)
.setStartTime(clipItem.startTime)
// .screenshots({
// timestamps: ['00:01.000'],
// filename: id,
// folder: '/thumbnails/',
// size: '320x240'
// })
.setDuration(duration)
.output(outp + id + ".mp4")
.on('end', function (err) {
res.sendFile(outp + id + ".mp4");
// res.json(clipItem);
if (!err) {
console.log('conversion Done');
}
})
.on('error', function (err, stdout, stderr) {
console.log("ffmpeg stdout:\n" + stderr);
}).run();
});
// All video editing functions have been moved to ./routes/editing
var checker = (function checkProperties(obj) {
for (var key in obj) {
if (obj[key] === null || obj[key] === "")
return false;
}
return true;
});
// ---------------------------------------------------------------------------------
// Users
// curl -X POST -H "Content-Type: application/json" -d '{"username": "admin", "password": "pass", "bio": "bio"}' http://localhost:3001/api/users
app.post('/api/users/', users.userRegister);
// curl -X PUT -H "Content-Type: application/json" -d '{"username": "admin", "password": "pass"}' http://localhost:3001/api/users/
app.put('/api/users/', users.userSignIn);
// Signs the current user out
app.get('/api/signout/', function (req, res, next) {
req.session.destroy();
res.setHeader('Set-Cookie', cookie.serialize('username', '', {
path: '/',
maxAge: 60 * 60 * 24 * 7 // 1 week in number of seconds
}));
res.end("signed out");
});
// curl -X GET http://localhost:3001/api/users/all/
app.get('/api/users/all', isAuthenticated, users.userGetAll);
// curl -X GET http://localhost:3001/api/users/:username/
app.get('/api/users/:username', isAuthenticated, users.userGetOne);
// // curl -X PATCH -H "Content-Type: application/json" -d '{"username": "admin", "password": "newpass","oldpassword": "pass"}' http://localhost:3001/api/users/
app.patch('/api/users/', isAuthenticated, users.userPatch);
// curl -X DELETE http://localhost:3001/api/users/:username
app.delete('/api/users/:username', isAuthenticated, users.deleteUser);
// ---------------------------------------------------------------------------------
// Sources
// curl -F '[email protected]' http://localhost:3001/api/sources/12/
app.post('/api/sources/:userid/:videoid', isAuthenticated, upload.single('sources'), sources.uploadSource);
// curl -X GET http://localhost:3001/api/sources/userid/thing
app.get('/api/sources/:userid/:videoid', isAuthenticated, sources.getVideoInformationByVideoId);
// curl -X GET http://localhost:3001/api/userid
app.get('/api/sources/:userid/', isAuthenticated, sources.getAllVideosByUserId);
// curl -X GET http://localhost:3001/api/sources/videoid/thing/source
app.get('/api/sources/:userid/:videoid/source', isAuthenticated, sources.getSource);
// curl -X DELETE http://localhost:3001/api/sources/1
app.delete('/api/sources/:userid/:videoid', isAuthenticated, sources.deleteSource);
// curl -X GET http://localhost:3000/api/sources/videoid/next
app.get('/api/sources/:userid/:videoid/next', isAuthenticated, sources.getNext);
// curl -X GET http://localhost:3000/api/sources/videoid/next
app.get('/api/sources/:userid/:videoid/previous', isAuthenticated, sources.getPrevious);
// ---------------------------------------------------------------------------------
// Projects
// curl -X POST -H "Content-Type: application/json" -d '{"title": "First project", "effects": "[russia]","clips": ["5ca00c76fa7f5d0b6c2824c0", "5ca00c8cfa7f5d0b6c2824c1"], "container": "something", "codec": "somethingelse"}' http://localhost:3000/api/projects/admin
app.post("/api/projects/:userid", isAuthenticated, function (req, res, next) {
let project = new Project(
req.params.userid,
req.body.title,
req.body.clips,
req.body.effects,
req.body.container,
req.body.codec
);
if (!checker(project)) res.status(400).end("Invalid arguments");
if (project.owner !== req.session.user) return res.status(401).end("Access Denied"); //cant post projects on behalf of other users
db.collection('projects').update({ owner: project.owner, title: project.title }, project, { upsert: true }, (err) => {
if (err) res.status(500).end(err.toString())
db.collection('projects').update(
{ owner: project.owner, title: project.title },
{
$currentDate: {
createtime: true
}
},
{ upsert: true }, (err) => {
if (err) res.status(500).end(err.toString());
}
)
res.json(project)
}
);
});
// // curl -X POST -d "title=title&tags=["putin","saudi",""]&clipids=["5ca00c76fa7f5d0b6c2824c0", "5ca00c8cfa7f5d0b6c2824c1"]&container=output&codec=output" http://localhost:3000/api/projects/:userid
app.patch("/api/projects/:userid/:projectid/", isAuthenticated, function (req, res, next) {
let project = new Project(
req.params.userid,
req.body.title,
req.body.clips,
req.body.effects,
req.body.container,
req.body.codec
);
if (project.owner !== req.session.user) return res.status(401).end("Access Denied"); //cant patch projects on behalf of other users
if (!checker(project)) res.status(400).end("Invalid arguments");
db.collection('projects').updateOne({ _id: oid(req.params.projectid) }, {
$set: {
title: req.body.title,
tags: req.body.tags,
clips: req.body.clips,
container: req.body.container,
codec: req.body.codec
}
}, { upsert: true }, err => err ? res.status(500).end(err.message) : res.json(project));
});
// curl -X GET http://localhost:3001/api/projects/KdHiNamDajGl1EyG/projectid
app.get('/api/projects/:userid/:projectid', function (req, res, next) {
db.collection('projects').find({
_id: oid(req.params.projectid),
owner: req.params.userid
}).toArray(function (err, project) {
if (err) return res.status(500).end(err);
if (project == 0) return res.status(404).end("Project doesn't exist");
return res.json(project[0]);
});
});
// curl -X GET http://localhost:3001/api/projects/KdHiNamDajGl1EyG/
app.get('/api/projects/:userid/', function (req, res, next) {
db.collection('projects').find({
owner: req.params.userid
}).toArray(function (err, project) {
if (err) return res.status(500).end(err);
if (project == 0) return res.status(404).end("Projects doesn't exist");
return res.json(project);
});
});
// curl -X GET http://localhost:3001/api/projects/
app.get('/api/projects/', function (req, res, next) {
db.collection('projects').find({}).toArray(function (err, project) {
if (err) return res.status(500).end(err);
if (project == 0) return res.status(404).end("Projects doesn't exist");
return res.json(project);
});
});
// curl -X GET http://localhost:3001/api/projects/KdHiNamDajGl1EyG/projectid/source
app.get('/api/projects/:userid/:projectid/source', function (req, res, next) {
db.collection('projects').find({
_id: oid(req.params.projectid),
owner: req.params.userid
}).toArray(function (err, project) {
if (err) return res.status(500).end(err);
if (project == 0) return res.status(404).end("Projects doesn't exist");
res.sendFile("/projects/" + project[0]._id);
});
});
// // curl -X DELETE http://localhost:3001/api/projects/KdHiNamDajGl1EyG/projectid
app.delete('/api/projects/:userid/:projectid', isAuthenticated, function (req, res, next) {
if (req.params.userid !== req.session.user) return res.status(401).end(" Access Denied"); //cant delete other user's projects
db.collection('projects').deleteOne({
_id: oid(req.params.projectid),
owner: req.params.userid
}, function (err, doc) {
if (err) return doc.status(500).end(err);
if (doc.result.ok == 0) return doc.status(404).end("Project doesn't exist");
return res.json("Deleted successfully");
});
});
// // curl -X DELETE http://localhost:3001/api/projects/KdHiNamDajGl1EyG/
app.delete('/api/projects/:userid/', function (req, res, next) {
if (req.params.userid === "all") {
db.collection('projects').deleteMany({}, function (err, doc) {
if (err) return doc.status(500).end(err);
if (doc.result.ok == 0) return doc.status(404).end("User doesn't have any projects");
return res.json("Deleted successfully");
});
} else {
if (req.params.userid !== req.session.user) return res.status(401).end(" Access Denied"); //cant delete other user's projects
db.collection('projects').deleteMany({
owner: req.params.userid
}, function (err, doc) {
if (err) return doc.status(500).end(err);
if (doc.result.ok == 0) return doc.status(404).end("User doesn't have any projects");
return res.json("Deleted successfully");
})
};
});
// app.patch("/api/projects/:userid/:projectid/", (req, res, next) => {
// if (req.params.userid !== req.session.user) return res.status(401).end(" Access Denied"); // cant post on behalf of other users
// projects.update({ _id: req.params.projectid });
// });
// ---------------------------------------------------------------------------------
// Clips
// Garbage code that does the job of creating a new clip
// curl -X POST -H "Content-Type: application/json" -d '{"videoid": "thing", "owner": "KdHiNamDajGl1EyG","startTime": "00:00:01","stopTime": "00:00:03", "tags": ["tag1", "tag2"]}' http://localhost:3001/api/clips
app.post('/api/clips/', isAuthenticated, function (req, res, next) {
var clipItem = new Clip(req.body);
if (!checker(clipItem)) return res.status(400).end("Invalid arguments");
if (clipItem.owner !== req.session.user) return res.status(401).end(" Access Denied"); // cant post on behalf of other users
// var start = moment.utc(clipItem.startTime, "HH:mm:ss");
// var end = moment.utc(clipItem.stopTime, "HH:mm:ss");
// var duration = end.diff(start, 'seconds');
db.collection('sources').find({
name: clipItem.videoid
}).toArray(function (err, source) {
if (err) return res.status(500).end(err);
if (source == 0) return res.status(409).end('source doesnt exist');
db.collection('clips').find(clipItem).toArray(function (err, clip) {
if (err) return res.status(500).end(err);
if (clip != 0) return res.status(409).end("clip " + clipItem + " already exists");
db.collection('clips').insertOne({
owner: clipItem.owner,
videoid: clipItem.videoid,
startTime: clipItem.startTime,
stopTime: clipItem.stopTime,
tags: clipItem.tags,
path: "/clips/",
// thumbnail: '/thumbnails/'
})
return res.status(200).end();
// .then(function (result) {
// db.collection('clips').updateOne({
// _id: result.ops[0]._id
// }, {
// $set: {
// path: "/clips/" + result.ops[0]._id + ".mp4",
// thumbnail: '/thumbnails/' + result.ops[0]._id
// }
// });
// // Todo: Create a worker that takes care of clipping the video
// clipper(source[0].video.path, clipItem, result.ops[0]._id, duration, res);
// if (err) return res.status(500).end(err);
// });
});
});
});
// curl -X POST -H "Content-Type: application/json" -d '{"videoid": "thing", "owner": "KdHiNamDajGl1EyG","startTime": "00:00:01","stopTime": "00:00:03", "tags": ["tag1", "tag2"]}' http://localhost:3001/api/clips
app.post('/api/clips/:clipid', isAuthenticated, function (req, res, next) {
var clipItem = new Clip(req.body);
if (!checker(clipItem)) return res.status(400).end("Invalid arguments");
var start = moment.utc(clipItem.startTime, "HH:mm:ss");
var end = moment.utc(clipItem.stopTime, "HH:mm:ss");
var duration = end.diff(start, 'seconds');
db.collection('sources').find({
name: req.params.videoid
}).toArray(function (err, source) {
if (err) return res.status(500).end(err);
if (source == 0) return res.status(409).end('source doesnt exist');
if (clipItem.owner !== req.session.user) return res.status(401).end(" Access Denied"); // cant post on behalf of other users
db.collection('clips').update({
_id: oid(req.params.clipid)
}, {
$set: {
startTime: clipItem.startTime,
stopTime: clipItem.stopTime,
}
})
res.json("Patched up");
// clipper("/sources/" + source.video.path, clipItem, video._id, duration, res);
if (err) return res.status(500).end(err);
});
});
// // curl -X PATCH -H "Content-Type: application/json" -d '{"videoid": "thing", "owner": "KdHiNamDajGl1EyG","startTime": "00:00:01","stopTime": "00:00:03", "tags": ["tag1", "tag2"]}' http://localhost:3001/api/clips
app.patch('/api/clips/:clipid', isAuthenticated, function (req, res, next) {
var clipItem = new Clip(req.body);
if (!checker(clipItem)) return res.status(400).end("Invalid arguments");
var start = moment.utc(clipItem.startTime, "HH:mm:ss");
var end = moment.utc(clipItem.stopTime, "HH:mm:ss");
var duration = end.diff(start, 'seconds');
if (clipItem.owner !== req.session.user) return res.status(401).end(" Access Denied"); // cant post on behalf of other users
db.collection('sources').find({
name: clipItem.videoid
}).toArray(function (err, source) {
if (err) return res.status(500).end(err);
if (source == 0) return res.status(409).end('source doesnt exist');
db.collection('clips').find(clipItem).toArray(function (err, clip) {
if (err) return res.status(500).end(err);
if (clip != 0) return res.status(409).end("clip " + clipItem + " already exists");
db.collection('clips').insertOne({
owner: clipItem.owner,
videoid: clipItem.videoid,
startTime: clipItem.startTime,
stopTime: clipItem.stopTime,
tags: clipItem.tags,
path: "/clips/",
thumbnail: '/thumbnails/'
}).then(function (result) {
db.collection('clips').updateOne({
_id: result.ops[0]._id
}, {
$set: {
path: "/clips/" + result.ops[0]._id + ".mp4",
thumbnail: '/thumbnails/' + result.ops[0]._id,
startTime: clipItem.startTime,
stopTime: clipItem.stopTime,
tags: clipItem.tags,
}
});
// Todo: Create a worker that takes care of clipping the video
clipper(source[0].video.path, clipItem, result.ops[0]._id, duration, res);
if (err) return res.status(500).end(err);
});
});
});
});
// Returns all clips belonging to that user
// curl -X GET http://localhost:3001/api/clips/KdHiNamDajGl1EyG
app.get('/api/clips/:userid/', function (req, res, next) {
db.collection('clips').find({
owner: req.params.userid,
}).toArray(function (err, clips) {
if (err) return res.status(500).end(err);
if (clips == 0) return res.status(409).end("video's by " + req.params.userid + " don't exist");
return res.json(clips);
});
});
// Returns all available clips informations
// curl -X GET http://localhost:3001/api/clips/
app.get('/api/clips/', function (req, res, next) {
db.collection('clips').find({}).toArray(function (err, clips) {
if (err) return res.status(500).end(err);
return res.json(clips);
});
});
// // Returns information about a specific clip
// // curl -X GET http://localhost:3001/api/clips/1/1/1
app.get('/api/clips/:userid/:objectid/', function (req, res, next) {
db.collection('clips').find({ videoid: req.params.objectid, owner: req.params.userid }
).toArray(function (err, clip) {
if (err) return res.status(500).end(err.toString());
if (clip == 0) {
if (oid.isValid(req.params.objectid)) {
db.collection('clips').find({ _id: oid(req.params.objectid) }).toArray(function (err, clips) {
if (clips == 0) return res.status(400).end("No clips with those parameters");
if (err) return res.status(500).end(err.toString());
return res.json(clips);
})
} else {
return res.status(400).end("No clips with those parameters");
}
} else {
return res.json(clip);
}
})
});
// Returns the actual clip source
// curl -X GET http://localhost:3001/api/clip/1/source/videoname
app.get('/api/clips/:userid/:clipid/source/', isAuthenticated, function (req, res, next) {
db.collection('clips').find({
owner: req.params.userid,
_id: oid(req.params.clipid)
}).toArray(function (err, clip) {
if (err) return res.status(500).end(err);
if (clip == 0) return res.status(409).end("video's by" + req.params.userid + " don't exist");
// res.setHeader('Content-Type', clip[0].path.file.mimetype);
res.sendFile(path.join(__dirname, clip[0].path));
});
});
// Searches for clips
// curl -X GET -H "Content-Type: application/json" -d '["russia"]' http://localhost:3001/api/search/
app.post('/api/search/', function (req, res, next) {
db.collection('clips').find({ tags: { $in: req.body } }).toArray(function (err, clips) {
if (err) return res.status(500).end(err);
return res.json(clips);
});
});
// Deletes a specific video
// curl -X DELETE http://localhost:3001/api/clips/userid/videoid
app.delete('/api/clips/:userid/:videoid/:clipid', isAuthenticated, function (req, res, next) {
db.collection('clips').find({
_id: oid(req.params.clipid)
}).toArray(function (err, clip) {
if (err) return res.status(500).end(err);
if (clip == 0) return res.status(409).end("clip's by" + req.params.userid + " don't exist");
if (clip[0].owner !== req.session.user) return res.status(401).end(" Access Denied" + clip.owner + " " + req.session.user); //cant delete other user's clips
db.collection('clips').deleteOne({
_id: oid(req.params.clipid)
}, function () {
if (err) return res.status(500).end(err);
return res.json("Success");
});
});
});
// Deletes all clips by a specific user
// curl -X DELETE http://localhost:3001/api/clips/user/KdHiNamDajGl1EyG
app.delete('/api/clips/:userid/', isAuthenticated, function (req, res, next) {
if (req.params.userid === "all") {
db.collection('clips').deleteMany({}, function (err) {
if (err) return res.status(500).end(err);
return res.json("Deleting succesful");
});
} else {
db.collection('clips').find({
owner: req.params.userid,
}).toArray(function (err, clip) {
if (err) return res.status(500).end(err);
if (clip == 0) return res.status(409).end("clip's by" + req.params.userid + " don't exist");
if (clip[0].owner !== req.session.user)
return res.status(401).end(" Access Denied" + clip[0].owner + " " + req.session.user);
//cant delete other user's projects
db.collection('clips').deleteMany({
owner: clip[0].owner
}, function () {
if (err) return res.status(500).end(err);
return res.json("Deleting succesful");
});
});
}
});
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
});
// error handler
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error');
});