Files
f0bm/src/inc/routes/index.mjs
2021-05-16 13:24:31 +02:00

139 lines
4.7 KiB
JavaScript

import router from "../router.mjs";
import cfg from "../../../config.json";
import url from "url";
import fs from "fs";
import sql from "../sql.mjs";
import lib from "../lib.mjs";
import tpl from "../tpl.mjs";
tpl.readdir("views");
const allowedMimes = [ "audio", "image", "video", "%" ];
router.get(/^\/(audio\/?|image\/?|video\/?)?(p\/\d+)?$/, async (req, res) => {
const tmpmime = (allowedMimes.filter(n => req.url.split[0].startsWith(n))[0] ? req.url.split[0] : "");
const mime = tmpmime + "%";
const total = (await sql("items").where("mime", "like", mime).count("* as total"))[0].total;
const pages = +Math.ceil(total / cfg.websrv.eps);
const page = Math.min(pages, +req.url.split[tmpmime.length > 0 ? 2 : 1] || 1);
const offset = (page - 1) * cfg.websrv.eps;
const rows = await sql("items")
.select("id", "mime")
.where("mime", "like", mime)
.orderBy("id", "desc")
.offset(offset)
.limit(cfg.websrv.eps);
let cheat = [];
for(let i = Math.max(1, page - 3); i <= Math.min(page + 3, pages); i++)
cheat.push(i);
rows.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: rows,
pagination: {
start: 1,
end: pages,
prev: (page > 1) ? page - 1 : null,
next: (page < pages) ? page + 1 : null,
page: page,
cheat: cheat,
link: `/${tmpmime ? tmpmime + "/" : ""}p/`
},
last: rows[rows.length - 1].id,
filter: tmpmime ? tmpmime : undefined,
themes: cfg.websrv.themes,
theme: (typeof req.cookies.theme !== "undefined" && cfg.websrv.themes.includes(req.cookies.theme)) ? req.cookies.theme : cfg.websrv.themes[0]
};
res.reply({ body: tpl.render("views/index", data) });
});
router.get(/^\/((audio\/|video\/|image\/)?[0-9]+)$/, async (req, res) => {
let id = false;
let mime = "";
let tmpmime = false;
if(allowedMimes.filter(n => req.url.split[0].startsWith(n))[0] ? req.url.split[0] : "") {
mime = tmpmime = req.url.split[0];
id = +req.url.split[1];
}
else {
mime = "%";
id = +req.url.split[0];
}
mime += "/%";
const query = (await sql("items").where("id", id).andWhere("mime", "like", mime).limit(1))?.shift();
if(!query?.id)
return res.redirect("/404");
const tags = await sql("tags_assign").leftJoin("tags", "tags.id", "tags_assign.tag_id").where("tags_assign.item_id", id);
const qmin = await sql("items").select("id").where("mime", "like", mime).orderBy("id").limit(1);
const qmax = await sql("items").select("id").where("mime", "like", mime).orderBy("id", "desc").limit(1);
const qnext = (await sql("items").select("id").where("id", ">", id).andWhere("mime", "like", mime).orderBy("id").limit(3)).reverse();
const qprev = await sql("items").select("id").where("id", "<", id).andWhere("mime", "like", mime).orderBy("id", "desc").limit(3);
const cheat = qnext.concat([{ id: id }].concat(qprev)).map(e => +e.id);
const next = qnext[qnext.length - 1] ? qnext[qnext.length - 1].id : false;
const prev = qprev[0] ? qprev[0].id : false;
for(let t = 0; t < tags.length; t++)
tags[t].tag = tags[t].tag.replace(/[\u00A0-\u9999<>\&]/g, i => '&#'+i.charCodeAt(0)+';');
const data = {
user: {
name: query.username,
channel: query.userchannel,
network: query.usernetwork
},
item: {
id: query.id,
src: {
long: query.src,
short: url.parse(query.src).hostname,
},
thumbnail: `${cfg.websrv.paths.thumbnails}/${query.id}.png`,
coverart: `${cfg.websrv.paths.coverarts}/${query.id}.png`,
dest: `${cfg.websrv.paths.images}/${query.dest}`,
mime: query.mime,
size: lib.formatSize(query.size),
timestamp: lib.timeAgo(new Date(query.stamp * 1e3).toISOString()),
tags: tags
},
title: `${query.id} - f0ck.me`,
pagination: {
start: qmax[0].id,
end: qmin[0].id,
prev: next,
next: prev,
page: query.id,
cheat: cheat,
link: `/${tmpmime ? tmpmime + "/" : ""}`
},
filter: tmpmime ? tmpmime : undefined,
themes: cfg.websrv.themes,
theme: (typeof req.cookies.theme !== "undefined" && cfg.websrv.themes.includes(req.cookies.theme)) ? req.cookies.theme : cfg.websrv.themes[0],
lul: cfg.websrv.phrases[~~(Math.random() * cfg.websrv.phrases.length)]
};
res.reply({ body: tpl.render("views/item", data) });
});
router.get(/^\/(about)$/, (req, res) => {
res.reply({
body: tpl.render(`views/${req.url.split[0]}`, {
themes: cfg.websrv.themes,
theme: (typeof req.cookies.theme !== "undefined" && cfg.websrv.themes.includes(req.cookies.theme)) ? req.cookies.theme : cfg.websrv.themes[0]
})
});
});