-
Notifications
You must be signed in to change notification settings - Fork 0
/
oakstreaming-web-server.js
321 lines (245 loc) · 10.6 KB
/
oakstreaming-web-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
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var crypto = require('crypto');
var path = require( 'path' );
var process = require( "process" );
var fs = require('fs');
var chokidar = require('chokidar');
var formidable = require('formidable');
// It could be useful to change this constants sometime.
// Port on which the server should listen.
var PORT = 8080;
// Path to the folder which contains all video files that this Web server should be able to serve via
// hash value identification.
var PATH_TO_VIDEO_FOLDER = __dirname + "/web/videos";
// Path where the text file hashValues.sha2 will be saved. This text file will contain the hash values of
// all files in PATH_TO_VIDEO_FOLDER.
var HASH_VALUES_FILE_PATH = __dirname + "/web";
io.on('connection', function(socket){console.log("An OakStreaming instance has established a connection " +
"to this server via socket.io")});
io.on('disconnect', function(socket){console.log("An OakStreaming instance disconnected")});
var numberOfFilesToProcess = -1;
// This call of app.use is necessary to allow Cross-Origin Resource Sharing (CORS).
// It enables OakStreaming instances of other domains to successfully request byte ranges of
// video files from this Web server.
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Range");
next();
});
// This function call effects that all files in PATH_TO_VIDEO_FOLDER are served by the Web server.
// Moreover, the function calculates the hash values of all files in PATH_TO_VIDEO_FOLDER and
// saves them in HASH_VALUES_FILE_PATH.
fs.readdir(PATH_TO_VIDEO_FOLDER, function( err, files ){
if( err ) {
console.error("Could not list the directory.", err );
process.exit( 1 );
}
numberOfFilesToProcess = files.length;
files.forEach( function( file, index ) {
var hash = crypto.createHash('sha256');
var filePath = PATH_TO_VIDEO_FOLDER + "/" + file;
fs.stat( filePath, function( error, stat ){
if( error ) {
console.error( "Error stating file.", error );
return;
}
if(stat.isFile()){
console.log( "'%s' is a file.", filePath );
var stream = fs.createReadStream(filePath);
stream.on('data', function (data){
hash.update(data, 'binary');
});
stream.on('end', function () {
var finalHash = hash.digest('hex');
fs.appendFile(HASH_VALUES_FILE_PATH + "/hashValues.sha2", file + "/////" + finalHash + '\n',
function (err){
if(err){
return console.log(err);
}
});
// After this function call, the Web server serves the file.
// The requester must specify the file he/it wants by its name.
app.get("/" + file, function (req, res) {
console.log("Received a request for: " + file);
res.sendFile(filePath);
});
// After this function call, the Web server serves the file.
// The requester must specify the file he/it wants by its SHA-2 hash value.
app.get("/" + finalHash, function (req, res) {
console.log("Received a request for: " + finalHash);
res.sendFile(filePath);
});
numberOfFilesToProcess--;
});
}
else if( stat.isDirectory() ){
console.log( "'%s' is a directory.", filePath );
}
});
});
});
function calculateHashOfFile(filePath, callback){
var hash = crypto.createHash('sha256');
fs.stat( filePath, function( error, stat ){
if( error ) {
console.error( "Error stating file.", error );
return;
}
if(stat.isFile()){
console.log( "'%s' is a file.", filePath );
var stream = fs.createReadStream(filePath);
stream.on('data', function (data){
hash.update(data, 'binary');
});
stream.on('end', function () {
callback(hash.digest('hex'));
});
}
});
}
function watchDirectory(){
if(numberOfFilesToProcess == 0){
var directoryWatcher = chokidar.watch(PATH_TO_VIDEO_FOLDER, {awaitWriteFinish: {
stabilityThreshold: 500}});
// This event listener gets called whenever a file is added to PATH_TO_VIDEO_FOLDER.
directoryWatcher.on("add", function(absolutePath){
console.log("Configuring that " + absolutePath + " can be served.");
var fileName = absolutePath.substring(absolutePath.lastIndexOf("/")+1);
// After this app.get function call, the Web server serves the file.
// The requester must specify the file he/it wants by its name.
app.get("/" + fileName, function(req, res){
console.log("Received a request for: " + fileName);
res.sendFile(absolutePath);
});
calculateHashOfFile(absolutePath, function(hashValue){
// After this app.get function call, the Web server serves the file.
// The requester must specify the file he/it wants by its hash value.
app.get("/" + hashValue, function(req, res){
console.log("Received a request for: " + hashValue);
res.sendFile(absolutePath);
});
});
});
// This event listener gets called whenever in PATH_TO_VIDEO_FOLDER a file is deleted.
directoryWatcher.on("unlink", function(absolutePath){
var fileName = absolutePath.substring(absolutePath.lastIndexOf("/")+1);
app.get("/" + fileName, function(req, res){console.log("The requested file has been deleted");});
});
// This event listener gets called whenever a file in PATH_TO_VIDEO_FOLDER gets changed.
directoryWatcher.on("change", function(absolutePath, stats){
calculateHashOfFile(absolutePath, function(hashValue){
// After this app.get function call, the Web server serves the file.
// The requester must specify the file he/it wants by its hash value.
app.get("/" + hashValue, function(req, res){
console.log("Received a request for: " + hashValue);
res.sendFile(absolutePath);
});
});
});
directoryWatcher.on("error", function(error){console.log("The following error happened: " + error)});
} else {
setTimeout(watchDirectory, 500);
}
}
watchDirectory();
console.log('Current directory: ' + process.cwd());
// Receiving of the oakstreaming streamticket
app.post('/streamticket', function(req, res){
});
// This function call does not implement a feature of the OakStreaming library but
// has been put here so that the participants of the user evaluation can solve
// task 1.
app.post('/upload1', function(req, res){
console.log("app.post('upload1', ...) is called");
// Create an incoming form object.
var form = new formidable.IncomingForm();
var fileName = "";
// Specify that we want to allow the user to upload multiple files in a single request.
form.multiples = true;
// Store all uploads in the __dirname + '/web/videos' directory.
form.uploadDir = path.join(__dirname, '/web/videos');
// Every time a file has been uploaded successfully,
// rename it to it's original name.
form.on('file', function(field, file){
fileName = file.name;
fs.rename(file.path, path.join(form.uploadDir, file.name));
});
// Log any errors that occur.
form.on('error', function(err) {
console.log('An error has occured: \n' + err);
});
// Once all the files have been uploaded, send a response to the client.
form.on('end', function() {
console.log("form.on('end'..) of app.post('upload1', ...) is called");
console.log("fileName: " + fileName);
io.emit("newVideo1", "http://gaudi.informatik.rwth-aachen.de:9912/uploads/example1.mp4");
res.end('success');
});
// Parse the incoming request containing the form data.
form.parse(req);
});
// This function call does not implement a feature of the OakStreaming library but
// has been put here so that the participants of the user evaluation can solve
// task 1.
app.post('/upload2', function(req, res){
console.log("app.post('upload2', ...) is called");
// Create an incoming form object.
var form = new formidable.IncomingForm();
var fileName = "";
// Specify that we want to allow the user to upload multiple files in a single request.
form.multiples = true;
// Store all uploads in the __dirname + '/web/videos' directory.
form.uploadDir = path.join(__dirname, '/web/videos');
// Every time a file has been uploaded successfully,
// rename it to it's original name.
form.on('file', function(field, file){
fileName = file.name;
fs.rename(file.path, path.join(form.uploadDir, file.name));
});
// Log any errors that occur.
form.on('error', function(err) {
console.log('An error has occured: \n' + err);
});
// Once all the files have been uploaded, send a response to the client.
form.on('end', function() {
console.log("form.on('end'..) of app.post('upload2', ...) is called");
console.log("fileName: " + fileName);
io.emit("newVideo2", "http://gaudi.informatik.rwth-aachen.de:9912/uploads/example2.mp4");
res.end('success');
});
// Parse the incoming request containing the form data.
form.parse(req);
});
// The following calls to the app.get function configure the Web server to serve several files.
app.get('/', function(req, res){
console.log("Request for index page has been received.");
res.sendFile(__dirname + '/web/index.html');
});
app.get('/index.html', function(req, res){
console.log("Request for index page has been received.");
res.sendFile(__dirname + '/web/index.html');
});
app.get('/uploads/example1.mp4', function(req, res){
console.log("Received a request for example1.mp4");
res.sendFile(__dirname + '/uploads/example1.mp4');
});
app.get('/uploads/example2.mp4', function(req, res){
res.sendFile(__dirname + '/uploads/example2.mp4');
});
app.get('/uploads/test1.mp4', function(req, res){
res.sendFile(__dirname + '/uploads/test1.mp4');
});
app.get('/uploads/test2.mp4', function(req, res){
res.sendFile(__dirname + '/uploads/test2.mp4');
});
app.get("/example-application.js", function(req, res){
res.sendFile(__dirname + "/web/" + "example-application.js");
});
app.get("/example-application.js.map", function(req, res){
res.sendFile(__dirname + "/web/" + "example-application.js.map");
});
http.listen(PORT, function(){
console.log('Oakstreaming Web server is running and listens on port ' + PORT);
});