Permalink
Browse files

Added custom express middleware to get upload stream, added code to p…

…ush uploaded files downstream
  • Loading branch information...
1 parent ecadd74 commit d254441786a84dfdbfbbbb52f023a9258953e7dd patrick committed Oct 21, 2012
Showing with 114 additions and 19 deletions.
  1. +2 −0 .gitignore
  2. +0 −13 public/index.html
  3. +7 −0 public/room_client.js
  4. +58 −6 server.js
  5. +10 −0 templates/index.jade
  6. +13 −0 templates/room.jade
  7. +24 −0 uplawder.js
View
@@ -1 +1,3 @@
*node_modules*
+*webm
+uploads*
View
@@ -1,13 +0,0 @@
-<html>
- <head>
- <script src='/socket.io/socket.io.js'></script>
- <script src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
-
- <script src='/index_client.js'></script>
- </head>
- <body>
- <button id="upload">Upload</button>
- <input id ="input"/>
- <button id="go">Go</button>
- </body>
-</html>
View
@@ -18,6 +18,13 @@ socket.on('pause',function () {
video.pause();
});
+socket.on('paused?', function(paused) {
+ if(paused)
+ video.pause();
+ else
+ video.play();
+});
+
socket.on('play', function (data) {
video.play();
video.currentTime = data;
View
@@ -3,15 +3,66 @@ var http = require('http');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
+var uplawder = require('./uplawder.js');
server.listen(8080); // start listening on 8080
app.configure(function () {
- app.use(express.static(__dirname+'/public')); // I will statically server all files in public
+ app.use(express.static(__dirname+'/public'));
+ //app.use(express.bodyParser({uploadDir: __dirname+'/uploads'}));
+ app.use(uplawder({uploadDir: __dirname+'/uploads'}));
+ app.set('views', __dirname+'/templates');
+ app.set('view engine','jade');
+ app.set('view options', {layout:false});
});
-app.get('/', function (request, response) { // Route / -> index.html
- response.sendfile(__dirname+'/index.html');
+app.get('/', function (request, response) {
+ //response.sendfile(__dirname+'/index.html');
+ response.render('index');
});
+app.get('/room/:id',function (req,res) {
+ console.log(req.params.id);
+ res.render('room', { test: 'TEST'});
+});
+
+var DaStream='';
+var DaData = new Buffer(0);
+
+app.post('/upload', function (req, res) {
+ console.log('Uplawding');
+
+ res.end('Uploading');
+ DaStream = req.fileStream;
+ console.log(DaStream);
+ DaStream.on('data', function(data) {
+ DaData = Buffer.concat([DaData,data]);
+ });
+ DaStream.on('end', function () {
+ console.log('==== END ====');
+ });
+});
+
+app.get('/stream/:id', function(req,res) {
+ var total = DaData.length;
+ if (req.headers['range']) {
+ var range = req.headers.range;
+ var parts = range.replace(/bytes=/, "").split("-");
+ var partialstart = parts[0];
+ var partialend = parts[1];
+
+ var start = parseInt(partialstart, 10);
+ var end = partialend ? parseInt(partialend, 10) : total-1;
+ var chunksize = (end-start)+1;
+ console.log('RANGE: ' + start + ' - ' + end + ' = ' + chunksize);
+
+ var file = DaData.slice(start,end);
+ res.writeHead(206, { 'Content-Range': 'bytes ' + start + '-' + end + '/' + total, 'Accept-Ranges': 'bytes', 'Content-Length': chunksize, 'Content-Type': DaStream.mime });
+ res.end(file);
+ } else {
+ console.log('ALL: ' + total);
+ res.writeHead(200, { 'Content-Length': total, 'Content-Type': DaStream.mime });
+ res.end(DaData);
+ }
+});
function Client(socket)
{
@@ -35,26 +86,27 @@ function timer() {
//timer();
// socket.io
+io.set('log level', 0);
io.sockets.on('connection', function (socket) {
clients.push(new Client(socket));
socket.emit('timer', currentTimer);
- socket.emit('pause');
+ socket.emit('paused?',paused);
socket.on('pause', function () {
for(var i = 0; i < clients.length; i++) {
clients[i].socket.emit('pause');
}
clearInterval(timerID);
- pause = true;
+ paused = true;
});
socket.on('play', function () {
for(var i = 0; i < clients.length; i++) {
clients[i].socket.emit('play',currentTimer);
}
timer();
- pause = false;
+ paused = false;
});
// Remove client from Clients
socket.on('disconnect', function () {
View
@@ -0,0 +1,10 @@
+doctype 5
+html
+ head
+ script(src='/socket.io/socket.io.js')
+ script(src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js')
+ script(src='/index_client.js')
+ body
+ form(action="/upload", method="post", enctype="multipart/form-data")
+ input(type="file", name="video")
+ button(type="submit") Go
View
@@ -0,0 +1,13 @@
+doctype 5
+html
+ head
+ script(src='/socket.io/socket.io.js')
+ script(src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js')
+ script(src='/room_client.js')
+ body
+ video(id="player")
+ source(src="/stream/Abv",type="video/webm")
+
+ br
+ button(id="pause") Pause
+ button(id="play") Play
View
@@ -0,0 +1,24 @@
+var formidable = require('formidable');
+
+exports = module.exports = function uplawder(options) {
+
+
+ return function (req,res,next) {
+ if(req.method == 'GET' || req.method == 'HEAD') return next();
+
+ //please be a form
+ //
+ var form = new formidable.IncomingForm;
+ Object.keys(options).forEach(function(key){
+ form[key] = options[key];
+ });
+
+ form.onPart = function (part) {
+
+ req.fileStream = part;
+ next();
+ };
+
+ form.parse(req);
+ }
+}

0 comments on commit d254441

Please sign in to comment.