add paste filter

This commit is contained in:
jkhsjdhjs 2018-02-21 04:48:06 +01:00
parent 2f1169d95b
commit b46f972da5

View File

@ -4,10 +4,11 @@ import highlight from "highlight.js";
import db from "../../lib/sql"; import db from "../../lib/sql";
const queries = { const queries = {
all: "select p.id, p.uuid, p.stamp, p.desc, p.paste, p.hidden, l.name as language, r.uuid as reply from `pastes` as p " all: filter => "select p.id, p.uuid, p.stamp, p.desc, p.paste, p.hidden, l.name as language, r.uuid as reply from `pastes` as p "
+ "left join `languages` as l on l.id = p.lang " + "left join `languages` as l on l.id = p.lang "
+ "left join `pastes` as r on r.id = p.reply " + "left join `pastes` as r on r.id = p.reply "
+ "where p.hidden = 0 " + "where p.hidden = 0 "
+ (filter || "")
+ "order by p.id desc limit 5", + "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 " 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 `languages` as l on l.id = p.lang "
@ -17,7 +18,7 @@ const queries = {
export default express.Router() export default express.Router()
.get("/", (req, res) => { .get("/", (req, res) => {
db.exec(queries.all) db.exec(queries.all())
.then(rows => { .then(rows => {
res.render("view", { pastes: rows.map(r => { res.render("view", { pastes: rows.map(r => {
const paste = new Buffer(r.paste, "base64").toString("ascii"); const paste = new Buffer(r.paste, "base64").toString("ascii");
@ -48,4 +49,35 @@ export default express.Router()
rows: paste.split(/\r?\n/).length rows: paste.split(/\r?\n/).length
}}); }});
}).catch(err => res.send("paste not found")); }).catch(err => res.send("paste not found"));
}); })
.get("/:filter(lang|desc)/:fvalue", (req, res) => {
const condition = {
desc: "and p.desc like ? ",
lang: "and l.name = ? "
};
let value = req.params.fvalue;
if(req.params.filter == "desc")
value = "%" + value + "%";
db.exec(queries.all(condition[req.params.filter]), value)
.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,
stamp: r.stamp,
desc: r.reply ? `Reply to <a href="/${r.reply}/" class="descID">/${r.reply}/</a>` : r.desc || "No desciption.",
rows: paste.split(/\r?\n/).length
};
})});
}).catch(err => res.render("view"));
})
.post("/", (req, res) => {
const desc = /^"(.*)"$/;
let match;
if(match = desc.exec(req.body.filter))
res.redirect("/v/desc/" + encodeURIComponent(match[1]));
else
res.redirect("/v/lang/" + encodeURIComponent(req.body.filter));
});