f0ckv2/src/inc/routes/index.mjs

103 lines
2.9 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";
2021-03-03 05:34:57 +01:00
import fs from "fs";
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
2021-03-03 05:34:57 +01:00
router.get(/\/(p\/\d+)?$/, async (req, res) => {
try {
const total = (await sql.query("select count(*) as total from items"))[0].total;
const limit = 299;
const pages = +Math.ceil(total / limit);
const page = Math.min(pages, +req.url.split[1] || 1);
const offset = (page - 1) * limit;
const query = await sql.query("select id, mime from items order by id desc limit ?, ?", [ offset, limit ]);
let cheat = [];
for(let i = Math.max(1, page - 3); i <= Math.min(page + 3, pages); i++)
cheat.push(i);
2020-04-02 04:35:28 +02:00
2021-03-03 05:34:57 +01:00
query.forEach(e => {
if(!fs.existsSync(`public/t/${e.id}.png`))
fs.copyFileSync("public/s/img/broken.png", `public/t/${e.id}.png`);
});
const data = {
items: query,
pagination: {
2021-03-03 07:56:46 +01:00
start: 1,
2021-03-03 05:34:57 +01:00
end: pages,
prev: (page > 1) ? page - 1 : null,
next: (page < pages) ? page + 1 : null,
page: page,
cheat: cheat,
link: "/p/"
},
last: query[query.length - 1].id
};
res.reply({ body: tpl.render("views/index", data) });
} catch(err) {
res.reply({ body: "error :(" });
}
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();
2021-03-03 05:34:57 +01:00
const qmax = (await sql.query("select id from items order by id desc limit 1"))[0].id;
2020-04-07 10:07:47 +02:00
2021-01-25 20:30:39 +01:00
if(!query?.id)
2020-04-06 13:16:21 +02:00
return res.redirect("/404");
2021-03-03 05:34:57 +01:00
let cheat = [];
for(let i = Math.min(query.id + 3, qmax); i >= Math.max(1, query.id - 3); i--)
cheat.push(i);
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,
2020-04-08 02:19:20 +02:00
prev: query.prev ? query.prev : null,
2021-03-03 05:34:57 +01:00
title: `${query.id} - f0ck.me`,
pagination: {
2021-03-03 07:56:46 +01:00
start: qmax,
2021-03-03 05:34:57 +01:00
end: 1,
prev: query.id + 1,
next: Math.max(query.id - 1, 1),
page: query.id,
cheat: cheat,
link: "/"
}
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
});
2021-03-01 22:46:06 +01:00
router.get(/^\/(contact|help|about)$/, (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
});