2017-06-01 19:41:29 +00:00
|
|
|
const express = require('express');
|
|
|
|
const busboy = require('connect-busboy');
|
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const bodyParser = require('body-parser');
|
|
|
|
const crypto = require('crypto');
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
const redis = require('redis'), client = redis.createClient();
|
|
|
|
|
|
|
|
client.on('error', function(err) {
|
2017-05-24 20:16:38 +00:00
|
|
|
console.log(err);
|
2017-06-01 19:41:29 +00:00
|
|
|
});
|
2017-05-24 20:16:38 +00:00
|
|
|
|
2017-05-30 16:24:16 +00:00
|
|
|
app.use(busboy());
|
2017-05-31 21:08:13 +00:00
|
|
|
app.use(bodyParser.json());
|
2017-06-01 19:41:29 +00:00
|
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
2017-05-24 20:16:38 +00:00
|
|
|
|
2017-06-01 19:41:29 +00:00
|
|
|
app.get('/download/:id', function(req, res) {
|
|
|
|
res.sendFile(path.join(__dirname + '/public/download.html'));
|
2017-05-24 20:16:38 +00:00
|
|
|
});
|
|
|
|
|
2017-06-01 19:41:29 +00:00
|
|
|
app.get('/assets/download/:id', function(req, res) {
|
2017-06-01 17:05:09 +00:00
|
|
|
let id = req.params.id;
|
2017-06-01 19:41:29 +00:00
|
|
|
if (!validateID(id)) {
|
2017-06-01 16:55:47 +00:00
|
|
|
res.send(404);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-01 19:41:29 +00:00
|
|
|
client.hget(id, 'filename', function(err, reply) {
|
|
|
|
// maybe some expiration logic too
|
2017-05-31 21:08:13 +00:00
|
|
|
if (!reply) {
|
2017-05-31 16:57:02 +00:00
|
|
|
res.sendStatus(404);
|
2017-05-24 20:16:38 +00:00
|
|
|
} else {
|
2017-06-01 19:41:29 +00:00
|
|
|
res.setHeader('Content-Disposition', 'attachment; filename=' + reply);
|
|
|
|
res.setHeader('Content-Type', 'application/octet-stream');
|
|
|
|
|
|
|
|
res.download(__dirname + '/static/' + id, reply, function(err) {
|
2017-05-31 16:57:02 +00:00
|
|
|
if (!err) {
|
|
|
|
client.del(id);
|
2017-06-01 19:41:29 +00:00
|
|
|
fs.unlinkSync(__dirname + '/static/' + id);
|
2017-05-31 16:57:02 +00:00
|
|
|
}
|
|
|
|
});
|
2017-05-24 20:16:38 +00:00
|
|
|
}
|
2017-06-01 19:41:29 +00:00
|
|
|
});
|
2017-05-30 16:24:16 +00:00
|
|
|
});
|
2017-05-24 20:16:38 +00:00
|
|
|
|
2017-06-01 19:41:29 +00:00
|
|
|
app.post('/delete/:id', function(req, res) {
|
2017-05-31 21:08:13 +00:00
|
|
|
let id = req.params.id;
|
2017-06-01 16:55:47 +00:00
|
|
|
|
2017-06-01 19:41:29 +00:00
|
|
|
if (!validateID(id)) {
|
2017-06-01 16:55:47 +00:00
|
|
|
res.send(404);
|
|
|
|
return;
|
|
|
|
}
|
2017-06-01 19:41:29 +00:00
|
|
|
|
2017-05-31 21:08:13 +00:00
|
|
|
let delete_token = req.body.delete_token;
|
2017-06-01 19:41:29 +00:00
|
|
|
|
|
|
|
if (!delete_token) {
|
2017-05-31 21:08:13 +00:00
|
|
|
res.sendStatus(404);
|
|
|
|
}
|
|
|
|
|
2017-06-01 19:41:29 +00:00
|
|
|
client.hget(id, 'delete', function(err, reply) {
|
2017-05-31 21:08:13 +00:00
|
|
|
if (!reply) {
|
|
|
|
res.sendStatus(404);
|
|
|
|
} else {
|
|
|
|
client.del(id);
|
2017-06-01 19:41:29 +00:00
|
|
|
fs.unlinkSync(__dirname + '/static/' + id);
|
2017-05-31 21:08:13 +00:00
|
|
|
res.sendStatus(200);
|
|
|
|
}
|
2017-06-01 19:41:29 +00:00
|
|
|
});
|
2017-05-31 21:08:13 +00:00
|
|
|
});
|
|
|
|
|
2017-06-01 19:41:29 +00:00
|
|
|
app.post('/upload/:id', function(req, res, next) {
|
|
|
|
if (!validateID(req.params.id)) {
|
|
|
|
res.send(404);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var fstream;
|
|
|
|
req.pipe(req.busboy);
|
|
|
|
req.busboy.on('file', function(fieldname, file, filename) {
|
|
|
|
console.log('Uploading: ' + filename);
|
|
|
|
|
|
|
|
//Path where image will be uploaded
|
|
|
|
fstream = fs.createWriteStream(__dirname + '/static/' + req.params.id);
|
|
|
|
file.pipe(fstream);
|
|
|
|
fstream.on('close', function() {
|
|
|
|
let id = req.params.id;
|
|
|
|
let uuid = crypto.randomBytes(10).toString('hex');
|
2017-05-24 20:16:38 +00:00
|
|
|
|
2017-06-01 19:41:29 +00:00
|
|
|
client.hmset([id, 'filename', filename, 'delete', uuid]);
|
|
|
|
|
|
|
|
// delete the file off the server in 24 hours
|
|
|
|
// setTimeout(function() {
|
|
|
|
// fs.unlinkSync(__dirname + "/static/" + id);
|
|
|
|
// }, 86400000);
|
|
|
|
|
|
|
|
client.expire(id, 86400000);
|
|
|
|
console.log('Upload Finished of ' + filename);
|
|
|
|
res.send(uuid);
|
2017-05-30 16:24:16 +00:00
|
|
|
});
|
2017-06-01 19:41:29 +00:00
|
|
|
});
|
2017-06-01 16:07:11 +00:00
|
|
|
});
|
2017-05-24 20:16:38 +00:00
|
|
|
|
2017-06-01 19:41:29 +00:00
|
|
|
app.listen(3000, function() {
|
|
|
|
console.log('Portal app listening on port 3000!');
|
|
|
|
});
|
2017-05-24 20:16:38 +00:00
|
|
|
|
2017-06-01 16:55:47 +00:00
|
|
|
function validateID(route_id) {
|
|
|
|
return route_id.match(/^[0-9a-fA-F]{32}$/) !== null;
|
2017-06-01 19:41:29 +00:00
|
|
|
}
|