f0ckv2/src/inc/routes/index.mjs

143 lines
5.1 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";
2021-04-17 10:43:23 +02:00
import { mimes } 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-05-15 13:58:24 +02:00
//router.get(/^\/(audio\/?|image\/?|video\/?|user\/?)?(p\/\d+)?$/, async (req, res) => {
2021-04-17 10:43:23 +02:00
router.get(/^\/(audio\/?|image\/?|video\/?)?(p\/\d+)?$/, async (req, res) => {
2021-03-03 05:34:57 +01:00
try {
2021-04-17 10:43:23 +02:00
let mime = false;
let q = false;
2021-05-15 13:58:24 +02:00
//if(req.url.split[0] == "user")
2021-04-17 10:43:23 +02:00
if(['audio', 'image', 'video'].includes(req.url.split[0])) {
mime = req.url.split[0];
q = " where " + (mimes[mime] ? mimes[mime].map(_mime => `mime = "${_mime}"`).join(" or ") : null);
}
const total = (await sql.query("select count(*) as total from items" + (mime ? q : "")))[0].total;
const pages = +Math.ceil(total / cfg.websrv.eps);
const page = Math.min(pages, +req.url.split[mime ? 2 : 1] || 1);
const offset = (page - 1) * cfg.websrv.eps;
2021-03-03 05:34:57 +01:00
2021-04-17 10:43:23 +02:00
const query = await sql.query(`select id, mime from items ${mime ? q : ""} order by id desc limit ?, ?`, [ offset, cfg.websrv.eps ]);
2021-03-03 05:34:57 +01:00
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,
2021-04-17 10:43:23 +02:00
link: `/${mime ? mime + "/" : ""}p/`
2021-03-03 05:34:57 +01:00
},
2021-04-17 10:43:23 +02:00
last: query[query.length - 1].id,
2021-04-17 19:55:25 +02:00
filter: mime ? mime : undefined,
2021-04-22 04:12:40 +02:00
themes: cfg.websrv.themes,
2021-04-17 19:55:25 +02:00
theme: (typeof req.cookies.theme !== "undefined" && cfg.websrv.themes.includes(req.cookies.theme)) ? req.cookies.theme : cfg.websrv.themes[0]
2021-03-03 05:34:57 +01:00
};
res.reply({ body: tpl.render("views/index", data) });
} catch(err) {
2021-04-17 10:43:23 +02:00
console.log(err);
res.reply({ body: "sorry bald wieder da lol :(" });
2021-03-03 05:34:57 +01:00
}
2020-04-02 04:35:28 +02:00
});
2021-04-17 10:43:23 +02:00
router.get(/^\/((audio\/|video\/|image\/)?[0-9]+)$/, async (req, res) => {
let id = false;
let mime = false;
let q = false;
if(['audio', 'video', 'image'].includes(req.url.split[0])) {
mime = req.url.split[0];
id = +req.url.split[1];
q = mimes[mime] ? mimes[mime].map(_mime => `mime = "${_mime}"`).join(" or ") : null;
}
else
id = +req.url.split[0];
const query = (await sql.query(`select items.* from items where items.id = ? ${mime ? "and ("+q+")" : ""} limit 1`, [ id ]))?.shift();
2021-01-25 20:30:39 +01:00
if(!query?.id)
2020-04-06 13:16:21 +02:00
return res.redirect("/404");
2021-04-17 10:43:23 +02:00
const tags = (await sql.query(`select tags.tag from tags_assign left join tags on tags.id = tags_assign.tag_id where tags_assign.item_id = ?`, [ id ]));
const qmin = (await sql.query(`select id from items ${mime ? "where "+q : ""} order by id asc limit 1`))[0].id;
const qmax = (await sql.query(`select id from items ${mime ? "where "+q : ""} order by id desc limit 1`))[0].id;
const qnext = (await sql.query(`select id from items where id > ? ${mime ? "and ("+q+")" : ""} order by id asc limit 3`, [ id ])).reverse();
const qprev = (await sql.query(`select id from items where id < ? ${mime ? "and ("+q+")" : ""} order by id desc limit 3`, [ id ]));
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;
2021-05-15 13:58:24 +02:00
for(let t = 0; t < tags.length; t++)
tags[t].tag = tags[t].tag.replace(/[\u00A0-\u9999<>\&]/g, i => '&#'+i.charCodeAt(0)+';');
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`,
2021-04-17 10:43:23 +02:00
coverart: `${cfg.websrv.paths.coverarts}/${query.id}.png`,
2020-04-07 10:07:47 +02:00
dest: `${cfg.websrv.paths.images}/${query.dest}`,
mime: query.mime,
size: lib.formatSize(query.size),
2021-04-22 04:12:40 +02:00
timestamp: lib.timeAgo(new Date(query.stamp * 1e3).toISOString()),
2021-04-17 10:43:23 +02:00
tags: tags
2020-04-06 13:16:21 +02:00
},
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-04-17 10:43:23 +02:00
end: qmin,
prev: next,
next: prev,
2021-03-03 05:34:57 +01:00
page: query.id,
cheat: cheat,
2021-04-17 10:43:23 +02:00
link: `/${mime ? mime + "/" : ""}`
},
2021-04-17 19:55:25 +02:00
filter: mime ? mime : undefined,
2021-04-22 04:12:40 +02:00
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)]
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-04-22 04:12:40 +02:00
router.get(/^\/(about)$/, (req, res) => {
2021-04-17 19:55:25 +02:00
res.reply({
body: tpl.render(`views/${req.url.split[0]}`, {
2021-04-22 04:12:40 +02:00
themes: cfg.websrv.themes,
2021-04-17 19:55:25 +02:00
theme: (typeof req.cookies.theme !== "undefined" && cfg.websrv.themes.includes(req.cookies.theme)) ? req.cookies.theme : cfg.websrv.themes[0]
})
});
2020-04-02 04:35:28 +02:00
});