routes and stuff

This commit is contained in:
Flummi 2021-12-06 10:21:11 +01:00
parent 43665884f6
commit f7313d864c
9 changed files with 216 additions and 151 deletions

View File

@ -39,7 +39,7 @@ const flash = ({ type, msg }) => {
(async () => {
if(_addtag = document.querySelector("a#a_addtag")) {
const postid = +document.querySelector("a.id-link").innerText;
const poster = document.querySelector("span#a_username").innerText;
const poster = document.querySelector("a#a_username").innerText;
let tags = [...document.querySelectorAll("#tags > .badge")].map(t => t.innerText.slice(0, -2));
const deleteEvent = async e => {

View File

@ -57,6 +57,13 @@ export default new class {
createID() {
return crypto.randomBytes(16).toString("hex") + Date.now().toString(24);
};
genLink(env) {
const link = [];
if(env.user) link.push("user", env.user);
if(env.mime.length > 2) link.push(env.mime);
if(env.page) link.push("p", env.page);
return link.join("/");
};
// async funcs
async countf0cks() {

View File

@ -1,10 +1,10 @@
import cfg from "../../../config.json" assert { type: "json" };
import cfg from "../config.mjs";
import sql from "../sql.mjs";
import lib from "../lib.mjs";
import fs from "fs";
import url from "url";
const allowedMimes = [ "audio", "image", "video", "%" ];
const allowedMimes = [ "audio", "image", "video" ];
const auth = async (req, res, next) => {
if(!req.session)
return res.redirect("/login");
@ -12,136 +12,177 @@ const auth = async (req, res, next) => {
};
export default (router, tpl) => {
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 tmp = tmpmime == "audio" ? lib.getMode(0) : lib.getMode(req.session.mode ?? 0);
router.get(/^\/?(?:user\/(?<user>[a-zA-Z0-9\[\]\-\_\{\}\\`\|]+))?(?:\/(?<mime>image|audio|video))?(?:\/p\/(?<page>\d+))?(?:\/(?<itemid>\d+))?$/, async (req, res) => {
const user = req.params.user ?? null;
const mime = (req.params.mime ?? "");
const smime = allowedMimes.includes(mime) ? mime + "/%" : mime === "" ? "%" : "%";
const page = +(req.params.page ?? 1);
const itemid = +(req.params.itemid ?? null);
const total = (
await sql("items")
.whereRaw(tmp)
.andWhere("items.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("items.id", "items.mime", "tags_assign.tag_id")
.joinRaw("left join tags_assign on tags_assign.item_id = items.id and (tags_assign.tag_id = 1 or tags_assign.tag_id = 2)")
.whereRaw(tmp)
.andWhere("items.mime", "like", mime)
.orderBy("items.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
const output = {
user, mime, smime, page, itemid
};
res.reply({ body: tpl.render("index", data, req) });
});
router.get(/^\/((audio\/|video\/|image\/)?[0-9]+)$/, async (req, res) => {
let id = false;
let mime = "";
let tmpmime = false;
const mode = itemid > 0 ? "item" : "index";
const modequery = mime == "audio" ? lib.getMode(0) : lib.getMode(req.session.mode ?? 0);
let data;
if(user && (await sql("items").where("username", "like", user).count("* as total"))[0].total === 0)
return res.end("200 - user not found lol");
if(mode === "item") { // item!
if(itemid == 404)
return res.end("404 - lol");
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")
.whereRaw(modequery)
.andWhere("id", itemid)
.andWhere("mime", "like", smime)
.andWhere("username", "like", user ? user : "%")
.limit(1))[0];
let tmp = tmpmime == "audio" ? lib.getMode(0) : lib.getMode(req.session.mode ?? 0);
if(!query?.id) {
return res.end("Sorry, this f0ck either doesn't exist (yet) or hasn't been tagged yet, please remain patient, a support assistant will be shortly with you.");
}
const tags = await sql("tags_assign")
.leftJoin("tags", "tags.id", "tags_assign.tag_id")
.where("tags_assign.item_id", itemid);
const qmin = await sql("items")
.select("id")
.whereRaw(modequery)
.andWhere("mime", "like", smime)
.andWhere("username", "like", user ? user : "%")
.orderBy("id")
.limit(1);
if(id == 404)
tmp = "";
const query = (await sql("items")
.whereRaw(tmp)
.andWhere("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").whereRaw(tmp).andWhere("mime", "like", mime).orderBy("id").limit(1);
const qmax = await sql("items").select("id").whereRaw(tmp).andWhere("mime", "like", mime).orderBy("id", "desc").limit(1);
const qnext = (await sql("items").select("id").whereRaw(tmp).andWhere("id", ">", id).andWhere("mime", "like", mime).orderBy("id").limit(3)).reverse();
const qprev = await sql("items").select("id").whereRaw(tmp).andWhere("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,
const qmax = await sql("items")
.select("id")
.whereRaw(modequery)
.andWhere("mime", "like", smime)
.andWhere("username", "like", user ? user : "%")
.orderBy("id", "desc")
.limit(1);
const qnext = (await sql("items")
.select("id")
.whereRaw(modequery)
.andWhere("id", ">", itemid)
.andWhere("mime", "like", smime)
.andWhere("username", "like", user ? user : "%")
.orderBy("id")
.limit(3)
).reverse();
const qprev = await sql("items")
.select("id")
.whereRaw(modequery)
.andWhere("id", "<", itemid)
.andWhere("mime", "like", smime)
.andWhere("username", "like", user ? user : "%")
.orderBy("id", "desc")
.limit(3);
const cheat = qnext.concat([{ id: itemid }].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 link = lib.genLink({ user, mime, act_page: query.id });
for(let t = 0; t < tags.length; t++)
tags[t].tag = tags[t].tag.replace(/[\u00A0-\u9999<>\&]/g, i => '&#'+i.charCodeAt(0)+';');
data = {
user: {
name: query.username,
channel: query.userchannel,
network: query.usernetwork
},
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
};
res.reply({ body: tpl.render("item", data, req) });
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: link,
uff: true
},
link: link,
tmp: output
};
}
else { // page!
const total = (await sql("items")
.whereRaw(modequery)
.andWhere("items.mime", "like", smime)
.andWhere("items.username", "like", user ? user : "%")
.count("* as total")
)[0].total;
const pages = +Math.ceil(total / cfg.websrv.eps);
const act_page = Math.min(pages, page || 1);
const offset = Math.max(0, (act_page - 1) * cfg.websrv.eps);
const rows = await sql("items")
.select("items.id", "items.mime", "tags_assign.tag_id")
.joinRaw("left join tags_assign on tags_assign.item_id = items.id and (tags_assign.tag_id = 1 or tags_assign.tag_id = 2)")
.whereRaw(modequery)
.andWhere("items.mime", "like", smime)
.andWhere("items.username", "like", user ? user : "%")
.orderBy("items.id", "desc")
.offset(offset)
.limit(cfg.websrv.eps);
if(rows.length === 0)
return res.end("oops");
rows.forEach(e => {
if(!fs.existsSync(`public/t/${e.id}.png`))
fs.copyFileSync("public/s/img/broken.png", `public/t/${e.id}.png`);
});
const cheat = [];
for(let i = Math.max(1, act_page - 3); i <= Math.min(act_page + 3, pages); i++)
cheat.push(i);
const link = lib.genLink({ user, mime, act_page });
data = {
items: rows,
pagination: {
start: 1,
end: pages,
prev: (act_page > 1) ? act_page - 1 : null,
next: (act_page < pages) ? act_page + 1 : null,
page: act_page,
cheat: cheat,
link: link,
uff: false
},
last: rows[rows.length - 1].id,
link: link,
tmp: output
};
}
res.reply({ body: tpl.render(mode, data, req) });
});
router.get(/^\/(about)$/, (req, res) => {

View File

@ -1,15 +1,33 @@
import sql from "../sql.mjs";
import lib from "../lib.mjs";
const allowedMimes = [ "audio", "image", "video", "%" ];
const allowedMimes = [ "audio", "image", "video" ];
export default (router, tpl) => {
router.get(/^\/random(\/image|\/video|\/audio)?$/, async (req, res) => {
const tmp = lib.getMode(req.session.mode ?? 0);
const mime = (allowedMimes.filter(n => req.url.split[1]?.startsWith(n))[0] ? req.url.split[1] : "") + "%";
const rows = await sql("items").select("id").whereRaw(tmp).andWhere("mime", "like", mime).orderByRaw("rand()").limit(1);
router.get(/^\/random\/?(?:user\/(?<user>[a-zA-Z0-9\[\]\-\_\{\}\\`\|]+))?(?:\/(?<mime>image|audio|video))?$/, async (req, res) => {
const user = req.params.user ?? null;
const mime = (req.params.mime ?? "");
const smime = allowedMimes.includes(mime) ? mime + "/%" : mime === "" ? "%" : "%";
const modequery = mime == "audio" ? lib.getMode(0) : lib.getMode(req.session.mode ?? 0);
if(user && (await sql("items").where("username", "like", user).count("* as total"))[0].total === 0)
return res.end("200 - user not found lol");
const rows = await sql("items")
.select("id")
.whereRaw(modequery)
.andWhere("mime", "like", smime)
.andWhere("username", "like", user ? user : "%")
.orderByRaw("rand()")
.limit(1);
if(rows.length === 0)
return res.end("nope");
const link = lib.genLink({ user, mime });
res.redirect(`/${req.url.split[1] ? req.url.split[1] + "/" : ""}${rows[0].id}`);
res.redirect(`/${link}${link.length != 0 ? "/": ""}${rows[0].id}`);
});
return router;
};

View File

@ -2,7 +2,7 @@
<div class="index-container">
<div id="posts">
@each(items as item)
<a href="/@if(typeof filter !== "undefined"){{ filter }}/@endif{{ item.id }}" data-mime="{{ item.mime }}" data-mode="{{ item.tag_id ? ['','sfw','nsfw'][item.tag_id] : 'null' }}" style="background-image: url('/t/{{ item.id }}.png')"><p></p></a>
<a href="/{{ link }}@if(link.length != 0)/@endif{{ item.id }}" data-mime="{{ item.mime }}" data-mode="{{ item.tag_id ? ['','sfw','nsfw'][item.tag_id] : 'null' }}" style="background-image: url('/t/{{ item.id }}.png')"><p></p></a>
@endeach
</div>
</div>

View File

@ -4,7 +4,7 @@
<div class="next-post">
@if(pagination.prev)
<div class="arrow-next">
<a id="next" href="{{ pagination.link }}{{ pagination.prev }}"></a>
<a id="next" href="/{{ link }}@if(link.length != 0)/@endif{{ pagination.prev }}"></a>
</div>
@else
<div class="arrow-next">
@ -20,7 +20,7 @@
preload="auto" autoplay controls loop playsinline></video>
</div>
@elseif(item.mime.startsWith("audio"))
<div class="embed-responsive embed-responsive-16by9" style="background: url('@if(item.coverart)//f0ck.me{{ item.coverart }}@else/s/img/200.gif@endif') no-repeat center / contain black;"></div>
<div class="embed-responsive embed-responsive-16by9" style="background: url('@if(item.coverart)//f0ck.me{{ item.coverart }}@else/s/img/200.gif@endif') no-repeat center / contain black;">
<audio id="my-video" class="embed-responsive-item" autoplay controls loop src="{{ item.dest }}" data-setup="{}" poster="@if(item.coverart){{ item.coverart }}@else/s/img/200.gif@endif" type="{{ item.mime }}"></audio>
</div>
@elseif(item.mime.startsWith("image"))
@ -36,7 +36,7 @@
<div class="previous-post">
@if(pagination.next)
<div class="arrow-prev">
<a id="prev" href="{{ pagination.link }}{{ pagination.next }}"></a>
<a id="prev" href="/{{ link }}@if(link.length != 0)/@endif{{ pagination.next }}"></a>
</div>
@else
<div class="arrow-prev">
@ -47,7 +47,7 @@
</div>
<div class="metadata">
<span class="badge badge-dark">
<a href="/{{ item.id }}" style="--hover-image: url('/t/{{ item.id }}.png');" class="id-link">{{ item.id }}</a>@if(session) (<span id="a_username">{{ user.name }}</span>)@endif
<a href="/{{ item.id }}" style="--hover-image: url('/t/{{ item.id }}.png');" class="id-link">{{ item.id }}</a>@if(session) (<a id="a_username" href="/user/{{ user.name.toLowerCase() }}@if(tmp.mime)/{{ tmp.mime }}@endif">{{ user.name }}</a>)@endif
</span>
<span class="badge badge-dark">{{ user.network }} / {{ user.channel }}</span>
<span class="badge badge-dark image-source"><a class="post_source" title="{{ item.src.long }}" href="{{ item.src.long }}" target="_blank">{{ item.src.short }}</a></span>

View File

@ -1,15 +1,15 @@
@if(typeof pagination !== "undefined")
@if(pagination)
<nav class="pagination">
<a href="{{ pagination.link }}{{ pagination.start }}" class="page-item-1 btn start@if(!pagination.prev) disabled@endif">&laquo;</a>
<a href="{{ pagination.link }}{{ pagination.prev }}" class="page-item-2 btn prev@if(!pagination.prev) disabled@endif">&lsaquo;</a>
<a href="/{{ pagination.link }}@if(pagination.link.length != 0)/@endif@if(!pagination.uff)p/@endif{{ pagination.start }}" class="page-item-1 btn start@if(!pagination.prev) disabled@endif">&laquo;</a>
<a href="/{{ pagination.link }}@if(pagination.link.length != 0)/@endif@if(!pagination.uff)p/@endif{{ pagination.prev }}" class="page-item-2 btn prev@if(!pagination.prev) disabled@endif">&lsaquo;</a>
@each(pagination.cheat as i)
@if(i == pagination.page)
<span class="btn disabled">{{ i }}</span>
@else
<a href="{{ pagination.link }}{{ i }}" class="pagination-int-item btn">{{ i }}</a>
<a href="/{{ pagination.link }}@if(pagination.link.length != 0)/@endif@if(!pagination.uff)p/@endif{{ i }}" class="pagination-int-item btn">{{ i }}</a>
@endif
@endeach
<a href="{{ pagination.link }}{{ pagination.next }}" class="page-item-3 btn next@if(!pagination.next) disabled@endif">&rsaquo;</a>
<a href="{{ pagination.link }}{{ pagination.end }}" class="page-item-4 btn start@if(!pagination.next) disabled@endif">&raquo;</a>
<a href="/{{ pagination.link }}@if(pagination.link.length != 0)/@endif@if(!pagination.uff)p/@endif{{ pagination.next }}" class="page-item-3 btn next@if(!pagination.next) disabled@endif">&rsaquo;</a>
<a href="/{{ pagination.link }}@if(pagination.link.length != 0)/@endif@if(!pagination.uff)p/@endif{{ pagination.end }}" class="page-item-4 btn start@if(!pagination.next) disabled@endif">&raquo;</a>
</nav>
@endif

View File

@ -29,12 +29,12 @@
</li>
<span class="placeholder">&nbsp;</span>
<li class="nav-item dropdown">
<a class="nav-link ddcontent" href="#"@if(typeof filter !== "undefined") content="{{ filter }}" data-toggle="dropdown"@endif>Filter@if(typeof filter === "undefined")&nbsp;&#9660;@endif</a>
<a class="nav-link ddcontent" href="#"@if(tmp.mime) content="{{ tmp.mime }}" data-toggle="dropdown"@endif>Filter@if(!tmp.mime)&nbsp;&#9660;@endif</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="/">All</a></li>
<li><a class="dropdown-item" href="/audio">Audio</a></li>
<li><a class="dropdown-item" href="/video">Video</a></li>
<li><a class="dropdown-item" href="/image">Image</a></li>
<li><a class="dropdown-item" href="/@if(tmp.user)user/{{ tmp.user }}/@endif">All</a></li>
<li><a class="dropdown-item" href="/@if(tmp.user)user/{{ tmp.user }}/@endifaudio">Audio</a></li>
<li><a class="dropdown-item" href="/@if(tmp.user)user/{{ tmp.user }}/@endifvideo">Video</a></li>
<li><a class="dropdown-item" href="/@if(tmp.user)user/{{ tmp.user }}/@endifimage">Image</a></li>
</ul>
</li>
@if(session)
@ -48,7 +48,7 @@
</li>
@endif
<li class="nav-item">
<a id="random" class="nav-link" href="/random@if(typeof filter !== "undefined")/{{ filter }}@endif">
<a id="random" class="nav-link" href="/random@if(tmp.user)/user/{{ tmp.user }}@endif@if(tmp.mime)/{{ tmp.mime }}@endif">
<span class="nav-link-identifier">Random</span>
</a>
</li>

View File

@ -36,7 +36,6 @@
<div class="pagination-container-fluid">
<div class="pagination-wrapper">
Henlo, {{ session.user }}&nbsp;&nbsp;<a href="/logout">Logout</a>
@include(partials/pagination)
</div>
</div>
</div>