114 lines
No EOL
3.6 KiB
JavaScript
114 lines
No EOL
3.6 KiB
JavaScript
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)
|
|
}
|
|
}
|
|
|
|
function apiStat(res, next, message, success = false, redirect = '/') {
|
|
res.api = { success, message, redirect };
|
|
next();
|
|
}
|
|
|
|
async function splitUp(content) {
|
|
let out = content.split(/(\s+)/g);
|
|
|
|
for (let i in out) {
|
|
let res = out[i];
|
|
if (res.startsWith('file::')) {
|
|
let ext = res.split('.').pop();
|
|
let content = '';
|
|
let path = new URL(res.slice('file::'.length), 'https://tbg.dervland.net/').pathname;
|
|
if (ext == 'txt' || ext == 'json') {
|
|
let san = sanitize(path.split('/').pop(), '');
|
|
content = await readFile(`${process.cwd()}/uploads/${san}`, 'utf-8');
|
|
}
|
|
|
|
let contentJson = {};
|
|
let width = 0;
|
|
let height = 0;
|
|
|
|
try {
|
|
contentJson = JSON.parse(content)
|
|
} catch (err) {
|
|
contentJson = {};
|
|
}
|
|
|
|
if (contentJson.pal) {
|
|
ext = '!neoboxels';
|
|
console.log(Object.keys(contentJson))
|
|
width = contentJson.width * 4;
|
|
height = contentJson.height *4;
|
|
}
|
|
|
|
out[i] = { type: 'file', path, ext, content, width, height };
|
|
continue;
|
|
}
|
|
if (res.startsWith('https://')) {
|
|
out[i] = { type: 'link', name: res, href: res };
|
|
continue;
|
|
}
|
|
let href = "";
|
|
if (res[0] == '@' || res[0] == '#') {
|
|
let type = res[0] == '@' ? '/users/' : '/walls/get/hub/';
|
|
let type2 = res[0] == '@' ? '' : '/0';
|
|
href = new URL(type + res.slice(1) + type2, 'https://tbg.dervland.net/').pathname
|
|
}
|
|
if (res[0] == '@') {
|
|
out[i] = { type: 'link', name: res, href };
|
|
continue;
|
|
}
|
|
if (res[0] == '#') {
|
|
out[i] = { type: 'link', name: res, href };
|
|
continue;
|
|
}
|
|
out[i] = {type: 'normal', data: res};
|
|
}
|
|
return out;
|
|
}
|
|
|
|
async function replyIterator(replies, db) {
|
|
for (let reply of replies) {
|
|
if (reply.childType == 'hub') {
|
|
reply.comment = { type: reply.childType, id: reply.childId, content: `The ${reply.childId} hub.`, username: false, date: undefined };
|
|
continue;
|
|
} else if (reply.childType == 'users') {
|
|
reply.comment = { type: reply.childType, id: reply.childId, content: `User ${reply.childId}'s wall.`, username: false, date: undefined };
|
|
continue;
|
|
}
|
|
|
|
let dat = await db.all('SELECT * FROM comment WHERE id = ?', [
|
|
reply.childId
|
|
]);
|
|
|
|
if (reply.childType != 'comment' || dat.length < 1) {
|
|
reply.comment = 'fail';
|
|
continue;
|
|
}
|
|
reply.comment = dat[0];
|
|
reply.comment.type = reply.childType;
|
|
//reply.comment.id = reply.childId;
|
|
}
|
|
for (let reply of replies) {
|
|
reply.comment.content = await splitUp(reply.comment.content)
|
|
}
|
|
return replies;
|
|
}
|
|
|
|
export { importRouters, apiStat, replyIterator, splitUp }; |