2017-05-24 20:16:38 +00:00
|
|
|
const express = require('express')
|
2017-05-30 16:24:16 +00:00
|
|
|
var busboy = require('connect-busboy'); //middleware for form/file upload
|
|
|
|
var path = require('path'); //used for file path
|
|
|
|
var fs = require('fs-extra'); //File System - for file manipulation
|
2017-05-24 20:16:38 +00:00
|
|
|
const app = express()
|
|
|
|
var redis = require("redis"),
|
|
|
|
client = redis.createClient();
|
|
|
|
|
|
|
|
client.on('error', function(err) {
|
|
|
|
console.log(err);
|
|
|
|
})
|
|
|
|
|
2017-05-30 16:24:16 +00:00
|
|
|
app.use(busboy());
|
|
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
2017-05-24 20:16:38 +00:00
|
|
|
|
2017-05-30 16:24:16 +00:00
|
|
|
app.get('/', function (req, res) {
|
|
|
|
res.send('Hello World!')
|
|
|
|
})
|
2017-05-24 20:16:38 +00:00
|
|
|
|
2017-05-30 16:24:16 +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-05-30 16:24:16 +00:00
|
|
|
app.get('/assets/download/:id', function(req, res) {
|
|
|
|
|
2017-05-24 20:16:38 +00:00
|
|
|
let id = req.params.id;
|
2017-05-30 16:24:16 +00:00
|
|
|
client.hget(id, "filename", function(err, reply) { // maybe some expiration logic too
|
2017-05-24 20:16:38 +00:00
|
|
|
if (!reply) {
|
|
|
|
res.send('error');
|
|
|
|
} else {
|
2017-05-30 16:24:16 +00:00
|
|
|
res.setHeader('Content-Disposition', 'attachment; filename=' + reply);
|
|
|
|
res.setHeader('Content-Type', 'application/octet-stream');
|
2017-05-24 20:16:38 +00:00
|
|
|
|
2017-05-30 16:24:16 +00:00
|
|
|
res.download(__dirname + '/static/' + reply);
|
2017-05-24 20:16:38 +00:00
|
|
|
}
|
|
|
|
})
|
2017-05-30 16:24:16 +00:00
|
|
|
|
|
|
|
});
|
2017-05-24 20:16:38 +00:00
|
|
|
|
2017-05-30 20:55:06 +00:00
|
|
|
app.route('/upload/:id')
|
2017-05-30 16:24:16 +00:00
|
|
|
.post(function (req, res, next) {
|
2017-05-24 20:16:38 +00:00
|
|
|
|
2017-05-30 16:24:16 +00:00
|
|
|
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/' + filename);
|
|
|
|
file.pipe(fstream);
|
|
|
|
fstream.on('close', function () {
|
2017-05-30 20:55:06 +00:00
|
|
|
let id = req.params.id;
|
2017-05-30 16:24:16 +00:00
|
|
|
client.hset(id, "filename", filename, redis.print);
|
|
|
|
client.hset(id, "expiration", 0, redis.print);
|
|
|
|
console.log("Upload Finished of " + filename);
|
2017-05-30 21:41:31 +00:00
|
|
|
res.send(id);
|
2017-05-30 16:24:16 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-05-24 20:16:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.listen(3000, function () {
|
2017-05-30 21:41:31 +00:00
|
|
|
console.log('Portal app listening on port 3000!')
|
2017-05-24 20:16:38 +00:00
|
|
|
})
|
|
|
|
|