querybuilder intensifies
This commit is contained in:
parent
3bc0a74932
commit
04d4787232
|
@ -1,80 +1,32 @@
|
|||
import router from "../router.mjs";
|
||||
import sql from "../sql.mjs";
|
||||
import { parse } from "url";
|
||||
import cfg from "../../../config.json";
|
||||
|
||||
import { mimes, queries } from "./inc/apiv1.mjs";
|
||||
const allowedMimes = [ "audio", "image", "video", "%" ];
|
||||
|
||||
router.get("/api/v1", (req, res) => {
|
||||
res.end("api lol");
|
||||
});
|
||||
|
||||
router.get(/^\/api\/v1\/random(\/user\/.+|\/image|\/video|\/audio)?$/, async (req, res) => {
|
||||
const args = [];
|
||||
let q = queries.random.main;
|
||||
const user = req.url.split[3] === "user" ? req.url.split[4] : "%";
|
||||
const mime = (allowedMimes.filter(n => req.url.split[3]?.startsWith(n))[0] ? req.url.split[3] : "") + "%";
|
||||
|
||||
if(req.url.split[3] === "user") {
|
||||
q += queries.random.where("username like ?");
|
||||
args.push(req.url.split[4] || "flummi");
|
||||
}
|
||||
else
|
||||
q += queries.random.where(mimes[req.url.split[3]] ? mimes[req.url.split[3]].map(mime => `mime = "${mime}"`).join(" or ") : null);
|
||||
const rows = await sql("items").orderByRaw("rand()").limit(1).where("mime", "like", mime).andWhere("username", "like", user);
|
||||
|
||||
try {
|
||||
const rows = await sql.query(q, args);
|
||||
res
|
||||
.writeHead(200, { "Content-Type": "application/json" })
|
||||
.end(JSON.stringify(rows.length > 0 ? rows[0] : []), "utf-8");
|
||||
} catch(err) {
|
||||
res
|
||||
.writeHead(500)
|
||||
.end(JSON.stringify(err), "utf-8");
|
||||
}
|
||||
});
|
||||
|
||||
router.get("/api/v1/p", async (req, res) => {
|
||||
let id = parseInt(req.url.qs.id) || 99999999;
|
||||
const eps = Math.min(parseInt(req.url.qs.eps) || 100, 200);
|
||||
let [ order, trend ] = req.url.qs.order === "asc" ? [ "asc", ">" ] : [ "desc", "<" ];
|
||||
|
||||
try {
|
||||
const tmp = (await sql.query("select min(id) as min, max(id) as max from items limit 1"))[0];
|
||||
if((id - 1 + eps) > tmp.max) {
|
||||
id = tmp.max;
|
||||
[ order, trend ] = [ "desc", "<=" ];
|
||||
}
|
||||
if((id + 1 - eps) < tmp.min) {
|
||||
id = tmp.min;
|
||||
[ order, trend ] = [ "asc", ">=" ];
|
||||
}
|
||||
|
||||
const rows = await sql.query(queries.p(trend, order), [ id, eps ]);
|
||||
const items = {
|
||||
items: rows,
|
||||
first: rows[0].id,
|
||||
last: rows[rows.length - 1].id,
|
||||
newest: tmp.max,
|
||||
oldest: tmp.min
|
||||
};
|
||||
res
|
||||
.writeHead(200, { "Content-Type": "application/json" })
|
||||
.end(JSON.stringify(items), "utf-8");
|
||||
} catch(err) {
|
||||
console.error(err);
|
||||
res
|
||||
.writeHead(500)
|
||||
.end(JSON.stringify(err), "utf-8");
|
||||
}
|
||||
});
|
||||
|
||||
router.get(/^\/api\/v1\/p\/([0-9]+)/, async (req, res) => { // legacy
|
||||
let eps = 100;
|
||||
let id = +req.url.split[3];
|
||||
|
||||
const query = await sql.query("select * from items where id < ? order by id desc limit ?", [ id, eps ]);
|
||||
const rows = await sql("items").where("id", "<", id).orderBy("id", "desc").limit(eps);
|
||||
|
||||
const items = {
|
||||
items: query,
|
||||
last: query[query.length - 1].id
|
||||
items: rows,
|
||||
last: rows[rows.length - 1].id
|
||||
};
|
||||
|
||||
res.writeHead(200, { "Content-Type": "application/json" });
|
||||
|
@ -82,33 +34,40 @@ router.get(/^\/api\/v1\/p\/([0-9]+)/, async (req, res) => { // legacy
|
|||
});
|
||||
|
||||
router.get(/^\/api\/v1\/item\/[0-9]+$/, async (req, res) => {
|
||||
try {
|
||||
const rows = await sql.query(queries.item, Array(3).fill(req.url.split[3]));
|
||||
const id = +req.url.split[3];
|
||||
|
||||
const item = await sql("items").where("id", id).limit(1);
|
||||
const next = await sql("items").select("id").where("id", ">", id).orderBy("id").limit(1);
|
||||
const prev = await sql("items").select("id").where("id", "<", id).orderBy("id", "desc").limit(1);
|
||||
|
||||
if(item.length === 0)
|
||||
return "nope";
|
||||
|
||||
const rows = {
|
||||
...item[0],
|
||||
...{
|
||||
next: next[0]?.id ?? null,
|
||||
prev: prev[0]?.id ?? null
|
||||
}
|
||||
};
|
||||
res.reply({
|
||||
type: "application/json",
|
||||
body: JSON.stringify(rows?.shift() || [])
|
||||
body: JSON.stringify(rows)
|
||||
});
|
||||
} catch(err) {
|
||||
res.reply({
|
||||
code: 500,
|
||||
body: JSON.stringify(err)
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
router.get(/^\/api\/v1\/user\/.*(\/[0-9]+)?$/, async (req, res) => { // auf qs umstellen
|
||||
const user = req.url.split[3];
|
||||
const eps = Math.min(req.url.split[4] || 50, 50);
|
||||
try {
|
||||
const rows = await sql.query(queries.user, [ user, eps ]);
|
||||
const eps = +req.url.split[4] || 50;
|
||||
|
||||
const rows = await sql("items")
|
||||
.select("id", "mime", "size", "src", "stamp", "userchannel", "username", "usernetwork")
|
||||
.where("username", user)
|
||||
.orderBy("stamp", "desc")
|
||||
.limit(eps);
|
||||
|
||||
res.reply({
|
||||
type: "application/json",
|
||||
body: JSON.stringify(rows.length > 0 ? rows : [])
|
||||
});
|
||||
} catch(err) {
|
||||
res.reply({
|
||||
code: 500,
|
||||
body: JSON.stringify(err)
|
||||
});
|
||||
}
|
||||
});
|
||||
|
|
|
@ -1,80 +1,32 @@
|
|||
import router from "../router.mjs";
|
||||
import sql from "../sql.mjs";
|
||||
import { parse } from "url";
|
||||
import cfg from "../../../config.json";
|
||||
|
||||
import { mimes, queries } from "./inc/apiv2.mjs";
|
||||
const allowedMimes = [ "audio", "image", "video", "%" ];
|
||||
|
||||
router.get("/api/v2", (req, res) => {
|
||||
res.end("api lol");
|
||||
});
|
||||
|
||||
router.get(/^\/api\/v2\/random(\/user\/.+|\/image|\/video|\/audio)?$/, async (req, res) => {
|
||||
const args = [];
|
||||
let q = queries.random.main;
|
||||
const user = req.url.split[3] === "user" ? req.url.split[4] : "%";
|
||||
const mime = (allowedMimes.filter(n => req.url.split[3]?.startsWith(n))[0] ? req.url.split[3] : "") + "%";
|
||||
|
||||
if(req.url.split[3] === "user") {
|
||||
q += queries.random.where("username like ?");
|
||||
args.push(req.url.split[4] || "flummi");
|
||||
}
|
||||
else
|
||||
q += queries.random.where(mimes[req.url.split[3]] ? mimes[req.url.split[3]].map(mime => `mime = "${mime}"`).join(" or ") : null);
|
||||
const rows = await sql("items").orderByRaw("rand()").limit(1).where("mime", "like", mime).andWhere("username", "like", user);
|
||||
|
||||
try {
|
||||
const rows = await sql.query(q, args);
|
||||
res
|
||||
.writeHead(200, { "Content-Type": "application/json" })
|
||||
.end(JSON.stringify(rows.length > 0 ? rows[0] : []), "utf-8");
|
||||
} catch(err) {
|
||||
res
|
||||
.writeHead(500)
|
||||
.end(JSON.stringify(err), "utf-8");
|
||||
}
|
||||
});
|
||||
|
||||
router.get("/api/v2/p", async (req, res) => {
|
||||
let id = parseInt(req.url.qs.id) || 99999999;
|
||||
const eps = Math.min(parseInt(req.url.qs.eps) || 100, 200);
|
||||
let [ order, trend ] = req.url.qs.order === "asc" ? [ "asc", ">" ] : [ "desc", "<" ];
|
||||
|
||||
try {
|
||||
const tmp = (await sql.query("select min(id) as min, max(id) as max from items limit 1"))[0];
|
||||
if((id - 1 + eps) > tmp.max) {
|
||||
id = tmp.max;
|
||||
[ order, trend ] = [ "desc", "<=" ];
|
||||
}
|
||||
if((id + 1 - eps) < tmp.min) {
|
||||
id = tmp.min;
|
||||
[ order, trend ] = [ "asc", ">=" ];
|
||||
}
|
||||
|
||||
const rows = await sql.query(queries.p(trend, order), [ id, eps ]);
|
||||
const items = {
|
||||
items: rows,
|
||||
first: rows[0].id,
|
||||
last: rows[rows.length - 1].id,
|
||||
newest: tmp.max,
|
||||
oldest: tmp.min
|
||||
};
|
||||
res
|
||||
.writeHead(200, { "Content-Type": "application/json" })
|
||||
.end(JSON.stringify(items), "utf-8");
|
||||
} catch(err) {
|
||||
console.error(err);
|
||||
res
|
||||
.writeHead(500)
|
||||
.end(JSON.stringify(err), "utf-8");
|
||||
}
|
||||
});
|
||||
|
||||
router.get(/^\/api\/v2\/p\/([0-9]+)/, async (req, res) => { // legacy
|
||||
let eps = 100;
|
||||
let id = +req.url.split[3];
|
||||
|
||||
const query = await sql.query("select * from items where id < ? order by id desc limit ?", [ id, eps ]);
|
||||
const rows = await sql("items").where("id", "<", id).orderBy("id", "desc").limit(eps);
|
||||
|
||||
const items = {
|
||||
items: query,
|
||||
last: query[query.length - 1].id
|
||||
items: rows,
|
||||
last: rows[rows.length - 1].id
|
||||
};
|
||||
|
||||
res.writeHead(200, { "Content-Type": "application/json" });
|
||||
|
@ -82,33 +34,40 @@ router.get(/^\/api\/v2\/p\/([0-9]+)/, async (req, res) => { // legacy
|
|||
});
|
||||
|
||||
router.get(/^\/api\/v2\/item\/[0-9]+$/, async (req, res) => {
|
||||
try {
|
||||
const rows = await sql.query(queries.item, Array(3).fill(req.url.split[3]));
|
||||
const id = +req.url.split[3];
|
||||
|
||||
const item = await sql("items").where("id", id).limit(1);
|
||||
const next = await sql("items").select("id").where("id", ">", id).orderBy("id").limit(1);
|
||||
const prev = await sql("items").select("id").where("id", "<", id).orderBy("id", "desc").limit(1);
|
||||
|
||||
if(item.length === 0)
|
||||
return "nope";
|
||||
|
||||
const rows = {
|
||||
...item[0],
|
||||
...{
|
||||
next: next[0]?.id ?? null,
|
||||
prev: prev[0]?.id ?? null
|
||||
}
|
||||
};
|
||||
res.reply({
|
||||
type: "application/json",
|
||||
body: JSON.stringify(rows?.shift() || [])
|
||||
body: JSON.stringify(rows)
|
||||
});
|
||||
} catch(err) {
|
||||
res.reply({
|
||||
code: 500,
|
||||
body: JSON.stringify(err)
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
router.get(/^\/api\/v2\/user\/.*(\/\d+)?$/, async (req, res) => { // auf qs umstellen
|
||||
const user = req.url.split[3];
|
||||
const eps = +req.url.split[4] || 50;
|
||||
try {
|
||||
const rows = await sql.query(queries.user, [ user, eps ]);
|
||||
|
||||
const rows = await sql("items")
|
||||
.select("id", "mime", "size", "src", "stamp", "userchannel", "username", "usernetwork")
|
||||
.where("username", user)
|
||||
.orderBy("stamp", "desc")
|
||||
.limit(eps);
|
||||
|
||||
res.reply({
|
||||
type: "application/json",
|
||||
body: JSON.stringify(rows.length > 0 ? rows : [])
|
||||
});
|
||||
} catch(err) {
|
||||
res.reply({
|
||||
code: 500,
|
||||
body: JSON.stringify(err)
|
||||
});
|
||||
}
|
||||
});
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
export const mimes = {
|
||||
image: [ "image/png", "image/gif", "image/jpeg" ],
|
||||
video: [ "video/webm", "video/mp4", "video/quicktime" ],
|
||||
audio: [ "audio/mpeg", "audio/flac", "audio/x-flac", "audio/ogg" ]
|
||||
};
|
||||
|
||||
export const queries = {
|
||||
random: {
|
||||
main: "select id, mime, size, username, userchannel, usernetwork, stamp, dest, src from items ",
|
||||
where: where => `${where?`where ${where}`:""} order by rand() limit 1`
|
||||
},
|
||||
item: "select id, mime, dest, size, src, stamp, userchannel, username, usernetwork, "
|
||||
+ "(select id from items where id = (select min(id) from items where id > ?)) as next, "
|
||||
+ "(select id from items where id = (select max(id) from items where id < ?)) as prev "
|
||||
+ "from items where items.id = ? limit 1",
|
||||
p: (trend, order) => `select id, mime from items where id ${trend} ? order by id ${order} limit ?`,
|
||||
user: "select id, mime, size, src, stamp, userchannel, username, usernetwork from items where username = ? limit ?"
|
||||
};
|
|
@ -1,18 +0,0 @@
|
|||
export const mimes = {
|
||||
image: [ "image/png", "image/gif", "image/jpeg" ],
|
||||
video: [ "video/webm", "video/mp4", "video/quicktime" ],
|
||||
audio: [ "audio/mpeg", "audio/flac", "audio/x-flac", "audio/ogg" ]
|
||||
};
|
||||
|
||||
export const queries = {
|
||||
random: {
|
||||
main: "select id, mime, size, username, userchannel, usernetwork, stamp, dest, src from items ",
|
||||
where: where => `${where?`where ${where}`:""} order by rand() limit 1`
|
||||
},
|
||||
item: "select id, mime, dest, size, src, stamp, userchannel, username, usernetwork, "
|
||||
+ "(select id from items where id = (select min(id) from items where id > ?)) as next, "
|
||||
+ "(select id from items where id = (select max(id) from items where id < ?)) as prev "
|
||||
+ "from items where items.id = ? limit 1",
|
||||
p: (trend, order) => `select id, mime from items where id ${trend} ? order by id ${order} limit ?`,
|
||||
user: "select id, mime, size, src, stamp, userchannel, username, usernetwork from items where username = ? order by stamp desc limit ?"
|
||||
};
|
|
@ -1,12 +0,0 @@
|
|||
export const mimes = {
|
||||
image: [ "image/png", "image/gif", "image/jpeg" ],
|
||||
video: [ "video/webm", "video/mp4", "video/quicktime" ],
|
||||
audio: [ "audio/mpeg", "audio/flac", "audio/x-flac", "audio/ogg" ]
|
||||
};
|
||||
|
||||
export const queries = {
|
||||
random: {
|
||||
main: "select id, mime, size, username, userchannel, usernetwork, stamp, dest, src from items ",
|
||||
where: where => `${where?`where ${where}`:""} order by rand() limit 1`
|
||||
}
|
||||
};
|
|
@ -2,42 +2,41 @@ import router from "../router.mjs";
|
|||
import cfg from "../../../config.json";
|
||||
import url from "url";
|
||||
import fs from "fs";
|
||||
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(/^\/(audio\/?|image\/?|video\/?|user\/?)?(p\/\d+)?$/, async (req, res) => {
|
||||
router.get(/^\/(audio\/?|image\/?|video\/?)?(p\/\d+)?$/, async (req, res) => {
|
||||
try {
|
||||
let mime = false;
|
||||
let q = false;
|
||||
//if(req.url.split[0] == "user")
|
||||
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 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 total = (await sql("items").where("mime", "like", mime).count("* as total"))[0].total;
|
||||
|
||||
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 page = Math.min(pages, +req.url.split[tmpmime.length > 0 ? 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 ]);
|
||||
const rows = await sql("items")
|
||||
.select("id", "mime")
|
||||
.where("mime", "like", mime)
|
||||
.orderBy("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);
|
||||
|
||||
query.forEach(e => {
|
||||
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: query,
|
||||
items: rows,
|
||||
pagination: {
|
||||
start: 1,
|
||||
end: pages,
|
||||
|
@ -45,49 +44,46 @@ router.get(/^\/(audio\/?|image\/?|video\/?)?(p\/\d+)?$/, async (req, res) => {
|
|||
next: (page < pages) ? page + 1 : null,
|
||||
page: page,
|
||||
cheat: cheat,
|
||||
link: `/${mime ? mime + "/" : ""}p/`
|
||||
link: `/${tmpmime ? tmpmime + "/" : ""}p/`
|
||||
},
|
||||
last: query[query.length - 1].id,
|
||||
filter: mime ? mime : undefined,
|
||||
last: rows[rows.length - 1].id,
|
||||
filter: tmpmime ? tmpmime : undefined,
|
||||
themes: cfg.websrv.themes,
|
||||
theme: (typeof req.cookies.theme !== "undefined" && cfg.websrv.themes.includes(req.cookies.theme)) ? req.cookies.theme : cfg.websrv.themes[0]
|
||||
};
|
||||
|
||||
res.reply({ body: tpl.render("views/index", data) });
|
||||
} catch(err) {
|
||||
console.log(err);
|
||||
res.reply({ body: "sorry bald wieder da lol :(" });
|
||||
}
|
||||
});
|
||||
|
||||
router.get(/^\/((audio\/|video\/|image\/)?[0-9]+)$/, async (req, res) => {
|
||||
let id = false;
|
||||
let mime = false;
|
||||
let q = false;
|
||||
let mime = "";
|
||||
let tmpmime = false;
|
||||
|
||||
if(['audio', 'video', 'image'].includes(req.url.split[0])) {
|
||||
mime = req.url.split[0];
|
||||
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];
|
||||
q = mimes[mime] ? mimes[mime].map(_mime => `mime = "${_mime}"`).join(" or ") : null;
|
||||
}
|
||||
else
|
||||
else {
|
||||
mime = "%";
|
||||
id = +req.url.split[0];
|
||||
}
|
||||
mime += "/%";
|
||||
|
||||
const query = (await sql.query(`select items.* from items where items.id = ? ${mime ? "and ("+q+")" : ""} limit 1`, [ id ]))?.shift();
|
||||
const query = (await sql("items").where("id", id).andWhere("mime", "like", mime).limit(1))?.shift();
|
||||
|
||||
if(!query?.id)
|
||||
return res.redirect("/404");
|
||||
|
||||
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 tags = await sql("tags_assign").leftJoin("tags", "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 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 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 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 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;
|
||||
|
||||
|
@ -116,15 +112,15 @@ router.get(/^\/((audio\/|video\/|image\/)?[0-9]+)$/, async (req, res) => {
|
|||
},
|
||||
title: `${query.id} - f0ck.me`,
|
||||
pagination: {
|
||||
start: qmax,
|
||||
end: qmin,
|
||||
start: qmax[0].id,
|
||||
end: qmin[0].id,
|
||||
prev: next,
|
||||
next: prev,
|
||||
page: query.id,
|
||||
cheat: cheat,
|
||||
link: `/${mime ? mime + "/" : ""}`
|
||||
link: `/${tmpmime ? tmpmime + "/" : ""}`
|
||||
},
|
||||
filter: mime ? mime : undefined,
|
||||
filter: tmpmime ? tmpmime : undefined,
|
||||
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)]
|
||||
|
|
|
@ -1,19 +1,11 @@
|
|||
import router from "../router.mjs";
|
||||
import { mimes, queries } from "./inc/index.mjs";
|
||||
import sql from "../sql.mjs";
|
||||
|
||||
const allowedMimes = [ "audio", "image", "video", "%" ];
|
||||
|
||||
router.get(/^\/random(\/image|\/video|\/audio)?$/, async (req, res) => {
|
||||
const args = [];
|
||||
let q = queries.random.main;
|
||||
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);
|
||||
|
||||
q += queries.random.where(mimes[req.url.split[1]] ? mimes[req.url.split[1]].map(mime => `mime = "${mime}"`).join(" or ") : null);
|
||||
|
||||
try {
|
||||
const rows = await sql.query(q, args);
|
||||
res.redirect(`/${req.url.split[1] ? req.url.split[1] + "/" : ""}${rows[0].id}`);
|
||||
} catch(err) {
|
||||
res
|
||||
.writeHead(500)
|
||||
.end(JSON.stringify(err), "utf-8");
|
||||
}
|
||||
});
|
||||
|
|
|
@ -4,14 +4,15 @@ import util from "util";
|
|||
import sql from "../sql.mjs";
|
||||
|
||||
router.get("/stats", async (req, res) => {
|
||||
const query = await sql.query("select src from items");
|
||||
const rows = await sql("items").select("src");
|
||||
let hosts = {};
|
||||
query.forEach(e => {
|
||||
rows.forEach(e => {
|
||||
const host = url.parse(e.src).hostname;
|
||||
hosts[host] ? hosts[host]++ : hosts[host] = 1;
|
||||
});
|
||||
const data = util.inspect(Object.keys(hosts).sort((a, b) => hosts[b] - hosts[a]).map(k => ({ [k]: hosts[k] })).reduce((a, b) => ({ ...a, ...b })));
|
||||
|
||||
res.reply({
|
||||
body: "<pre>" + util.inspect(Object.keys(hosts).sort((a, b) => hosts[b] - hosts[a]).map(k => ({ [k]: hosts[k] })).reduce((a, b) => ({ ...a, ...b }))) + "</pre>"
|
||||
body: "<pre>" + data + "</pre>"
|
||||
});
|
||||
});
|
||||
|
|
|
@ -11,7 +11,7 @@ router.get(/^\/tags(\/\w+)?/, async (req, res) => {
|
|||
console.log(tag);
|
||||
|
||||
return res.reply({ body: "wip" });
|
||||
const total = (await sql.query("select count(*) as total from items" + (mime ? q : "")))[0].total;
|
||||
/*const total = (await sql.query("select count(*) as total from items" + (mime ? q : "")))[0].total;
|
||||
const limit = 299;
|
||||
const pages = +Math.ceil(total / limit);
|
||||
const page = Math.min(pages, +req.url.split[mime ? 2 : 1] || 1);
|
||||
|
@ -43,7 +43,7 @@ router.get(/^\/tags(\/\w+)?/, async (req, res) => {
|
|||
filter: mime ? mime : undefined
|
||||
};
|
||||
|
||||
res.reply({ body: tpl.render("views/index", data) });
|
||||
res.reply({ body: tpl.render("views/index", data) });*/
|
||||
} catch(err) {
|
||||
console.log(err);
|
||||
res.reply({ body: "sorry bald wieder da lol :(" });
|
||||
|
|
|
@ -1,38 +1,19 @@
|
|||
import mariadb from "mariadb";
|
||||
import knex from "knex";
|
||||
import callback from "mariadb/callback.js";
|
||||
import Client_MySQL from "knex/lib/dialects/mysql/index.js";
|
||||
import cfg from "../../config.json";
|
||||
|
||||
const sql = mariadb.createPool(cfg.sql);
|
||||
class Client_MariaDB extends Client_MySQL {
|
||||
driverName = "mariadb";
|
||||
_driver() {
|
||||
return callback;
|
||||
}
|
||||
validateConnection(conn) {
|
||||
return conn.isValid();
|
||||
}
|
||||
};
|
||||
|
||||
export default sql;
|
||||
//export default sql = mariadb.createPool(cfg.sql);//.getConnection();
|
||||
|
||||
/*const allowedMimes = [ "audio", "video", "image", "%" ];
|
||||
const allowedColumns = [ "id", "mime", "size", "username", "userchannel", "usernetwork", "stamp", "dest", "src" ];
|
||||
|
||||
export const getItems = async ({
|
||||
select = [],
|
||||
user = "%",
|
||||
mime = "%",
|
||||
orderby = "stamp",
|
||||
order = "desc",
|
||||
offset = 0,
|
||||
limit = 5
|
||||
}) => {
|
||||
select = (select.length === 0 ? allowedColumns : select).join(", ");
|
||||
order = !["asc", "desc"].includes(order) ? "asc" : order;
|
||||
offset = +offset;
|
||||
limit = +limit;
|
||||
mime = allowedMimes.filter(n => mime.startsWith(n))[0] ? mime : "%";
|
||||
const query =
|
||||
`select
|
||||
${select}
|
||||
from
|
||||
items
|
||||
where
|
||||
username like ? and
|
||||
mime like ?
|
||||
order by ${orderby} ${order}
|
||||
limit ?, ?
|
||||
`;
|
||||
return await sql.query(query, [ user, mime, offset, limit ]);
|
||||
};*/
|
||||
export default knex({
|
||||
client: Client_MariaDB,
|
||||
connection: cfg.sql
|
||||
});
|
||||
|
|
|
@ -14,14 +14,14 @@ export default async bot => {
|
|||
if(id <= 0)
|
||||
return false;
|
||||
|
||||
const f0ck = await sql.query("select dest from items where id = ? limit 1", [ id ]);
|
||||
const f0ck = await sql("items").select("dest").where("id", id).limit(1);
|
||||
if(f0ck.length === 0)
|
||||
return false;
|
||||
|
||||
await fs.unlink(`./public/b/${f0ck[0].dest}`).catch(_=>{});
|
||||
await fs.unlink(`./public/t/${id}`).catch(_=>{});
|
||||
|
||||
await sql.query("delete from items where id = ? limit 1", [ id ]);
|
||||
await sql("items").where("id", id).del().limit(1);
|
||||
|
||||
return id;
|
||||
}))).filter(d => d);
|
||||
|
@ -29,7 +29,7 @@ export default async bot => {
|
|||
if(ret.length > 0)
|
||||
e.reply(`deleted ${ret.length}/${e.args.length} (${ret.join(",")}) f0cks`);
|
||||
else
|
||||
e.reply(`oof`)
|
||||
e.reply(`oof`);
|
||||
}
|
||||
}]
|
||||
};
|
||||
|
|
|
@ -26,7 +26,7 @@ export default async bot => {
|
|||
case "limit":
|
||||
return e.reply(`up to ${lib.formatSize(cfg.main.maxfilesize)} (${lib.formatSize(cfg.main.maxfilesize * 2.5)} for admins)`);
|
||||
case "thumb":
|
||||
const rows = await sql.query("select id from items");
|
||||
const rows = await sql("items").select("id");
|
||||
const dir = (await fs.readdir("./public/t")).filter(d => d.endsWith(".png")).map(e => +e.split(".")[0]);
|
||||
const tmp = [];
|
||||
for(let row of rows)
|
||||
|
|
|
@ -2,7 +2,6 @@ import cfg from "../../../config.json";
|
|||
import sql from "../sql.mjs";
|
||||
import lib from "../lib.mjs";
|
||||
|
||||
const _query = "select id, mime, size, username, userchannel, usernetwork, stamp from items where ";
|
||||
const regex = /(https\:\/\/f0ck\.me|http\:\/\/fockmoonsb24iczs7odozzy5uktlzbcgp337nabrgffzxv5ihabgpvyd\.onion)(\/(video|image|audio))?\/(\d+|(?:b\/)(\w{8})\.(jpg|webm|gif|mp4|png|mov|mp3|ogg|flac))/gi;
|
||||
|
||||
export default async bot => {
|
||||
|
@ -13,18 +12,14 @@ export default async bot => {
|
|||
active: true,
|
||||
f: async e => {
|
||||
const dat = e.message.match(regex)[0].split(/\//).pop();
|
||||
let query, arg;
|
||||
let query = sql("items").select("id", "mime", "size", "username", "userchannel", "usernetwork", "stamp");
|
||||
|
||||
if(dat.includes(".")) {
|
||||
query = _query + "dest like ?";
|
||||
arg = `%${dat}%`;
|
||||
}
|
||||
else {
|
||||
query = _query + "id = ?";
|
||||
arg = dat;
|
||||
}
|
||||
if(dat.includes("."))
|
||||
query = query.where("dest", "like", `%${dat}%`);
|
||||
else
|
||||
query = query.where("id", dat);
|
||||
|
||||
const rows = await sql.query(query, [ arg ]);
|
||||
const rows = await query;
|
||||
if(rows.length === 0)
|
||||
return e.reply("no f0cks given! lol D:");
|
||||
|
||||
|
|
|
@ -9,31 +9,22 @@ export default async bot => {
|
|||
call: /^gib f0ck/i,
|
||||
active: true,
|
||||
f: async e => {
|
||||
let args = e.args.slice(2);
|
||||
let params = {
|
||||
inc: [],
|
||||
exc: []
|
||||
};
|
||||
let args = e.args.slice(1);
|
||||
let rows = sql("items").select("id", "username", "mime", "size");
|
||||
|
||||
for(let i = 0; i < args.length; i++) {
|
||||
let name = args[0];
|
||||
if(name.charAt(0) === "!")
|
||||
params.exc.push(name.slice(1));
|
||||
if(args[i].charAt(0) === "!")
|
||||
rows = rows.where("username", "not like", args[i].slice(1));
|
||||
else
|
||||
params.inc.push(name);
|
||||
rows = rows.where("username", "like", args[i]);
|
||||
}
|
||||
|
||||
let vars = params.inc.concat(params.inc.length === 0 ? params.exc : []);
|
||||
params.inc = new Array(params.inc.length).fill("username like ?");
|
||||
params.exc = new Array(params.inc.length == 0 ? params.exc.length : 0).fill("username not like ?");
|
||||
const where = params.inc.concat(params.exc).join(" || ");
|
||||
const query = `select id, username, mime, size from items ${where.length > 0 ? `where ${where}` : ""} order by rand() limit 1`;
|
||||
rows = await rows.orderByRaw("rand()").limit(1);
|
||||
|
||||
const rows = await sql.query(query, vars);
|
||||
if(rows.length === 0)
|
||||
return e.reply("nothing found, f0cker");
|
||||
|
||||
e.reply(`f0ckrnd: ${cfg.main.url}/${rows[0].id} by: ${rows[0].username} (${rows[0].mime}, ~${lib.formatSize(rows[0].size)})`);
|
||||
return e.reply(`f0ckrnd: ${cfg.main.url}/${rows[0].id} by: ${rows[0].username} (${rows[0].mime}, ~${lib.formatSize(rows[0].size)})`);
|
||||
}
|
||||
}];
|
||||
};
|
||||
|
|
|
@ -8,7 +8,8 @@ import { exec as _exec } from "child_process";
|
|||
import { promisify } from "util";
|
||||
const exec = promisify(_exec);
|
||||
|
||||
const regex = /https?:\/\/[\w\S(\.|:|/)]+/gi;
|
||||
//const regex = /https?:\/\/[\w\S(\.|:|/)]+/gi;
|
||||
const regex = /https?:\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?/gi;
|
||||
|
||||
export default async bot => {
|
||||
|
||||
|
@ -35,12 +36,12 @@ export default async bot => {
|
|||
|
||||
links.forEach(async link => {
|
||||
// check repost (link)
|
||||
const q_repost = await sql.query("select id from items where src = ?", [ link ]);
|
||||
const q_repost = await sql("items").select("id").where("src", link);
|
||||
if(q_repost.length > 0)
|
||||
return e.reply(`repost motherf0cker (link): ${cfg.main.url}/${q_repost[0].id}`);
|
||||
|
||||
// generate uuid
|
||||
const uuid = (await sql.query("select left(uuid(), 8) as uuid"))[0].uuid;
|
||||
const uuid = (await sql.select(sql.raw("left(uuid(), 8) as uuid")))[0].uuid;
|
||||
|
||||
const maxfilesize = (getLevel(e.user).level > 50 ? cfg.main.maxfilesize * 2.5 : cfg.main.maxfilesize) / 1024;
|
||||
|
||||
|
@ -48,7 +49,6 @@ export default async bot => {
|
|||
// read metadata
|
||||
try {
|
||||
meta = JSON.parse((await exec(`youtube-dl -f "bestvideo[ext=mp4,filesize<${maxfilesize}k][width<2000][height<=1200]+bestaudio[ext=m4a,filesize<${maxfilesize}k]/bestvideo[width<2000][height<=1200]+bestaudio/best[width<2000][height<=1200]/best" --skip-download --dump-json "${link}"`)).stdout);
|
||||
//meta = JSON.parse((await exec(`youtube-dl -f "bestvideo[ext=mp4,filesize<${maxfilesize}k]+bestaudio/best" --skip-download --dump-json "${link}"`)).stdout);
|
||||
}
|
||||
catch(err) {
|
||||
//e.reply("(╯° °)╯︵ ┻━┻)");
|
||||
|
@ -56,6 +56,9 @@ export default async bot => {
|
|||
return;
|
||||
}
|
||||
|
||||
if(!cfg.ext.includes(meta.ext.toLowerCase()))
|
||||
return;
|
||||
|
||||
let filename = `${uuid}.${meta.ext}`;
|
||||
|
||||
//e.reply(`downloading ${uuid}...`);
|
||||
|
@ -65,18 +68,14 @@ export default async bot => {
|
|||
const start = new Date();
|
||||
let source;
|
||||
if(meta.ext === "mp4") {
|
||||
//source = (await exec(`youtube-dl "${link}" --max-filesize ${maxfilesize}k -f "bestvideo[ext=mp4,filesize<${maxfilesize}k]+bestaudio/best" --merge-output-format mp4 -o ./tmp/${filename}`)).stdout.trim();
|
||||
source = (await exec(`youtube-dl "${link}" --max-filesize ${maxfilesize}k -f "bestvideo[ext=mp4,filesize<${maxfilesize}k][width<2000][height<=1200]+bestaudio[ext=m4a,filesize<${maxfilesize}k]/bestvideo[width<2000][height<=1200]+bestaudio/best[width<2000][height<=1200]/best" --merge-output-format mp4 -o ./tmp/${filename}`)).stdout.trim();
|
||||
console.log("mp4 lol");
|
||||
//console.log("mp4 lol");
|
||||
}
|
||||
else {
|
||||
//source = (await exec(`youtube-dl "${link}" --max-filesize ${maxfilesize}k -f "bestvideo[filesize<${maxfilesize}k][width<2000][height<=1200]+bestaudio[ext=${meta.ext},filesize<${maxfilesize}k]/bestvideo[width<2000][height<=1200]+bestaudio/best[ext=${meta.ext},width<2000][height<=1200]/best" --merge-output-format ${meta.ext} -o ./tmp/${filename}`)).stdout.trim();
|
||||
source = (await exec(`youtube-dl "${link}" --max-filesize ${maxfilesize}k -f "bestvideo[filesize<${maxfilesize}k][width<2000][height<=1200][ext=${meta.ext}]+bestaudio[filesize<${maxfilesize}k][ext=${meta.ext}]/best" -o ./tmp/${filename}`)).stdout.trim();
|
||||
//source = (await exec(`youtube-dl "${link}" --max-filesize ${maxfilesize}k -f "bestvideo[ext=mp4,filesize<${maxfilesize}k][width<2000][height<=1200]+bestaudio[ext=m4a,filesize<${maxfilesize}k]/bestvideo[width<2000][height<=1200]+bestaudio/best[width<2000][height<=1200]/best" -o ./tmp/${filename}`)).stdout.trim();
|
||||
//source = (await exec(`youtube-dl "${link}" --max-filesize ${maxfilesize}k -f "bestvideo[ext=mp4,filesize<${maxfilesize}k]+bestaudio/best" -o ./tmp/${filename}`)).stdout.trim();
|
||||
console.log("alles andere lol");
|
||||
//console.log("alles andere lol");
|
||||
}
|
||||
console.log(source);
|
||||
//console.log(source);
|
||||
|
||||
if(source.match(/larger than/))
|
||||
return e.reply("too large lol");
|
||||
|
@ -115,17 +114,25 @@ export default async bot => {
|
|||
}
|
||||
|
||||
// check repost (checksum)
|
||||
const q_repostc = await sql.query("select id from items where checksum = ?", [ checksum ]);
|
||||
const q_repostc = await sql("items").select("id").where("checksum", checksum);
|
||||
if(q_repostc.length > 0)
|
||||
return e.reply(`repost motherf0cker (checksum): ${cfg.main.url}/${q_repostc[0].id}`);
|
||||
|
||||
await fs.promises.copyFile(`./tmp/${filename}`, `./public/b/${filename}`);
|
||||
await fs.promises.unlink(`./tmp/${filename}`).catch(_=>{});
|
||||
|
||||
const insertq = await sql.query(
|
||||
"insert into items (src, dest, mime, size, checksum, username, userchannel, usernetwork, stamp, active) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
[ e.photo ? "" : link, filename, mime, size, checksum, e.user.nick || e.user.username, e.channel, e.network, ~~(new Date() / 1000), 1 ]
|
||||
);
|
||||
const insertq = (await sql("items").insert({
|
||||
src: e.photo ? "" : link,
|
||||
dest: filename,
|
||||
mime: mime,
|
||||
size: size,
|
||||
checksum: checksum,
|
||||
username: e.user.nick || e.user.username,
|
||||
userchannel: e.channel,
|
||||
usernetwork: e.network,
|
||||
stamp: ~~(new Date() / 1000),
|
||||
active: 1
|
||||
}))[0];
|
||||
|
||||
// generate thumbnail
|
||||
try {
|
||||
|
@ -134,17 +141,17 @@ export default async bot => {
|
|||
if(mime.startsWith("image") && mime !== "image/gif")
|
||||
thumb_orig = `./public/b/${filename}`;
|
||||
else if(!mime.startsWith("audio")) {
|
||||
await exec(`ffmpegthumbnailer -i./public/b/${filename} -s1024 -o./tmp/${insertq.insertId}`);
|
||||
thumb_orig = `./tmp/${insertq.insertId}`;
|
||||
await exec(`ffmpegthumbnailer -i./public/b/${filename} -s1024 -o./tmp/${insertq}`);
|
||||
thumb_orig = `./tmp/${insertq}`;
|
||||
}
|
||||
else if(mime.startsWith("audio")) {
|
||||
await exec(`ffmpeg -i ./public/b/${filename} -update 1 -map 0:v -map 0:1 -c copy ./tmp/${insertq.insertId}.png`)
|
||||
await exec(`cp ./tmp/${insertq.insertId}.png ./public/ca/${insertq.insertId}.png`)
|
||||
thumb_orig = `./tmp/${insertq.insertId}.png`;
|
||||
await exec(`ffmpeg -i ./public/b/${filename} -update 1 -map 0:v -map 0:1 -c copy ./tmp/${insertq}.png`)
|
||||
await exec(`cp ./tmp/${insertq}.png ./public/ca/${insertq}.png`)
|
||||
thumb_orig = `./tmp/${insertq}.png`;
|
||||
}
|
||||
}
|
||||
await exec(`convert "${thumb_orig}" -resize "200x200^" -gravity center -crop 128x128+0+0 +repage ./public/t/${insertq.insertId}.png`);
|
||||
await fs.promises.unlink(`./tmp/${insertq.insertId}`).catch(_=>{});
|
||||
await exec(`convert "${thumb_orig}" -resize "200x200^" -gravity center -crop 128x128+0+0 +repage ./public/t/${insertq}.png`);
|
||||
await fs.promises.unlink(`./tmp/${insertq}`).catch(_=>{});
|
||||
} catch(err) {
|
||||
e.reply("\x033>no thumb lol");
|
||||
console.error(err);
|
||||
|
@ -155,8 +162,8 @@ export default async bot => {
|
|||
|
||||
e.reply([
|
||||
//`title: ${meta.fulltitle}`,
|
||||
`link: ${cfg.main.url}/${insertq.insertId} | size: ${lib.formatSize(size)} | speed: ${speed}`
|
||||
//`link: ${cfg.main.url}/${insertq.insertId}`
|
||||
`link: ${cfg.main.url}/${insertq} | size: ${lib.formatSize(size)} | speed: ${speed}`
|
||||
//`link: ${cfg.main.url}/${insertq}`
|
||||
]);
|
||||
|
||||
});
|
||||
|
|
|
@ -2,14 +2,10 @@ import sql from "../sql.mjs";
|
|||
import cfg from "../config.mjs";
|
||||
import { getLevel } from "../admin.mjs";
|
||||
|
||||
const queries = {
|
||||
getTags: "select tags.id, tags.tag from tags_assign left join tags on tags.id = tags_assign.tag_id where tags_assign.item_id = ?",
|
||||
assignTag: "insert into tags_assign (tag_id, item_id, prefix) values (?, ?, ?)"
|
||||
};
|
||||
|
||||
const getTags = async itemid => {
|
||||
return await sql.query(queries.getTags, [ itemid ]);
|
||||
};
|
||||
const getTags = async itemid => await sql("tags_assign")
|
||||
.leftJoin("tags", "tags.id", "tags_assign.tag_id")
|
||||
.where("tags_assign.item_id", itemid)
|
||||
.select("tags.id", "tags.tag");
|
||||
|
||||
export default async bot => {
|
||||
return [{
|
||||
|
@ -21,7 +17,7 @@ export default async bot => {
|
|||
const id = +e.args[1];
|
||||
if(!id)
|
||||
return e.reply("lol no");
|
||||
const tags = await getTags(id).map(t => t.tag);
|
||||
const tags = (await getTags(id)).map(t => t.tag);
|
||||
if(tags.length === 0)
|
||||
return e.reply(`item ${cfg.main.url}/${id} has no tags!`);
|
||||
return e.reply(`item ${cfg.main.url}/${id} is tagged as: ${tags.join(', ')}`);
|
||||
|
@ -48,13 +44,20 @@ export default async bot => {
|
|||
await Promise.all(newtags.map(async ntag => {
|
||||
try {
|
||||
let tagid;
|
||||
const tag_exists = (await sql.query(`select id, tag from tags where tag = ?`, [ ntag ]));
|
||||
if(tag_exists.length === 0) // create new tag
|
||||
tagid = (await sql.query("insert into tags (tag) values (?)", [ ntag ])).insertId;
|
||||
else
|
||||
const tag_exists = await sql("tags").select("id", "tag").where("tag", ntag);
|
||||
if(tag_exists.length === 0) { // create new tag
|
||||
tagid = (await sql("tags").insert({
|
||||
tag: ntag
|
||||
}))[0];
|
||||
}
|
||||
else {
|
||||
tagid = tag_exists[0].id;
|
||||
|
||||
return await sql.query(queries.assignTag, [ tagid, id, `${e.user.prefix}${e.channel}` ]);
|
||||
}
|
||||
return await sql("tags_assign").insert({
|
||||
tag_id: tagid,
|
||||
item_id: id,
|
||||
prefix: `${e.user.prefix}${e.channel}`
|
||||
});
|
||||
} catch(err) {
|
||||
console.error(err);
|
||||
}
|
||||
|
@ -94,14 +97,12 @@ export default async bot => {
|
|||
};
|
||||
}
|
||||
|
||||
let q;
|
||||
if(getLevel(e.user).level > 50)
|
||||
q = !!(await sql.query("delete from tags_assign where tag_id = ? and item_id = ? limit 1", [ tagid, id ])).affectedRows;
|
||||
else
|
||||
q = !!(await sql.query("delete from tags_assign where tag_id = ? and item_id = ? and prefix = ? limit 1", [ tagid, id, `${e.user.prefix}${e.channel}` ])).affectedRows;
|
||||
let q = sql("tags_assign").where("tag_id", tagid).andWhere("item_id", id).del();
|
||||
if(getLevel(e.user).level < 50)
|
||||
q = q.andWhere("prefix", `${e.user.prefix}${e.channel}`);
|
||||
|
||||
return {
|
||||
success: q,
|
||||
success: !!(await q),
|
||||
tag: rtag,
|
||||
tagid: tagid
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue
Block a user