2020-04-02 04:35:28 +02:00
|
|
|
import router from "../router.mjs";
|
|
|
|
import cfg from "../../../config.json";
|
2019-04-25 19:25:38 +00:00
|
|
|
import fs from "fs";
|
2020-04-02 04:35:28 +02:00
|
|
|
import url from "url";
|
2020-04-05 18:47:09 +02:00
|
|
|
import sql from "../sql.mjs";
|
|
|
|
import lib from "../lib.mjs";
|
|
|
|
import tpl from "../tpl.mjs";
|
2019-04-25 18:00:47 +00:00
|
|
|
|
2020-04-02 04:35:28 +02:00
|
|
|
const templates = {
|
2020-04-06 13:16:21 +02:00
|
|
|
contact: fs.readFileSync("./views/contact.html", "utf-8"),
|
|
|
|
how: fs.readFileSync("./views/how.html", "utf-8"),
|
|
|
|
index: fs.readFileSync("./views/index.html", "utf-8"),
|
|
|
|
item: fs.readFileSync("./views/item.html", "utf-8"),
|
|
|
|
snippets: {
|
|
|
|
navbar: fs.readFileSync("./views/snippets/navbar.html", "utf-8")
|
|
|
|
}
|
2020-04-02 04:35:28 +02:00
|
|
|
};
|
2019-04-25 19:25:38 +00:00
|
|
|
|
2020-04-06 13:16:21 +02:00
|
|
|
tpl.snippets = templates.snippets;
|
|
|
|
|
2020-04-02 04:35:28 +02:00
|
|
|
router.get("/", async (req, res) => {
|
|
|
|
const query = await sql.query("select id, mime from items order by id desc limit 300");
|
|
|
|
const data = {
|
|
|
|
items: query,
|
2020-04-03 13:12:35 +02:00
|
|
|
last: query[query.length - 1].id
|
2020-04-02 04:35:28 +02:00
|
|
|
};
|
|
|
|
|
2020-04-06 13:16:21 +02:00
|
|
|
res.reply({ body: tpl.render(templates.index, data) });
|
2020-04-02 04:35:28 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
router.get(/^\/([0-9]+)$/, async (req, res) => {
|
|
|
|
const q = "select * from items where id = ? limit 1; " // get item
|
|
|
|
+ "select id from items where id = (select min(id) from items where id > ?); " // get previous item
|
|
|
|
+ "select id from items where id = (select max(id) from items where id < ?)"; // get next item
|
2020-04-06 13:16:21 +02:00
|
|
|
const query = await sql.query(q, Array(3).fill(req.url.split[0]));
|
|
|
|
if(!query[0][0])
|
|
|
|
return res.redirect("/404");
|
|
|
|
|
|
|
|
const e = query[0][0];
|
2020-04-02 04:35:28 +02:00
|
|
|
const data = {
|
2020-04-06 13:16:21 +02:00
|
|
|
user: {
|
|
|
|
name: e.username,
|
|
|
|
channel: e.userchannel,
|
|
|
|
network: e.usernetwork
|
|
|
|
},
|
|
|
|
item: {
|
|
|
|
id: e.id,
|
|
|
|
src: {
|
|
|
|
long: e.src,
|
|
|
|
short: url.parse(e.src).hostname,
|
|
|
|
},
|
|
|
|
thumbnail: `${cfg.websrv.paths.thumbnails}/${e.id}.png`,
|
|
|
|
dest: `${cfg.websrv.paths.images}/${e.dest}`,
|
|
|
|
mime: e.mime,
|
|
|
|
size: lib.formatSize(e.size),
|
|
|
|
timestamp: new Date(e.stamp * 1000).toISOString()
|
|
|
|
},
|
|
|
|
next: query[1].length ? query[1][0].id : null,
|
|
|
|
prev: query[2].length ? query[2][0].id : null
|
2020-04-02 04:35:28 +02:00
|
|
|
};
|
2020-04-06 13:16:21 +02:00
|
|
|
res.reply({ body: tpl.render(templates.item, data) });
|
2020-04-02 04:35:28 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
router.get(/^\/(contact|help|how)$/, (req, res) => {
|
2020-04-06 13:16:21 +02:00
|
|
|
res.reply({ body: tpl.render(templates[req.url.split[0]]) });
|
2020-04-02 04:35:28 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
router.get("/random", async (req, res) => {
|
2020-04-06 13:16:21 +02:00
|
|
|
res.redirect("/" + (await sql.query("select id from items order by rand() limit 1"))[0].id)
|
2019-04-25 18:00:47 +00:00
|
|
|
});
|