var bodyParser = require('body-parser'); var express = require('express'); var fs = require('fs'); var connection = require("./js/mysql.js"); 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 = 8081; var types; function connect() { // if (connection !== undefined && connection !== null) { // connection.destroy(); // } connection.connect(function(err) { if (err) { console.log(err); connect(); } else { connection.on('error', function(err) { console.log('db error', err); if (err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually connect(); } else { // connnection idle timeout (the wait_timeout throw err; // server variable configures this) } }); } }); } connect(); 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 = "127.0.0.1"; 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() { });