f0ckv2/src/inc/routes/index.mjs

58 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-04-02 04:35:28 +02:00
import router from "../router.mjs";
import cfg from "../../../config.json";
import url from "url";
2020-04-07 10:07:47 +02:00
import { queries } from "./inc/index.mjs";
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-06 22:15:26 +02:00
tpl.readdir("views");
2020-04-06 13:16:21 +02:00
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 22:15:26 +02:00
res.reply({ body: tpl.render("views/index", data) });
2020-04-02 04:35:28 +02:00
});
router.get(/^\/([0-9]+)$/, async (req, res) => {
2020-04-07 10:07:47 +02:00
const query = (await sql.query(queries.item, Array(3).fill(req.url.split[0])))?.shift();
if(query.length === 0)
2020-04-06 13:16:21 +02:00
return res.redirect("/404");
2020-04-02 04:35:28 +02:00
const data = {
2020-04-06 13:16:21 +02:00
user: {
2020-04-07 10:07:47 +02:00
name: query.username,
channel: query.userchannel,
network: query.usernetwork
2020-04-06 13:16:21 +02:00
},
item: {
2020-04-07 10:07:47 +02:00
id: query.id,
2020-04-06 13:16:21 +02:00
src: {
2020-04-07 10:07:47 +02:00
long: query.src,
short: url.parse(query.src).hostname,
2020-04-06 13:16:21 +02:00
},
2020-04-07 10:07:47 +02:00
thumbnail: `${cfg.websrv.paths.thumbnails}/${query.id}.png`,
dest: `${cfg.websrv.paths.images}/${query.dest}`,
mime: query.mime,
size: lib.formatSize(query.size),
timestamp: new Date(query.stamp * 1000).toISOString()
2020-04-06 13:16:21 +02:00
},
2020-04-07 10:07:47 +02:00
next: query.next ? query.next : null,
prev: query.prev ? query.prev : null
2020-04-02 04:35:28 +02:00
};
2020-04-06 22:15:26 +02:00
res.reply({ body: tpl.render("views/item", data) });
2020-04-02 04:35:28 +02:00
});
router.get(/^\/(contact|help|how)$/, (req, res) => {
2020-04-06 22:15:26 +02:00
res.reply({ body: tpl.render(`views/${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
});