115 lines
3.1 KiB
JavaScript
115 lines
3.1 KiB
JavaScript
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";
|
|
|
|
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;
|
|
|
|
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);
|
|
|
|
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 items = {
|
|
items: query,
|
|
last: query[query.length - 1].id
|
|
};
|
|
|
|
res.writeHead(200, { "Content-Type": "application/json" });
|
|
res.end(JSON.stringify(items), "utf-8");
|
|
});
|
|
|
|
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]));
|
|
res.reply({
|
|
type: "application/json",
|
|
body: JSON.stringify(rows?.shift() || [])
|
|
});
|
|
} catch(err) {
|
|
res.reply({
|
|
code: 500,
|
|
body: JSON.stringify(err)
|
|
});
|
|
}
|
|
});
|
|
|
|
router.get(/^\/api\/v2\/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 ]);
|
|
res.reply({
|
|
type: "application/json",
|
|
body: JSON.stringify(rows.length > 0 ? rows : [])
|
|
});
|
|
} catch(err) {
|
|
res.reply({
|
|
code: 500,
|
|
body: JSON.stringify(err)
|
|
});
|
|
}
|
|
});
|