import express from "express"; import highlight from "highlight.js"; import db from "../../lib/sql"; const queries = { all: "select p.id, p.uuid, p.stamp, p.desc, p.paste, l.name as language, r.uuid as reply from `pastes` as p " + "left join `languages` as l on l.id = p.lang " + "left join `pastes` as r on r.id = p.reply " + "order by p.id desc limit 5", single: "select p.id, p.uuid, p.stamp, p.desc, p.paste, l.name as language, r.uuid as reply from `pastes` as p " + "left join `languages` as l on l.id = p.lang " + "left join `pastes` as r on r.id = p.reply " + "where p.uuid = ? limit 1" }; export default express.Router() .get("/", (req, res) => { db.exec(queries.all) .then(rows => { res.render("view", { pastes: rows.map(r => { const paste = new Buffer(r.paste, "base64").toString("ascii"); return { uuid: r.uuid, paste: highlight.highlightAuto(paste).value, language: r.language, desc: r.reply ? `Reply to <a href="/${r.reply}/" class="descID">/${r.reply}/</a>` : "No description.", rows: paste.split(/\r?\n/).length }; })}); }).catch(err => res.render("view")); }) .get("/:uuid", (req, res) => { if(req.params.uuid.length !== 8) return res.send("error"); db.exec(queries.single, [req.params.uuid]) .then(rows => { const r = rows[0]; const paste = new Buffer(r.paste, "base64").toString("ascii"); res.render("viewsingle", { paste: { uuid: r.uuid, paste: highlight.highlightAuto(paste).value, language: r.lang, desc: r.desc, rows: paste.split(/\r?\n/).length }}); }).catch(err => res.send("paste not found")); });