-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (71 loc) · 2.63 KB
/
index.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
var http = require('http');
var socketio = require('socket.io');
var iostream = require('socket.io-stream');
var fs = require('fs');
var path = require('path');
var Busboy = require('busboy');
var formidable = require('formidable');
var util = require('util');
var app = http.createServer(function (req, res) {
// if (req.method === 'POST') {
// var timeStart = new Date();
// var busboy = new Busboy({ headers: req.headers });
// busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
// //console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype);
// var filename = 'post_test_' + counter + '_' + filename;
// file.on('end', function() {
// var timeEnd = new Date();
// var duration = timeEnd - timeStart;
// console.log('HTTP streaming upload complete in ' + duration + 'ms');
// res.writeHead(200, {'Content-Type': 'text/plain'});
// res.end('okay');
// });
// file.on('data', function() {});
// counter++;
// });
// req.pipe(busboy);
// }
if (req.method === 'POST') {
var form = new formidable.IncomingForm();
var timeStart = new Date();
form.parse(req, function(err, fields, files) {
// res.writeHead(200, {'content-type': 'text/plain'});
// res.write('received upload:\n\n');
// console.log(files);
// res.end(util.inspect({fields: fields, files: files}));
});
// form.on('progress', function(bytesReceived, bytesExpected) {
// console.log('Received '+bytesReceived + '/'+bytesExpected);
// });
form.on('fileBegin', function(filename, file){
var filename = 'post_test_' + counter + '_' + filename;
file.on('end', function() {
var timeEnd = new Date();
var duration = timeEnd - timeStart;
console.log('HTTP streaming upload complete in ' + duration + 'ms');
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('okay');
});
file.on('data', function() {});
counter++;
})
return;
}
});
var io = socketio(app);
var counter = 0;
io.on('connection', function(socket) {
iostream(socket).on('upload', function(stream, data) {
var timeStart = new Date();
var filename = 'socket_test_' + counter + '_' + path.basename(data.name);
stream.on('end', function() {
var timeEnd = new Date();
var duration = timeEnd - timeStart;
console.log('socket.io streaming upload complete in ' + duration + 'ms');
socket.disconnect();
});
stream.on('data', function() {});
counter++;
});
});
app.listen(process.env.PORT || 3000);