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")
|
|
|
|
, form = require("connect-form")
|
|
|
|
, fs = require('fs')
|
|
|
|
, path = require('path')
|
|
|
|
, crypto = require("crypto")
|
|
|
|
, mime = require("mime")
|
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-01-12 11:38:40 +01:00
|
|
|
// FIXME: dummy database
|
2012-01-11 17:02:22 +01:00
|
|
|
var draggables = {};
|
|
|
|
// Initialise the draggables info.
|
2012-01-12 11:15:41 +01:00
|
|
|
fs.readdir(__dirname + '/public/upload', function (err, files) {
|
2012-01-11 17:02:22 +01:00
|
|
|
if (err)
|
|
|
|
throw(err)
|
|
|
|
for (var i in files) {
|
|
|
|
if (files[i][0] == ".")
|
|
|
|
continue;
|
2012-01-12 13:22:22 +01:00
|
|
|
draggables[files[i]] = { mime: mime.lookup(files[i]),
|
|
|
|
top: 200,
|
|
|
|
left: 350 };
|
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);
|
|
|
|
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
|
|
|
|
});
|
|
|
|
|
|
|
|
// Server the main index file statically for now.
|
|
|
|
app.get('/', function(req, res) {
|
|
|
|
res.redirect('/index.html');
|
|
|
|
});
|
|
|
|
|
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-01-11 23:30:00 +01:00
|
|
|
res.send(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) {
|
|
|
|
next(err);
|
|
|
|
}
|
|
|
|
else {
|
2012-01-12 12:23:08 +01:00
|
|
|
var file_id, file_name, file_mime;
|
|
|
|
if (fields.text) {
|
|
|
|
md5sum = crypto.createHash('md5');
|
|
|
|
file_id = md5sum.update(fields.text).digest('hex') + ".txt"
|
|
|
|
file_name = "public/upload/" + file_id;
|
|
|
|
file_mime = 'text/plain';
|
|
|
|
fs.writeFile(file_name, fields.text, function (err) {
|
|
|
|
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);
|
|
|
|
file_id = path.basename(files.file.path);
|
|
|
|
file_name = files.file.filename;
|
2012-01-12 13:22:22 +01:00
|
|
|
file_mime = files.file.mime;
|
2012-01-12 12:23:08 +01:00
|
|
|
}
|
|
|
|
draggables[file_id] = { name: file_name,
|
|
|
|
mime: file_mime,
|
2012-01-12 11:38:40 +01:00
|
|
|
top: 200,
|
|
|
|
left: 350 };
|
2012-01-11 15:06:29 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
res.redirect('home');
|
|
|
|
});
|
|
|
|
|
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;
|
|
|
|
var file_name = "../upload/" + req.params.id;
|
|
|
|
console.log("Get draggable: " + drag_id);
|
2012-01-12 11:38:40 +01:00
|
|
|
// Stuff taken from the Camping implementation.
|
2012-01-12 13:22:22 +01:00
|
|
|
var drag = draggables[drag_id];
|
2012-01-12 11:38:40 +01:00
|
|
|
var default_style = "left:" + drag.left + "px;top:" + drag.top + "px;";
|
2012-01-12 13:22:22 +01:00
|
|
|
var mime_type = drag.mime.split("/")
|
|
|
|
switch (mime_type[0]) {
|
|
|
|
case "image":
|
|
|
|
res.send('<img class="draggable" id="' + drag_id + '" ' +
|
|
|
|
'style="' + default_style + '" src="' + file_name + '">' +
|
|
|
|
'</img>');
|
|
|
|
break;
|
|
|
|
case "video":
|
|
|
|
res.send('<video class="draggable" id="' + drag_id + '" ' +
|
|
|
|
'style="' + default_style + '" src="' + file_name +
|
|
|
|
'" controls="true"></video>');
|
|
|
|
break;
|
|
|
|
case "audio":
|
|
|
|
res.send('<audio class="draggable" id="' + drag_id + '" ' +
|
|
|
|
'style="' + default_style + ';height=80px;" src="' +
|
|
|
|
file_name +
|
|
|
|
'" controls="true"></audio>');
|
|
|
|
break;
|
|
|
|
case "text":
|
|
|
|
file_contents = fs.readFileSync("public/upload/" + drag_id);
|
|
|
|
res.send('<div class="draggable" id="' + drag_id + '" ' +
|
|
|
|
'style="' + default_style + '"><pre>' + file_contents +
|
|
|
|
'</pre></div>');
|
2012-01-12 14:21:35 +01:00
|
|
|
break;
|
|
|
|
case 'application': // FIXME: treat as code for now, but it is probably wrong
|
|
|
|
file_contents = fs.readFileSync("public/upload/" + drag_id);
|
|
|
|
res.send('<div class="draggable" id="' + drag_id + '" ' +
|
|
|
|
'style="' + default_style + '"><pre><code>' + file_contents +
|
|
|
|
'</code></pre></div>');
|
|
|
|
break;
|
2012-01-12 13:22:22 +01:00
|
|
|
default:
|
|
|
|
res.send('<div class="draggable" id="' + drag_id + '" ' +
|
2012-01-12 14:21:35 +01:00
|
|
|
'style="' + default_style + '">Unknown type: ' +
|
|
|
|
mime_type + '</div>');
|
2012-01-12 13:22:22 +01:00
|
|
|
}
|
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-12 13:22:22 +01:00
|
|
|
console.log("Position update for draggable " + req.params.id + ";" +
|
2012-01-12 11:38:40 +01:00
|
|
|
" left: " + req.body.left +
|
|
|
|
" top: " + req.body.top);
|
|
|
|
// Set the position for the file with the given ID.
|
|
|
|
var new_pos = draggables[req.params.id];
|
|
|
|
new_pos.top = req.body.top;
|
|
|
|
new_pos.left = req.body.left;
|
|
|
|
});
|
|
|
|
|
2012-01-11 15:06:29 +01:00
|
|
|
// Start the application.
|
|
|
|
app.listen(3300);
|
|
|
|
console.log('Plemp! started on http://127.0.0.1:%d/', app.address().port)
|