aktueller Stand
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
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 sql from "../sql.mjs";
|
||||
import lib from "../lib.mjs";
|
||||
@ -8,22 +9,55 @@ import tpl from "../tpl.mjs";
|
||||
|
||||
tpl.readdir("views");
|
||||
|
||||
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,
|
||||
last: query[query.length - 1].id
|
||||
};
|
||||
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;
|
||||
|
||||
res.reply({ body: tpl.render("views/index", data) });
|
||||
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);
|
||||
|
||||
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: {
|
||||
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 :(" });
|
||||
}
|
||||
});
|
||||
|
||||
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;
|
||||
|
||||
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 data = {
|
||||
user: {
|
||||
name: query.username,
|
||||
@ -44,7 +78,15 @@ router.get(/^\/([0-9]+)$/, async (req, res) => {
|
||||
},
|
||||
next: query.next ? query.next : null,
|
||||
prev: query.prev ? query.prev : null,
|
||||
title: `${query.id} - f0ck.me`
|
||||
title: `${query.id} - f0ck.me`,
|
||||
pagination: {
|
||||
end: 1,
|
||||
prev: query.id + 1,
|
||||
next: Math.max(query.id - 1, 1),
|
||||
page: query.id,
|
||||
cheat: cheat,
|
||||
link: "/"
|
||||
}
|
||||
};
|
||||
res.reply({ body: tpl.render("views/item", data) });
|
||||
});
|
||||
|
Reference in New Issue
Block a user