v2.1 lol
This commit is contained in:
@ -2,22 +2,28 @@ import router from "../router.mjs";
|
||||
import cfg from "../../../config.json";
|
||||
import url from "url";
|
||||
import fs from "fs";
|
||||
import { queries } from "./inc/index.mjs";
|
||||
import { mimes } from "./inc/index.mjs";
|
||||
import sql from "../sql.mjs";
|
||||
import lib from "../lib.mjs";
|
||||
import tpl from "../tpl.mjs";
|
||||
|
||||
tpl.readdir("views");
|
||||
|
||||
router.get(/\/(p\/\d+)?$/, async (req, res) => {
|
||||
router.get(/^\/(audio\/?|image\/?|video\/?)?(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;
|
||||
let mime = false;
|
||||
let q = false;
|
||||
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 query = await sql.query("select id, mime from items order by id desc limit ?, ?", [ offset, limit ]);
|
||||
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;
|
||||
|
||||
const query = await sql.query(`select id, mime from items ${mime ? q : ""} order by id desc limit ?, ?`, [ offset, cfg.websrv.eps ]);
|
||||
|
||||
let cheat = [];
|
||||
for(let i = Math.max(1, page - 3); i <= Math.min(page + 3, pages); i++)
|
||||
@ -37,28 +43,50 @@ router.get(/\/(p\/\d+)?$/, async (req, res) => {
|
||||
next: (page < pages) ? page + 1 : null,
|
||||
page: page,
|
||||
cheat: cheat,
|
||||
link: "/p/"
|
||||
link: `/${mime ? mime + "/" : ""}p/`
|
||||
},
|
||||
last: query[query.length - 1].id
|
||||
last: query[query.length - 1].id,
|
||||
filter: mime ? mime : undefined
|
||||
};
|
||||
|
||||
res.reply({ body: tpl.render("views/index", data) });
|
||||
} catch(err) {
|
||||
res.reply({ body: "error :(" });
|
||||
console.log(err);
|
||||
res.reply({ body: "sorry bald wieder da lol :(" });
|
||||
}
|
||||
});
|
||||
|
||||
router.get(/^\/([0-9]+)$/, async (req, res) => {
|
||||
const query = (await sql.query(queries.item, Array(3).fill(req.url.split[0])))?.shift();
|
||||
const qmax = (await sql.query("select id from items order by id desc limit 1"))[0].id;
|
||||
|
||||
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();
|
||||
|
||||
if(!query?.id)
|
||||
return res.redirect("/404");
|
||||
|
||||
let cheat = [];
|
||||
for(let i = Math.min(query.id + 3, qmax); i >= Math.max(1, query.id - 3); i--)
|
||||
cheat.push(i);
|
||||
|
||||
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;
|
||||
|
||||
const data = {
|
||||
user: {
|
||||
name: query.username,
|
||||
@ -72,23 +100,24 @@ router.get(/^\/([0-9]+)$/, async (req, res) => {
|
||||
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: new Date(query.stamp * 1000).toISOString()
|
||||
timestamp: lib.timeAgo(new Date(query.stamp * 1000).toISOString()),
|
||||
tags: tags
|
||||
},
|
||||
next: query.next ? query.next : null,
|
||||
prev: query.prev ? query.prev : null,
|
||||
title: `${query.id} - f0ck.me`,
|
||||
pagination: {
|
||||
start: qmax,
|
||||
end: 1,
|
||||
prev: query.id + 1,
|
||||
next: Math.max(query.id - 1, 1),
|
||||
end: qmin,
|
||||
prev: next,
|
||||
next: prev,
|
||||
page: query.id,
|
||||
cheat: cheat,
|
||||
link: "/"
|
||||
}
|
||||
link: `/${mime ? mime + "/" : ""}`
|
||||
},
|
||||
filter: mime ? mime : undefined
|
||||
};
|
||||
res.reply({ body: tpl.render("views/item", data) });
|
||||
});
|
||||
@ -96,7 +125,3 @@ router.get(/^\/([0-9]+)$/, async (req, res) => {
|
||||
router.get(/^\/(contact|help|about)$/, (req, res) => {
|
||||
res.reply({ body: tpl.render(`views/${req.url.split[0]}`) });
|
||||
});
|
||||
|
||||
router.get("/random", async (req, res) => {
|
||||
res.redirect("/" + (await sql.query("select id from items order by rand() limit 1"))[0].id)
|
||||
});
|
||||
|
Reference in New Issue
Block a user