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.
|
|
|
|
|
|
|
|
var express = require("express"),
|
|
|
|
form = require("connect-form"),
|
|
|
|
fs = require('fs'),
|
|
|
|
util = require("util")
|
|
|
|
|
|
|
|
// 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-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-11 23:28:21 +01:00
|
|
|
draggables[files[i]] = {left: 350, top: 200};
|
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-11 17:02:45 +01:00
|
|
|
// The draggable route: provides direct access to the HTML generating code
|
|
|
|
// for draggable objects.
|
|
|
|
app.get('/draggable/:id', function(req, res) {
|
2012-01-12 11:15:41 +01:00
|
|
|
var file = "public/upload/" + req.params.id;
|
2012-01-11 23:29:41 +01:00
|
|
|
// Stuff taken from the Camping implementation.
|
|
|
|
drag = draggables[req.params.id];
|
|
|
|
var default_style = "left:" + drag.left + "px;top:" + drag.top + "px;";
|
|
|
|
console.log("Draggable: " + file);
|
|
|
|
res.send('<img class="draggable" id="' + req.params.id + '" ' +
|
|
|
|
'style="' + default_style + '" src="' + file + '">');
|
2012-01-11 17:02:45 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// The current route: accessed through AJAX requests by the main page
|
2012-01-11 15:06:29 +01:00
|
|
|
// for getting the current (global) state (positions) of the draggables.
|
|
|
|
app.get('/current', function(req, res) {
|
|
|
|
// 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
|
|
|
});
|
|
|
|
|
|
|
|
// The position save route: access through AJAX request by the main page
|
|
|
|
// for committing position changes of the draggables to the database, i.e. the
|
|
|
|
// global state.
|
2012-01-11 23:30:19 +01:00
|
|
|
app.post('/savepos/:id', function(req, res) {
|
2012-01-11 15:06:29 +01:00
|
|
|
// Set the position for the file with the given ID.
|
2012-01-11 23:30:19 +01:00
|
|
|
console.log("Got position for " + req.params.id + ";" +
|
|
|
|
" left: " + req.body.left +
|
|
|
|
" top: " + req.body.top);
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
// The upload route: 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('/upload', function(req, res, next) {
|
|
|
|
req.form.complete(function(err, fields, files) {
|
|
|
|
if (err) {
|
|
|
|
next(err);
|
|
|
|
}
|
|
|
|
else {
|
2012-01-11 17:02:22 +01:00
|
|
|
draggables[files.file.filename] = [350,200];
|
2012-01-11 15:19:29 +01:00
|
|
|
console.log('File %s uploaded to %s', files.file.filename,
|
|
|
|
files.file.path);
|
2012-01-11 15:06:29 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
res.redirect('home');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Start the application.
|
|
|
|
app.listen(3300);
|
|
|
|
console.log('Plemp! started on http://127.0.0.1:%d/', app.address().port)
|