2017-06-01 20:14:14 +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();
|
|
|
|
|
2017-06-01 22:10:00 +00:00
|
|
|
client.on("error", (err) => {
|
2017-06-01 20:14:14 +00:00
|
|
|
console.log(err);
|
|
|
|
})
|
|
|
|
|
|
|
|
app.use(busboy());
|
|
|
|
app.use(bodyParser.json());
|
|
|
|
app.use(express.static(path.join(__dirname, "../public")));
|
|
|
|
|
2017-06-01 22:10:00 +00:00
|
|
|
app.get("/download/:id", (req, res) => {
|
2017-06-01 20:14:14 +00:00
|
|
|
res.sendFile(path.join(__dirname + "/../public/download.html"));
|
|
|
|
});
|
|
|
|
|
2017-06-01 22:10:00 +00:00
|
|
|
app.get("/assets/download/:id", (req, res) => {
|
|
|
|
|
2017-06-01 20:14:14 +00:00
|
|
|
let id = req.params.id;
|
|
|
|
if (!validateID(id)){
|
|
|
|
res.send(404);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-01 22:10:00 +00:00
|
|
|
client.hget(id, "filename", (err, reply) => { // maybe some expiration logic too
|
2017-06-01 20:14:14 +00:00
|
|
|
if (!reply) {
|
|
|
|
res.sendStatus(404);
|
|
|
|
} else {
|
|
|
|
res.setHeader("Content-Disposition", "attachment; filename=" + reply);
|
|
|
|
res.setHeader("Content-Type", "application/octet-stream");
|
|
|
|
|
2017-06-01 22:10:00 +00:00
|
|
|
res.download(__dirname + "/../static/" + id, reply, (err) => {
|
2017-06-01 20:14:14 +00:00
|
|
|
if (!err) {
|
|
|
|
client.del(id);
|
|
|
|
fs.unlinkSync(__dirname + "/../static/" + id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2017-06-01 22:10:00 +00:00
|
|
|
app.post("/delete/:id", (req, res) => {
|
2017-06-01 20:14:14 +00:00
|
|
|
let id = req.params.id;
|
|
|
|
|
|
|
|
if (!validateID(id)){
|
|
|
|
res.send(404);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let delete_token = req.body.delete_token;
|
|
|
|
|
|
|
|
if (!delete_token){
|
|
|
|
res.sendStatus(404);
|
|
|
|
}
|
|
|
|
|
2017-06-01 22:10:00 +00:00
|
|
|
client.hget(id, "delete", (err, reply) => {
|
2017-06-01 20:14:14 +00:00
|
|
|
if (!reply) {
|
|
|
|
res.sendStatus(404);
|
|
|
|
} else {
|
|
|
|
client.del(id);
|
|
|
|
fs.unlinkSync(__dirname + "/../static/" + id);
|
|
|
|
res.sendStatus(200);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2017-06-01 22:10:00 +00:00
|
|
|
app.post("/upload/:id", (req, res, next) => {
|
2017-06-01 20:14:14 +00:00
|
|
|
|
|
|
|
if (!validateID(req.params.id)){
|
|
|
|
res.send(404);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-01 22:10:00 +00:00
|
|
|
let fstream;
|
2017-06-01 20:14:14 +00:00
|
|
|
req.pipe(req.busboy);
|
2017-06-01 22:10:00 +00:00
|
|
|
req.busboy.on("file", (fieldname, file, filename) => {
|
2017-06-01 20:14:14 +00:00
|
|
|
console.log("Uploading: " + filename);
|
|
|
|
|
|
|
|
//Path where image will be uploaded
|
|
|
|
fstream = fs.createWriteStream(__dirname + "/../static/" + req.params.id);
|
|
|
|
file.pipe(fstream);
|
2017-06-01 22:10:00 +00:00
|
|
|
fstream.on("close", () => {
|
2017-06-01 20:14:14 +00:00
|
|
|
let id = req.params.id;
|
|
|
|
let uuid = crypto.randomBytes(10).toString('hex');
|
|
|
|
|
|
|
|
client.hmset([id, "filename", filename, "delete", uuid]);
|
|
|
|
|
|
|
|
// delete the file off the server in 24 hours
|
2017-06-01 22:10:00 +00:00
|
|
|
// setTimeout(() => {
|
2017-06-01 20:14:14 +00:00
|
|
|
// fs.unlinkSync(__dirname + "/static/" + id);
|
|
|
|
// }, 86400000);
|
|
|
|
|
|
|
|
client.expire(id, 86400000);
|
|
|
|
console.log("Upload Finished of " + filename);
|
|
|
|
res.send(uuid);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-01 22:10:00 +00:00
|
|
|
app.listen(3000, () => {
|
2017-06-01 20:14:14 +00:00
|
|
|
console.log("Portal app listening on port 3000!")
|
|
|
|
})
|
|
|
|
|
2017-06-01 22:10:00 +00:00
|
|
|
let validateID = (route_id) => {
|
2017-06-01 20:14:14 +00:00
|
|
|
return route_id.match(/^[0-9a-fA-F]{32}$/) !== null;
|
|
|
|
}
|