2012-01-11 23:26:58 +01:00
|
|
|
// plemp.rb - The Plemp! application, create your own on-line pile of junk!
|
|
|
|
//
|
2012-01-11 15:06:29 +01:00
|
|
|
// Plemp! is Copyright © 2012 Paul van Tilburg <paul@mozcode.nl>
|
2012-01-11 23:26:58 +01:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify it under
|
|
|
|
// the terms of the GNU General Public License as published by the Free Software
|
|
|
|
// Foundation; either version 2 of the License, or (at your option) any later
|
2012-01-11 15:06:29 +01:00
|
|
|
// version.
|
|
|
|
|
2012-01-12 13:22:22 +01:00
|
|
|
var express = require("express")
|
2012-01-15 14:03:33 +01:00
|
|
|
, db = require("./db")
|
2012-01-12 13:22:22 +01:00
|
|
|
, form = require("connect-form")
|
2012-02-11 23:56:40 +01:00
|
|
|
, fs = require("fs")
|
|
|
|
, path = require("path")
|
2012-01-12 13:22:22 +01:00
|
|
|
, crypto = require("crypto")
|
2012-02-09 10:22:17 +01:00
|
|
|
, mime = require("mime")
|
|
|
|
, url = require("url");
|
2012-01-11 15:06:29 +01:00
|
|
|
|
|
|
|
// Set up the Node Express application.
|
2012-01-11 15:19:29 +01:00
|
|
|
var app = express.createServer(form({ keepExtensions: true,
|
2012-01-12 11:15:41 +01:00
|
|
|
uploadDir: __dirname + '/public/upload' }));
|
2012-01-11 15:06:29 +01:00
|
|
|
|
2012-02-09 10:22:17 +01:00
|
|
|
// List of events.
|
|
|
|
var events = [];
|
|
|
|
// List of deferred requests/connections.
|
|
|
|
var defers = [];
|
|
|
|
// Maximum age of events (in seconds).
|
|
|
|
var maxAge = 3600;
|
|
|
|
// Request sequence number.
|
|
|
|
var lastRequestId = 0;
|
|
|
|
|
|
|
|
// Return the current time time in milliseconds.
|
|
|
|
function currentTimestamp() {
|
|
|
|
return new Date().getTime();
|
|
|
|
}
|
|
|
|
|
2012-02-11 01:35:25 +01:00
|
|
|
// Escape the HTML.
|
|
|
|
function escapeHTML(text) {
|
2012-02-11 23:56:40 +01:00
|
|
|
return text.replace(/&/g, '&')
|
|
|
|
.replace(/</g, '<')
|
|
|
|
.replace(/>/g, '>')
|
|
|
|
.replace(/"/g, '"')
|
|
|
|
.replace(/'/g, ''');
|
2012-02-11 01:35:25 +01:00
|
|
|
}
|
|
|
|
|
2012-02-09 10:22:17 +01:00
|
|
|
// Compacts an array by removing all undefined values.
|
|
|
|
function compact(arr) {
|
|
|
|
if (!arr) return null;
|
|
|
|
|
|
|
|
var i, data = [];
|
|
|
|
for (i = 0; i < arr.length; i++) {
|
|
|
|
if (arr[i]) data.push(arr[i]);
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a new event with the given type and optional data.
|
|
|
|
function addEvent(type, data) {
|
|
|
|
var event = { type: type,
|
2012-02-11 23:56:40 +01:00
|
|
|
timestamp: currentTimestamp() };
|
2012-02-09 10:22:17 +01:00
|
|
|
if (data) event.data = data;
|
|
|
|
|
|
|
|
events.push(event);
|
|
|
|
console.log("[P] " + JSON.stringify(event));
|
|
|
|
// Notify deferred connections that there is something new.
|
|
|
|
notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the next event in the list of events after the optional timestamp.
|
|
|
|
// Expires events that are older than the maximum age (maxAge).
|
|
|
|
function nextEvent(timestamp) {
|
|
|
|
if (!events) return null;
|
|
|
|
if (!timestamp) timestamp = 0;
|
|
|
|
|
|
|
|
var event, nEvent, i;
|
|
|
|
var minTimestamp = currentTimestamp() - maxAge * 1000;
|
|
|
|
for (i = 0; i < events.length; i++) {
|
|
|
|
event = events[i];
|
|
|
|
|
|
|
|
// Check if event is expired.
|
|
|
|
if (event.timestamp < minTimestamp) {
|
|
|
|
console.log("[.] expired: " + JSON.stringify(event));
|
|
|
|
delete events[i];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Check if event is newer.
|
|
|
|
if (event.timestamp > timestamp) {
|
|
|
|
console.log("[.] next event after timestamp " + timestamp + ": " +
|
|
|
|
JSON.stringify(event));
|
|
|
|
nEvent = event;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Compact the list of events.
|
|
|
|
events = compact(events);
|
|
|
|
return nEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify all deferred connections of their respect nextEvent.
|
|
|
|
function notify() {
|
|
|
|
if (!defers) return;
|
|
|
|
|
|
|
|
var i, ctx, event;
|
|
|
|
for (i = 0; i < defers.length; i++) {
|
|
|
|
ctx = defers[i];
|
|
|
|
|
|
|
|
if (!ctx.req) {
|
2012-02-11 23:56:40 +01:00
|
|
|
// Apparently this connectioned was timed out.
|
2012-02-09 10:22:17 +01:00
|
|
|
delete defers[i];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
event = nextEvent(ctx.timestamp);
|
|
|
|
if (event) {
|
|
|
|
ctx.req.resume();
|
|
|
|
ctx.res.send(event);
|
|
|
|
ctx.res.end();
|
|
|
|
delete defers[i];
|
|
|
|
console.log("[" + ctx.id + "] sent " + JSON.stringify(event));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Compact the list of deferred connections.
|
|
|
|
defers = compact(defers);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save and pause the request.
|
|
|
|
function pause(timestamp, req, res, requestId) {
|
|
|
|
var ctx = { id: requestId,
|
|
|
|
timestamp: timestamp,
|
|
|
|
req: req,
|
|
|
|
res: res };
|
|
|
|
defers.push(ctx);
|
|
|
|
|
|
|
|
req.connection.setTimeout(600 * 1000);
|
|
|
|
req.connection.on('timeout', function() {
|
|
|
|
ctx.req = null;
|
|
|
|
ctx.res = null;
|
2012-02-11 00:37:45 +01:00
|
|
|
console.log("[" + requestId + "] timed out");
|
2012-02-09 10:22:17 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
req.pause();
|
|
|
|
console.log("[" + requestId + "] paused");
|
|
|
|
}
|
|
|
|
|
2012-01-15 14:03:33 +01:00
|
|
|
// Retrieve the draggables info.
|
|
|
|
var draggables = db.load();
|
2012-01-15 17:16:40 +01:00
|
|
|
for (drag_id in draggables) {
|
2012-02-11 23:56:40 +01:00
|
|
|
var drag = draggables[drag_id];
|
|
|
|
|
2012-02-09 10:25:55 +01:00
|
|
|
if (!path.existsSync(__dirname + "/public/upload/" + drag.name)) {
|
|
|
|
console.log("Could not find file for draggable " + drag_id +
|
2012-01-15 17:16:40 +01:00
|
|
|
"; removing from database!");
|
|
|
|
delete draggables[drag_id];
|
|
|
|
}
|
|
|
|
}
|
2012-01-11 17:02:22 +01:00
|
|
|
|
2012-01-11 15:06:29 +01:00
|
|
|
// Application settings and middleware configuration.
|
|
|
|
app.configure(function() {
|
|
|
|
app.use(express.logger());
|
2012-01-11 15:20:01 +01:00
|
|
|
app.use(express.static(__dirname + '/public'));
|
2012-01-11 15:06:29 +01:00
|
|
|
app.use(express.bodyParser());
|
|
|
|
app.use(express.methodOverride());
|
|
|
|
app.use(app.router);
|
2012-01-16 11:34:19 +01:00
|
|
|
});
|
|
|
|
|
2012-01-16 23:45:15 +01:00
|
|
|
app.configure('development', function() {
|
2012-01-11 15:06:29 +01:00
|
|
|
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
|
|
|
|
});
|
|
|
|
|
2012-01-16 23:45:15 +01:00
|
|
|
app.configure('production', function() {
|
2012-01-16 11:34:19 +01:00
|
|
|
app.use(express.errorHandler());
|
|
|
|
});
|
|
|
|
|
2012-01-11 15:06:29 +01:00
|
|
|
// Server the main index file statically for now.
|
|
|
|
app.get('/', function(req, res) {
|
|
|
|
res.redirect('/index.html');
|
|
|
|
});
|
|
|
|
|
2012-02-09 10:24:40 +01:00
|
|
|
// The events controller: accessed through AJAX long-poll requests by the
|
|
|
|
// main page for getting new events.
|
|
|
|
app.get('/events', function(req, res) {
|
|
|
|
var u = url.parse(req.url, true);
|
|
|
|
|
|
|
|
if (!u.query) {
|
|
|
|
res.send(null, 400);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var timestamp = u.query.timestamp || 0,
|
|
|
|
requestId = lastRequestId++,
|
|
|
|
event = nextEvent(timestamp);
|
|
|
|
|
|
|
|
if (!event) {
|
|
|
|
pause(timestamp, req, res, requestId);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.send(event);
|
|
|
|
res.end();
|
|
|
|
console.log("[" + requestId + "] direct sent " + JSON.stringify(event));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-02-11 12:31:52 +01:00
|
|
|
// The download controller: provides access to the uploaded files but ensures
|
|
|
|
// that they are downloaded by the client.
|
|
|
|
app.get('/download/:id', function(req, res) {
|
|
|
|
var drag_id = req.params.id;
|
2012-02-14 00:21:31 +01:00
|
|
|
var drag = draggables[drag_id];
|
2012-02-11 12:31:52 +01:00
|
|
|
console.log("Provide download for draggable " + drag_id);
|
|
|
|
res.download(__dirname + "/public/upload/" + drag.name);
|
|
|
|
});
|
|
|
|
|
2012-01-12 12:23:52 +01:00
|
|
|
// The retrieval controller: accessed through AJAX requests by the main
|
2012-01-12 11:38:40 +01:00
|
|
|
// page for getting/setting the state (positions) of the draggables.
|
|
|
|
app.get('/draggables', function(req, res) {
|
2012-01-11 15:06:29 +01:00
|
|
|
// Retrieve the current status of the draggables and return in JSON format.
|
2012-02-09 10:25:26 +01:00
|
|
|
res.send({ timestamp: currentTimestamp(),
|
|
|
|
list: draggables });
|
2012-01-11 15:06:29 +01:00
|
|
|
});
|
|
|
|
|
2012-01-12 11:38:40 +01:00
|
|
|
// The upload controller: handles uploads from the main site. This can
|
|
|
|
// either be a file or some pasted text. After upload the controler
|
|
|
|
// redirects to the main page which includes the just uploaded file.
|
|
|
|
app.post('/draggables', function(req, res) {
|
2012-01-11 15:06:29 +01:00
|
|
|
req.form.complete(function(err, fields, files) {
|
|
|
|
if (err) {
|
2012-01-18 22:17:50 +01:00
|
|
|
// FIXME: next is undefined!
|
2012-01-11 15:06:29 +01:00
|
|
|
next(err);
|
|
|
|
}
|
|
|
|
else {
|
2012-02-11 00:47:41 +01:00
|
|
|
var drag_id, file_name, file_mime, file_title;
|
2012-01-12 12:23:08 +01:00
|
|
|
if (fields.text) {
|
2012-02-11 00:47:41 +01:00
|
|
|
var md5sum = crypto.createHash('md5');
|
2012-01-16 23:45:15 +01:00
|
|
|
md5sum = md5sum.update(fields.text).digest('hex');
|
2012-02-09 10:25:55 +01:00
|
|
|
drag_id = md5sum
|
|
|
|
file_name = drag_id + "." + fields.type;
|
2012-02-11 01:35:48 +01:00
|
|
|
file_title = fields.title || "Untitled";
|
2012-01-15 14:26:56 +01:00
|
|
|
file_mime = mime.lookup(fields.type);
|
2012-02-09 10:25:55 +01:00
|
|
|
fs.writeFile(__dirname + "/public/upload/" + file_name,
|
|
|
|
fields.text, function (err) {
|
2012-01-12 12:23:08 +01:00
|
|
|
if (err)
|
|
|
|
throw err;
|
|
|
|
console.log('Text saved to %s', file_name);
|
|
|
|
});
|
|
|
|
// FIXME: prevent this file being created from the start!
|
|
|
|
fs.unlink(files.file.path);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log('File %s uploaded to %s', files.file.filename,
|
|
|
|
files.file.path);
|
2012-02-09 10:25:55 +01:00
|
|
|
file_name = path.basename(files.file.path);
|
|
|
|
drag_id = path.basename(file_name, path.extname(file_name));
|
2012-02-11 01:35:48 +01:00
|
|
|
file_title = fields.title ||
|
|
|
|
path.basename(files.file.filename,
|
2012-02-09 10:25:55 +01:00
|
|
|
path.extname(files.file.filename));
|
2012-02-11 00:47:20 +01:00
|
|
|
file_mime = files.file.mime || "application/octet-stream";
|
2012-01-12 12:23:08 +01:00
|
|
|
}
|
2012-02-09 10:25:55 +01:00
|
|
|
draggables[drag_id] = { name: file_name,
|
2012-01-12 12:23:08 +01:00
|
|
|
mime: file_mime,
|
2012-01-18 22:17:50 +01:00
|
|
|
title: file_title,
|
2012-01-12 11:38:40 +01:00
|
|
|
top: 200,
|
|
|
|
left: 350 };
|
2012-02-09 10:23:29 +01:00
|
|
|
addEvent("add", { id: drag_id, info: draggables[drag_id] });
|
2012-01-18 22:14:22 +01:00
|
|
|
// If this was a drag & drop upload, do not redirect to /.
|
|
|
|
if (fields.type == 'dnd') {
|
|
|
|
res.send(true);
|
|
|
|
res.end();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.redirect('home');
|
|
|
|
}
|
2012-01-11 15:06:29 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-01-12 12:23:52 +01:00
|
|
|
// The draggable controller: provides direct access to the HTML
|
2012-01-12 11:38:40 +01:00
|
|
|
// generating code for draggable objects.
|
|
|
|
app.get('/draggables/:id', function(req, res) {
|
2012-01-12 13:22:22 +01:00
|
|
|
var drag_id = req.params.id;
|
2012-02-09 10:25:55 +01:00
|
|
|
var drag = draggables[drag_id];
|
|
|
|
var file_name = "../upload/" + drag.name;
|
2012-01-12 13:22:22 +01:00
|
|
|
console.log("Get draggable: " + drag_id);
|
2012-01-12 11:38:40 +01:00
|
|
|
// Stuff taken from the Camping implementation.
|
2012-02-11 12:29:01 +01:00
|
|
|
var default_style = "left:" + drag.left + "px;top:" + drag.top + "px;display:none;";
|
2012-02-11 23:56:40 +01:00
|
|
|
var title = drag.title || drag.name || 'Untitled';
|
2012-01-15 16:17:50 +01:00
|
|
|
var content;
|
2012-01-16 23:45:15 +01:00
|
|
|
var mime_type = drag.mime.split("/");
|
2012-01-12 13:22:22 +01:00
|
|
|
switch (mime_type[0]) {
|
|
|
|
case "image":
|
2012-01-15 16:17:50 +01:00
|
|
|
content = '<img src="' + file_name + '"></img>';
|
2012-01-12 13:22:22 +01:00
|
|
|
break;
|
|
|
|
case "video":
|
2012-01-15 16:17:50 +01:00
|
|
|
content = '<video src="' + file_name + '" controls="true"></video>';
|
2012-01-12 13:22:22 +01:00
|
|
|
break;
|
|
|
|
case "audio":
|
2012-01-15 16:17:50 +01:00
|
|
|
content = '<audio src="' + file_name + '" controls="true"></audio>';
|
2012-01-12 13:22:22 +01:00
|
|
|
break;
|
|
|
|
case "text":
|
2012-02-11 01:35:25 +01:00
|
|
|
file_contents = fs.readFileSync(__dirname + "/public/upload/" + drag.name, 'utf8');
|
|
|
|
content = '<pre>' + escapeHTML(file_contents) + '</pre>';
|
2012-01-12 14:21:35 +01:00
|
|
|
break;
|
2012-02-14 18:48:02 +01:00
|
|
|
case "application": // FIXME: treat as code for now, but it is probably wrong
|
2012-02-11 01:35:25 +01:00
|
|
|
file_contents = fs.readFileSync(__dirname + "/public/upload/" + drag.name, 'utf8');
|
|
|
|
content = '<pre><code class="' + drag.type + '">' +
|
|
|
|
escapeHTML(file_contents) +
|
2012-01-15 16:17:50 +01:00
|
|
|
'</code></pre>';
|
2012-01-12 14:21:35 +01:00
|
|
|
break;
|
2012-01-12 13:22:22 +01:00
|
|
|
default:
|
2012-01-15 16:17:50 +01:00
|
|
|
content = '<span>Unknown type: ' + mime_type + '</span>';
|
2012-01-12 13:22:22 +01:00
|
|
|
}
|
2012-01-15 16:17:50 +01:00
|
|
|
|
|
|
|
// Wrap the content in a div with title and comments.
|
|
|
|
res.send('<div class="draggable" id="' + drag_id + '" ' +
|
|
|
|
'style="' + default_style + '">' +
|
2012-01-16 10:57:37 +01:00
|
|
|
'<h2><span class="title">' + title + '</span>' +
|
2012-02-11 12:31:52 +01:00
|
|
|
'<div class="delete" title="Delete…">⨯</div>' +
|
|
|
|
'<div class="download" title="Download…">↓</div>' +
|
2012-02-14 18:48:02 +01:00
|
|
|
'</h2>' + content + '<div class="comments">Comments (0)</div>' +
|
2012-01-15 16:17:50 +01:00
|
|
|
'</div>');
|
2012-01-12 11:38:40 +01:00
|
|
|
});
|
|
|
|
|
2012-01-12 12:23:52 +01:00
|
|
|
// The position save controller: access through AJAX request by the main
|
2012-01-12 11:38:40 +01:00
|
|
|
// page for committing position changes of the draggables to the database,
|
|
|
|
// i.e. the global state.
|
|
|
|
app.post('/draggables/:id', function(req, res) {
|
2012-01-15 23:44:56 +01:00
|
|
|
var drag = draggables[req.params.id];
|
|
|
|
if (req.body.title) {
|
|
|
|
// It's a title update!
|
|
|
|
console.log("Title update for draggable " + req.params.id + ": " +
|
|
|
|
req.body.title);
|
|
|
|
drag.title = req.body.title;
|
2012-02-09 10:23:29 +01:00
|
|
|
addEvent("title update", { id: req.params.id, title: req.body.title });
|
2012-01-15 23:44:56 +01:00
|
|
|
res.send(req.body.title);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Set the position for the file with the given ID.
|
|
|
|
console.log("Position update for draggable " + req.params.id + ";" +
|
|
|
|
" left: " + req.body.left +
|
|
|
|
" top: " + req.body.top);
|
|
|
|
drag.top = req.body.top;
|
|
|
|
drag.left = req.body.left;
|
2012-02-09 10:23:29 +01:00
|
|
|
addEvent("reposition", { id: req.params.id, top: drag.top, left: drag.left });
|
2012-01-15 23:44:56 +01:00
|
|
|
}
|
2012-01-12 11:38:40 +01:00
|
|
|
});
|
|
|
|
|
2012-01-15 17:14:18 +01:00
|
|
|
// Draggable removal controller: removes the specific draggable from the
|
|
|
|
// database.
|
|
|
|
app.del('/draggables/:id', function(req, res) {
|
2012-02-09 10:25:55 +01:00
|
|
|
var drag_id = req.params.id;
|
|
|
|
var drag = draggables[drag_id];
|
|
|
|
fs.unlink(__dirname + "/public/upload/" + drag.name, function(err) {
|
2012-01-15 17:14:18 +01:00
|
|
|
if (err) {
|
2012-02-09 10:25:55 +01:00
|
|
|
console.log("Something went wrong while deleting draggable " +
|
|
|
|
drag_id + ": " + err);
|
2012-01-15 17:14:18 +01:00
|
|
|
res.send(err);
|
|
|
|
throw err;
|
2012-02-09 10:25:55 +01:00
|
|
|
return;
|
2012-01-15 17:14:18 +01:00
|
|
|
}
|
2012-02-09 10:23:29 +01:00
|
|
|
delete draggables[drag_id];
|
|
|
|
console.log("Deleted draggable " + drag_id);
|
|
|
|
addEvent("delete", { id: req.params.id });
|
2012-01-15 17:14:18 +01:00
|
|
|
res.send(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-01-15 14:03:33 +01:00
|
|
|
// Signal handling.
|
|
|
|
process.on('SIGINT', function() { db.save(draggables); process.exit(0); });
|
|
|
|
process.on('SIGTERM', function() { db.save(draggables); process.exit(0); });
|
|
|
|
|
2012-01-11 15:06:29 +01:00
|
|
|
// Start the application.
|
|
|
|
app.listen(3300);
|
2012-01-16 11:34:19 +01:00
|
|
|
console.log('Plemp! started on http://127.0.0.1:%d/ in %s mode',
|
2012-01-16 23:45:15 +01:00
|
|
|
app.address().port, app.settings.env);
|