This commit is contained in:
Flummi
2021-12-04 11:08:55 +01:00
parent 5b7dd4293c
commit d885dd8e4e
7 changed files with 123 additions and 19 deletions

View File

@ -13,10 +13,12 @@ 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 tmp = req.cookies.session ? "" : "id in (select item_id from tags_assign where tag_id = 1 group by item_id)";
const total = (
await sql("items")
.where("mime", "like", mime)
.whereRaw(tmp)
.andWhere("items.mime", "like", mime)
.count("* as total")
)[0].total;
@ -25,9 +27,10 @@ router.get(/^\/(audio\/?|image\/?|video\/?)?(p\/\d+)?$/, async (req, res) => {
const offset = (page - 1) * cfg.websrv.eps;
const rows = await sql("items")
.select("id", "mime")
.where("mime", "like", mime)
.orderBy("id", "desc")
.select("items.id", "items.mime")
.whereRaw(tmp)
.andWhere("items.mime", "like", mime)
.orderBy("items.id", "desc")
.offset(offset)
.limit(cfg.websrv.eps);
@ -62,6 +65,7 @@ router.get(/^\/((audio\/|video\/|image\/)?[0-9]+)$/, async (req, res) => {
let id = false;
let mime = "";
let tmpmime = false;
const tmp = req.cookies.session ? "" : "id in (select item_id from tags_assign where tag_id = 1 group by item_id)";
if(allowedMimes.filter(n => req.url.split[0].startsWith(n))[0] ? req.url.split[0] : "") {
mime = tmpmime = req.url.split[0];
@ -73,18 +77,18 @@ router.get(/^\/((audio\/|video\/|image\/)?[0-9]+)$/, async (req, res) => {
}
mime += "/%";
const query = (await sql("items").where("id", id).andWhere("mime", "like", mime).limit(1))?.shift();
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").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 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").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 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;

View File

@ -5,7 +5,8 @@ const allowedMimes = [ "audio", "image", "video", "%" ];
router.get(/^\/random(\/image|\/video|\/audio)?$/, async (req, res) => {
const mime = (allowedMimes.filter(n => req.url.split[1]?.startsWith(n))[0] ? req.url.split[1] : "") + "%";
const rows = await sql("items").select("id").where("mime", "like", mime).orderByRaw("rand()").limit(1);
const tmp = req.cookies.session ? "" : "id in (select item_id from tags_assign where tag_id = 1 group by item_id)";
const rows = await sql("items").select("id").whereRaw(tmp).andWhere("mime", "like", mime).orderByRaw("rand()").limit(1);
res.redirect(`/${req.url.split[1] ? req.url.split[1] + "/" : ""}${rows[0].id}`);
});