add embeds support

This commit is contained in:
biglyderv 2025-05-26 18:06:31 -04:00
parent e86d4f4212
commit 21476e6e43
Signed by: biglyderv
GPG key ID: 0E2EB0B4CD7397B5
3 changed files with 63 additions and 6 deletions

49
lib.js
View file

@ -1,3 +1,20 @@
import { readFile } from "fs/promises";
var illegalRe = /[\/\?<>\\:\*\|":]/g;
var controlRe = /[\x00-\x1f\x80-\x9f]/g;
var reservedRe = /^\.+$/;
var windowsReservedRe = /^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$/i;
function sanitize(input, replacement) {
var sanitized = input
.replace(illegalRe, replacement)
.replace(controlRe, replacement)
.replace(reservedRe, replacement)
.replace(windowsReservedRe, replacement);
return sanitized.slice(0,200);
}
async function importRouters(app, routers) {
for (let router in routers) {
await app.use(router, (await import(routers[router])).default)
@ -9,17 +26,37 @@ function apiStat(res, next, message, success = false, redirect = '/') {
next();
}
function splitUp(content) {
async function splitUp(content) {
let out = content.split(/(\s+)/g);
for (let i in out) {
let res = out[i];
if (res.startsWith('file::')) {
out[i] = ['file', new URL(res.slice('file::'.length), 'https://tbg.dervland.net/').pathname , res.split('.').pop()];
let type = res.split('.').pop();
let inner = '';
let path = new URL(res.slice('file::'.length), 'https://tbg.dervland.net/').pathname;
if (type == 'txt' || type == 'json') {
let san = sanitize(path.split('/').pop(), '');
inner = await readFile(`${process.cwd()}/uploads/${san}`, 'utf-8');
}
let innerJson = {};
try {
innerJson = JSON.parse(inner)
} catch (err) {
innerJson = {};
}
if (innerJson.pal) {
type = '!neoboxels'
}
out[i] = ['file', path, type, inner];
continue;
}
if (res.startsWith('https://')) {
out[i] = ['link',res];
out[i] = ['link', res];
continue;
}
let urlThing = "";
@ -29,11 +66,11 @@ function splitUp(content) {
urlThing = new URL(type + res.slice(1) + type2, 'https://tbg.dervland.net/').pathname
}
if (res[0] == '@') {
out[i] = ['curl',res, urlThing];
out[i] = ['curl', res, urlThing];
continue;
}
if (res[0] == '#') {
out[i] = ['curl',res, urlThing];
out[i] = ['curl', res, urlThing];
continue;
}
out[i] = [res];
@ -64,7 +101,7 @@ async function replyIterator(replies, db) {
//reply.comment.id = reply.childId;
}
for (let reply of replies) {
reply.comment.content = splitUp(reply.comment.content)
reply.comment.content = await splitUp(reply.comment.content)
}
return replies;
}