This commit is contained in:
Flummi
2020-04-02 04:35:28 +02:00
parent 5ff96cdf5e
commit d39deeb038
100 changed files with 34498 additions and 1100 deletions

View File

@ -1,16 +1,15 @@
import router from "../router";
import sql from "../sql";
import router from "../router.mjs";
import sql from "../sql.mjs";
import { parse } from "url";
import cfg from "../../../config.json";
import { mimes, queries } from "./inc/api";
import { mimes, queries } from "./inc/api.mjs";
router.get(/^\/api$/, (req, res) => {
router.get("/api", (req, res) => {
res.end("api lol");
});
router.get(/^\/api\/random(\/user\/.+|\/image|\/video|\/audio)?$/, async (req, res) => {
const db = await sql.getConnection();
const args = [];
let q = queries.random.main;
@ -22,26 +21,24 @@ router.get(/^\/api\/random(\/user\/.+|\/image|\/video|\/audio)?$/, async (req, r
q += queries.random.where(mimes[req.url.split[2]] ? mimes[req.url.split[2]].map(mime => `mime = "${mime}"`).join(" or ") : null);
try {
const rows = await db.query(q, args);
const rows = await sql.query(q, args);
res
.writeHead(200, { 'Content-Type': 'application/json' })
.end(JSON.stringify(rows.length > 0 ? rows[0] : []), 'utf-8');
.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');
.end(JSON.stringify(err), "utf-8");
}
db.end();
});
router.get(/^\/api\/p$/, async (req, res) => {
const db = await sql.getConnection();
router.get("/api/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 db.query("select min(id) as min, max(id) as max from items limit 1"))[0];
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", "<=" ];
@ -51,7 +48,7 @@ router.get(/^\/api\/p$/, async (req, res) => {
[ order, trend ] = [ "asc", ">=" ];
}
const rows = await db.query(queries.p(trend, order), [ id, eps ]);
const rows = await sql.query(queries.p(trend, order), [ id, eps ]);
const items = {
items: rows,
first: rows[0].id,
@ -60,21 +57,33 @@ router.get(/^\/api\/p$/, async (req, res) => {
oldest: tmp.min
};
res
.writeHead(200, { 'Content-Type': 'application/json' })
.end(JSON.stringify(items), 'utf-8');
.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');
.end(JSON.stringify(err), "utf-8");
}
db.end();
});
router.get(/^\/api\/p\/([0-9]+)/, async (req, res) => { // legacy
let eps = 100;
let id = +req.url.split[2];
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\/item\/[0-9]+$/, async (req, res) => {
const db = await sql.getConnection();
try {
const rows = await db.query(queries.item, Array(3).fill(req.url.split[2]));
const rows = await sql.query(queries.item, Array(3).fill(req.url.split[2]));
const data = rows[0].length > 0 ? {
...rows[0][0], ...{
thumb: `${cfg.main.url}/t/${rows[0][0].id}.png`,
@ -86,29 +95,27 @@ router.get(/^\/api\/item\/[0-9]+$/, async (req, res) => {
error: true
};
res
.writeHead(200, { 'Content-Type': 'application/json' })
.end(JSON.stringify(data), 'utf-8');
.writeHead(200, { "Content-Type": "application/json" })
.end(JSON.stringify(data), "utf-8");
} catch(err) {
res
.writeHead(500)
.end(JSON.stringify(err), 'utf-8');
.end(JSON.stringify(err), "utf-8");
}
db.end();
});
router.get(/^\/api\/user\/.*(\/[0-9]+)?$/, async (req, res) => { // auf qs umstellen
const db = await sql.getConnection();
const user = req.url.split[2];
const eps = Math.min(req.url.split[3] || 50, 50);
try {
const rows = await db.query(queries.user, [ user, eps ]);
const rows = await sql.query(queries.user, [ user, eps ]);
res
.writeHead(200, { 'Content-Type': 'application/json' })
.end(JSON.stringify(rows.length > 0 ? rows : []), 'utf-8');
.writeHead(200, { "Content-Type": "application/json" })
.end(JSON.stringify(rows.length > 0 ? rows : []), "utf-8");
} catch(err) {
res
.writeHead(500)
.end(JSON.stringify(err), 'utf-8');
.end(JSON.stringify(err), "utf-8");
}
db.end();
});

View File

@ -1,10 +1,104 @@
import router from "../router";
import router from "../router.mjs";
import cfg from "../../../config.json";
import fs from "fs";
import sql from "../sql.mjs";
import swig from "swig";
import url from "url";
const tpl = fs.readFileSync("./views/index.html", "utf-8");
const templates = {
contact: fs.readFileSync("./views/contact.html", "utf-8"),
help: fs.readFileSync("./views/help.html", "utf-8"),
how: fs.readFileSync("./views/how.html", "utf-8"),
index: fs.readFileSync("./views/index.html", "utf-8"),
item: fs.readFileSync("./views/item.html", "utf-8")
};
router.get(/^\/(page\/[0-9]+)?$/, async (req, res) => {
res
.writeHead(200, { 'Content-Type': 'text/html' })
.end(tpl);
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[0].id
};
res.reply({
body: swig.compile(templates.index)(data)
});
});
router.get(/^\/([0-9]+)$/, async (req, res) => {
const q = "select * from items where id = ? limit 1; " // get item
+ "select id from items where id = (select min(id) from items where id > ?); " // get previous item
+ "select id from items where id = (select max(id) from items where id < ?)"; // get next item
const query = await sql.query(q, [req.url.split[0], req.url.split[0], req.url.split[0]]);
const data = {
id: '',
username: '',
item: '',
src: '',
dest: '',
mime: '',
size: '',
userchannel: '',
usernetwork: '',
thumb: null,
thumbnail: null,
next: null,
prev: null
};
if(query[0][0]) {
const e = query[0][0];
switch(e.mime) {
case "image/png":
case "image/jpeg":
case "image/gif":
data.item = "image";
break;
case "video/webm":
case "video/mp4":
case "video/quicktime":
data.item = "video";
break;
case "audio/mpeg":
case "audio/ogg":
case "audio/flac":
case "audio/x-flac":
data.item = "audio";
break;
}
data.id = e.id;
data.username = e.username;
data.srcurl = e.src;
data.src = url.parse(e.src).hostname;
data.thumb = `${cfg.websrv.paths.thumbnails}/${e.id}.png`;
data.dest = `${cfg.websrv.paths.images}/${e.dest}`;
data.mime = e.mime;
data.size = e.size;//lib.formatSize(e.size);
data.userchannel = e.userchannel;
data.usernetwork = e.usernetwork;
data.timestamp = new Date(e.stamp * 1000).toISOString();
if(query[1].length)
data.next = query[1][0].id;
if(query[2].length)
data.prev = query[2][0].id;
}
res.reply({
body: swig.compile(templates.item)(data)
});
});
router.get(/^\/(contact|help|how)$/, (req, res) => {
res.reply({
body: templates[req.url.split[0]]
});
});
router.get("/random", async (req, res) => {
res
.writeHead(301, {
"Cache-Control": "no-cache, public",
"Location": "/" + (await sql.query("select id from items order by rand() limit 1"))[0].id
})
.end();
});

17
src/inc/routes/static.mjs Normal file
View File

@ -0,0 +1,17 @@
import path from "path";
import router from "../router.mjs";
router.static({
dir: path.resolve() + "/public/b",
route: /^\/b\//
});
router.static({
dir: path.resolve() + "/public/s",
route: /^\/s\//
});
router.static({
dir: path.resolve() + "/public/t",
route: /^\/t\//
});