forked from uclcg/uclcg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
177 lines (155 loc) · 5.75 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
var fs = require('fs');
var bodyParser = require('body-parser');
var path = require('path');
var passport = require('passport');
var config = require('./server/config.js');
var ss = require('socket.io-stream');
var express = require('express');
var app = express();
var http = require('http').Server(app);
//var io = require('socket.io')(http);
var sessionMiddleware = require('./server/session.js');
var routes = require('./server/routes.js');
var db = require('./server/database.js');
app.use(require('serve-favicon')(path.join(__dirname, "favicon.ico")));
app.use('/public', express.static(path.join(__dirname, 'public')));
app.use('/experiments', express.static(path.join(__dirname, 'experiments')));
app.use(sessionMiddleware);
app.use(passport.initialize());
app.use(passport.session());
app.use(bodyParser.urlencoded({extended: true}));
app.use('/', routes);
/*
io.use(function (socket, next) {
sessionMiddleware(socket.request, socket.request.res, next);
});
//Socket IO for setups
io.on('connection', function (socket) {
try {
var user = socket.request.session.passport.user;
} catch(err) {
var user = undefined;
}
socket.on('checkLoggedIn', function () {
if (typeof(user) == 'undefined') {
socket.emit('loggedOut', {});
} else {
socket.emit('loggedIn', {});
}
});
// ##### SHOW ALL #####
socket.on('getAllSetups', function (data) {
//find all setups in the Database (no login required)
db.setups.find(function (err, setups) {
if (err) return console.error(err);
socket.emit('returnAllSetups', JSON.stringify(setups));
});
});
// ##### DELETE #####
socket.on('deleteSetup', function (data) {
//make sure the user is logged in
if (typeof user == "undefined") {
console.log('Non-authenticated connection refused.');
socket.emit('unauthorized', {});
}
//find setup first
db.setups.findById(data.id2delete, function (err, instance) {
if (err) return console.error(err);
if (instance == null) {
console.log('ERROR: Cannot delete id: ' + data.id2delete);
return;
}
//remember files
var picPath = instance.picture;
var jsPath = instance.jsFile;
//delete the setup
db.setups.findByIdAndRemove(data.id2delete, function (err) {
//delete files on disk
fs.unlink(picPath, function () {
fs.unlink(jsPath, function () {
console.log('Sucesfully deleted setup: ' + data.id2delete);
socket.emit('setupDeleteSuccessful', {});
});
});
});
});
});
// ##### HIDE #####
socket.on('hideSetup', function (data) {
if (typeof user == "undefined") {
console.log('Non-authenticated connection refused.');
socket.emit('unauthorized', {});
}
db.setups.findById(data.id2hide, function (err, instance) {
if (err) return console.error(err);
instance.hidden = data.hide;
instance.save(function (err, updatedInstance) {
if (err) return console.error(err);
//emit success msg
console.log("Updated Setup:");
console.log(updatedInstance);
});
});
});
// ##### ADD #####
var isAdding = 0;
var isAddingInner = 0;
var filenamePicture;
var metaData;
ss(socket).on('addSetup', function (stream, data) {
isAdding += 1;
if (stream == null || isAdding > 1) {
socket.emit('invalidStream', {});
return;
}
//make sure the user is logged in
if (typeof user == "undefined") {
console.log('Non-authenticated connection refused.');
socket.emit('unauthorized', {});
return;
}
var currentTime = String(Date.now());
metaData = JSON.parse(data);
filenamePicture = 'public/experiments/images/' + user.username + '_' + currentTime + '.png';
stream.pipe(fs.createWriteStream(filenamePicture));
stream.on('end', function () {
console.log("End: Requesting Js File");
socket.emit('addSetupRequestJS');
});
});
ss(socket).on('addSetupJS', function (stream1, dataNone) {
isAddingInner += 1;
if (stream1 == null || isAddingInner > 1) {
socket.emit('invalidStream', {});
return;
}
var currentTime = String(Date.now());
var filenameJS = 'public/experiments/code/' + user.username + '_' + currentTime + '.js';
stream1.pipe(fs.createWriteStream(filenameJS));
//once uploaded, add to db and then return success event
stream1.on('end', function () {
//write to db
var newSetup = new db.setups({
jsFile: filenameJS,
category: metaData.category,
picture: filenamePicture,
niceName: metaData.niceName,
shortDescription: metaData.shortDescription,
author: metaData.author
});
newSetup.save(function (err, newInstance) {
if (err) return console.error(err);
//emit success msg
console.log("New Setup Added:");
console.log(newInstance);
socket.emit('addSetupSuccess');
stream1.destroy();
isAdding -= 1;
isAddingInner -= 1;
});
});
});
});*/
http.listen(config.port, function () {
console.log('Example app listening on port:' + config.port);
});