This commit is contained in:
Flummi
2020-04-05 18:47:09 +02:00
parent a202ae2e69
commit 29329702da
10 changed files with 188 additions and 234 deletions

View File

@ -1,9 +1,10 @@
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";
import sql from "../sql.mjs";
import lib from "../lib.mjs";
import tpl from "../tpl.mjs";
const templates = {
contact: fs.readFileSync("./views/contact.html", "utf-8"),
@ -21,7 +22,7 @@ router.get("/", async (req, res) => {
};
res.reply({
body: swig.compile(templates.index)(data)
body: tpl.render(templates.index, data)
});
});
@ -74,7 +75,7 @@ router.get(/^\/([0-9]+)$/, async (req, res) => {
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.size = lib.formatSize(e.size);
data.userchannel = e.userchannel;
data.usernetwork = e.usernetwork;
data.timestamp = new Date(e.stamp * 1000).toISOString();
@ -84,7 +85,7 @@ router.get(/^\/([0-9]+)$/, async (req, res) => {
data.prev = query[2][0].id;
}
res.reply({
body: swig.compile(templates.item)(data)
body: tpl.render(templates.item, data)
});
});

30
src/inc/tpl.mjs Normal file
View File

@ -0,0 +1,30 @@
export default new class {
syntax = [
[ "each", t => `util.forEach(${t.slice(4).trim()},($value,$key)=>{` ],
[ "/each", () => "});" ],
[ "if", t => `if(${t.slice(2).trim()}){` ],
[ "elseif", t => `}else if(${t.slice(6).trim()}){` ],
[ "else", () => "}else{" ],
[ "/if", () => "}" ],
[ "=", t => `html+=${t.slice(1).trim()};` ]
];
forEach(o, f) {
if(Array.isArray(o))
o.forEach(f);
else if(typeof o === "object")
Object.keys(o).forEach(k => f.call(null, o[k], k));
else
throw new Error(`${o} is not a iterable object`);
}
render(tpl, data) {
return new Function("util", "data", "let html = \"\";with(data){"
+ tpl.trim().replace(/[\n\r]/g, "").split(/{{\s*([^}]+)\s*}}/).filter(Boolean).map(t => {
for(let i = 0; i < this.syntax.length; i++)
if(t.indexOf(this.syntax[i][0]) === 0)
return this.syntax[i][1](t);
return `html+='${t}';`;
})
.join`` + "}return html.trim().replace(/>[\\n\\r\\s]*?</g, '><')")
.bind(null, { forEach: this.forEach })(data);
}
};

View File

@ -18,12 +18,22 @@ export default async bot => {
t: await fs.readdir("./public/t")
};
const sizes = {
b: (await Promise.all(dirs.b.map(async file => (await fs.stat(`./public/b/${file}`)).size))).reduce((a, b) => b + a),
t: (await Promise.all(dirs.t.map(async file => (await fs.stat(`./public/t/${file}`)).size))).reduce((a, b) => b + a),
b: lib.formatSize((await Promise.all(dirs.b.map(async file => (await fs.stat(`./public/b/${file}`)).size))).reduce((a, b) => b + a)),
t: lib.formatSize((await Promise.all(dirs.t.map(async file => (await fs.stat(`./public/t/${file}`)).size))).reduce((a, b) => b + a)),
};
return e.reply(`${dirs.b.length} f0cks: ${sizes.b}, ${dirs.t.length} thumbnails: ${sizes.t}`);
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 dir = (await fs.readdir("./public/t")).filter(d => d.endsWith(".png")).map(e => +e.split(".")[0]);
const tmp = [];
for(let row of rows) {
!dir.includes(row.id) ? tmp.push(row.id) : null;
}
e.reply(`${tmp.length}, ${rows.length}, ${dir.length}`);
break;
default:
return e.reply("lul");
}