2017-06-06 00:35:36 +02: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 conf = require('./config.js');
|
|
|
|
const stream = require('stream');
|
|
|
|
|
|
|
|
let aws_credentials = conf.get('aws_credentials');
|
|
|
|
|
|
|
|
const AWS = require('aws-sdk');
|
|
|
|
AWS.config.loadFromPath('../../.aws/credentials');
|
|
|
|
const s3 = new AWS.S3();
|
|
|
|
|
|
|
|
|
|
|
|
const app = express()
|
|
|
|
const redis = require("redis"),
|
|
|
|
redis_client = redis.createClient();
|
|
|
|
|
|
|
|
redis_client.on("error", (err) => {
|
2017-06-01 22:14:14 +02:00
|
|
|
console.log(err);
|
2017-06-06 23:24:51 +02:00
|
|
|
});
|
2017-06-01 22:14:14 +02:00
|
|
|
|
|
|
|
app.use(busboy());
|
|
|
|
app.use(bodyParser.json());
|
2017-06-06 23:24:51 +02:00
|
|
|
app.use(express.static(path.join(__dirname, '../public')));
|
2017-06-01 22:14:14 +02:00
|
|
|
|
2017-06-06 23:24:51 +02:00
|
|
|
app.get('/download/:id', (req, res) => {
|
|
|
|
res.sendFile(path.join(__dirname + '/../public/download.html'));
|
2017-06-01 22:14:14 +02:00
|
|
|
});
|
|
|
|
|
2017-06-06 23:24:51 +02:00
|
|
|
app.get('/assets/download/:id', (req, res) => {
|
2017-06-01 22:14:14 +02:00
|
|
|
let id = req.params.id;
|
2017-06-06 23:24:51 +02:00
|
|
|
if (!validateID(id)) {
|
2017-06-01 22:14:14 +02:00
|
|
|
res.send(404);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-06 00:35:36 +02:00
|
|
|
let params = {
|
|
|
|
Bucket: aws_credentials.bucketName,
|
|
|
|
Key: req.params.id
|
|
|
|
}
|
2017-06-06 23:24:51 +02:00
|
|
|
|
2017-06-06 00:35:36 +02:00
|
|
|
s3.getObject(params, function(err, data) {
|
|
|
|
if (err) {
|
|
|
|
console.log(err, err.stack); // an error occurred
|
2017-06-01 22:14:14 +02:00
|
|
|
}
|
2017-06-06 00:35:36 +02:00
|
|
|
else {
|
|
|
|
res.writeHead(200, {"Content-Disposition": "attachment; filename=response", //+ reply,
|
|
|
|
"Content-Type": "application/octet-stream"});
|
|
|
|
// res.setHeader("Content-Type", "application/octet-stream");
|
|
|
|
res.end(new Buffer(data.Body))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// redis_client.hget(id, "filename", (err, reply) => { // maybe some expiration logic too
|
|
|
|
// if (!reply) {
|
|
|
|
// res.sendStatus(404);
|
|
|
|
// } else {
|
|
|
|
|
|
|
|
// res.download(__dirname + "/../static/" + id, reply, (err) => {
|
|
|
|
// if (!err) {
|
|
|
|
// redis_client.del(id);
|
|
|
|
// fs.unlinkSync(__dirname + "/../static/" + id);
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// })
|
2017-06-01 22:14:14 +02:00
|
|
|
});
|
|
|
|
|
2017-06-06 23:24:51 +02:00
|
|
|
app.post('/delete/:id', (req, res) => {
|
2017-06-01 22:14:14 +02:00
|
|
|
let id = req.params.id;
|
|
|
|
|
2017-06-06 23:24:51 +02:00
|
|
|
if (!validateID(id)) {
|
2017-06-01 22:14:14 +02:00
|
|
|
res.send(404);
|
|
|
|
return;
|
|
|
|
}
|
2017-06-06 23:24:51 +02:00
|
|
|
|
2017-06-01 22:14:14 +02:00
|
|
|
let delete_token = req.body.delete_token;
|
2017-06-06 23:24:51 +02:00
|
|
|
|
|
|
|
if (!delete_token) {
|
2017-06-01 22:14:14 +02:00
|
|
|
res.sendStatus(404);
|
|
|
|
}
|
|
|
|
|
2017-06-06 00:35:36 +02:00
|
|
|
<<<<<<< HEAD
|
2017-06-06 23:24:51 +02:00
|
|
|
client.hget(id, 'delete', (err, reply) => {
|
2017-06-01 22:14:14 +02:00
|
|
|
if (!reply) {
|
|
|
|
res.sendStatus(404);
|
|
|
|
} else {
|
|
|
|
client.del(id);
|
2017-06-06 23:24:51 +02:00
|
|
|
fs.unlinkSync(__dirname + '/../static/' + id);
|
2017-06-06 00:35:36 +02:00
|
|
|
=======
|
|
|
|
redis_client.hget(id, "delete", (err, reply) => {
|
|
|
|
if (!reply) {
|
|
|
|
res.sendStatus(404);
|
|
|
|
} else {
|
|
|
|
redis_client.del(id);
|
|
|
|
fs.unlinkSync(__dirname + "/../static/" + id);
|
|
|
|
>>>>>>> currently not working, decryption seems to fail
|
2017-06-01 22:14:14 +02:00
|
|
|
res.sendStatus(200);
|
|
|
|
}
|
2017-06-06 23:24:51 +02:00
|
|
|
});
|
2017-06-01 22:14:14 +02:00
|
|
|
});
|
|
|
|
|
2017-06-06 00:35:36 +02:00
|
|
|
app.post("/upload/:id", (req, res, next) => {
|
2017-06-06 23:24:51 +02:00
|
|
|
|
2017-06-06 00:35:36 +02:00
|
|
|
if (!validateID(req.params.id)){
|
|
|
|
res.send(404);
|
|
|
|
return;
|
|
|
|
}
|
2017-06-06 23:24:51 +02:00
|
|
|
|
2017-06-06 00:35:36 +02:00
|
|
|
let fstream;
|
|
|
|
req.pipe(req.busboy);
|
|
|
|
req.busboy.on("file", (fieldname, file, filename) => {
|
|
|
|
console.log("Uploading: " + filename);
|
2017-06-01 22:14:14 +02:00
|
|
|
|
2017-06-06 23:24:51 +02:00
|
|
|
|
2017-06-06 00:35:36 +02:00
|
|
|
let params = {
|
|
|
|
Bucket: aws_credentials.bucketName,
|
|
|
|
Key: req.params.id,
|
|
|
|
Body: file
|
|
|
|
}
|
2017-06-06 23:24:51 +02:00
|
|
|
|
2017-06-06 00:35:36 +02:00
|
|
|
s3.upload(params, function(err, data) {
|
|
|
|
if (err) {
|
|
|
|
console.log(err, err.stack); // an error occurred
|
|
|
|
} else {
|
|
|
|
console.log(data);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
fstream = fs.createWriteStream(__dirname + "/../static/" + req.params.id);
|
|
|
|
file.pipe(fstream);
|
|
|
|
fstream.on("close", () => {
|
|
|
|
let id = req.params.id;
|
|
|
|
let uuid = crypto.randomBytes(10).toString('hex');
|
|
|
|
|
|
|
|
redis_client.hmset([id, "filename", filename, "delete", uuid]);
|
|
|
|
|
|
|
|
// delete the file off the server in 24 hours
|
|
|
|
// setTimeout(() => {
|
|
|
|
// fs.unlinkSync(__dirname + "/static/" + id);
|
|
|
|
// }, 86400000);
|
|
|
|
|
|
|
|
redis_client.expire(id, 86400000);
|
|
|
|
console.log("Upload Finished of " + filename);
|
|
|
|
res.send(uuid);
|
|
|
|
});
|
2017-06-01 22:14:14 +02:00
|
|
|
});
|
2017-06-06 23:24:51 +02:00
|
|
|
});
|
2017-06-01 22:14:14 +02:00
|
|
|
});
|
|
|
|
|
2017-06-02 00:10:00 +02:00
|
|
|
app.listen(3000, () => {
|
2017-06-06 23:24:51 +02:00
|
|
|
console.log('Portal app listening on port 3000!');
|
|
|
|
});
|
2017-06-01 22:14:14 +02:00
|
|
|
|
2017-06-06 23:24:51 +02:00
|
|
|
let validateID = route_id => {
|
2017-06-01 22:14:14 +02:00
|
|
|
return route_id.match(/^[0-9a-fA-F]{32}$/) !== null;
|
2017-06-06 23:24:51 +02:00
|
|
|
};
|