toothpaste/app.js
2019-07-19 13:56:37 +02:00

175 lines
4.2 KiB
JavaScript

var bodyParser = require('body-parser');
var express = require('express');
var fs = require('fs');
var connection = require("./js/mysql.js");
var mysql = require("mysql");
var rhg = require('random-hash-generator');
var pastesThisSession = 0;
function PlayRocketLeague() {
pastesThisSession++;
return new Date().toString() + pastesThisSession / 1000;
}
var app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
const PORT = 8077;
var types;
function query(str, callback) {
query(str, [], callback);
}
function query(str, arg, callback) {
var isConnected = true;
connection.query(str, arg, callback);
}
query("SELECT * FROM types", function(err, result) {
types = result;
});
app.port = PORT;
app.host = "0.0.0.0";
function generateId(data) {
pastesThisSession++;
return rhg.calc(data, 8, PlayRocketLeague()).replace('/', '-').replace('+', '_');
}
function getContentId(data, callback) {
var id = generateId(data);
query("SELECT id FROM pastes WHERE id = '?'", [id], function(err, result) {
if (err === null && result.length > 0) {
getContentId(data, callback);
}
else {
callback(id);
}
});
}
function insertPaste(data, type, callback) {
if (data !== null && type !== null) {
query("SELECT * FROM types WHERE id = ?", [type], function(err, result) {
if (err === null && result.length > 0) {
getContentId(data, function(id) {
query("INSERT INTO pastes (id, type, content) VALUES (?, ?, ?)", [id, type, data], function(err, result) {
if (err === null) {
callback(id, null);
}
});
});
}
});
}
else {
callback(null, "Content or Type not defined.");
}
}
function readFile(path, callback) {
if (!path.match(/\.\./g)) {
fs.readFile('./static/' + path, (err, data) => {
if (err) {
readFile("404.html", callback);
}
else {
callback(data);
}
});
}
else {
callback("Nope");
}
}
function handleRequest(request, response) {
console.log(request.params.path);
var type = request.params.option;
var id = request.params.path.match(/^[a-zA-Z 0-9\_\-]*$/g);
console.log(id);
if (id !== null && id[0] != null && id[0].length == 8) {
query("SELECT * FROM pastes WHERE id = ?", [id], function(err, result) {
if (err === null && result.length > 0) {
if (type == "raw") {
response.end(result[0].content);
}
else {
readFile("get.html", function(data) {
response.end(data.toString().replace("%JSONStuff%", JSON.stringify({
types: types,
type: result[0].type,
pasteId: result[0].id,
content: result[0].content
})));
});
}
}
else {
readFile("404.html", function(data) {
response.end(data);
});
}
});
}
else {
readFile("404.html", function(data) {
response.end(data);
});
}
}
app.get("/", function(request, response) {
readFile("insert.html", function(data) {
response.end(data.toString().replace("%JSONStuff%", JSON.stringify({
types: types
})));
});
});
app.get("/static/:filename", function(request, response) {
readFile(request.params.filename, function(data) {
response.end(data);
});
});
app.post("/fritten", function(request, response) {
insertPaste(request.body.content, request.body.type, function(id, error) {
response.status(200).json({
id: id,
err: error
});
});
});
app.get("/:path", handleRequest);
app.get("/:path/:option", handleRequest);
app.listen(PORT, "127.0.0.1", 511, function() {
console.log("Static Webserver started on port "+PORT);
});