new backend

This commit is contained in:
Flummi
2021-12-04 12:19:47 +01:00
parent d885dd8e4e
commit 43665884f6
42 changed files with 946 additions and 1226 deletions

View File

@@ -1,4 +1,8 @@
import crypto from "crypto";
import util from "util";
import sql from "./sql.mjs";
const scrypt = util.promisify(crypto.scrypt);
const epochs = [
["year", 31536000],
@@ -21,15 +25,63 @@ const getDuration = timeAgoInSeconds => {
export default new class {
formatSize(size, i = ~~(Math.log(size) / Math.log(1024))) {
return (size / Math.pow(1024, i)).toFixed(2) * 1 + " " + ["B", "kB", "MB", "GB", "TB"][i];
}
};
calcSpeed(b, s) {
return (Math.round((b * 8 / s / 1e6) * 1e4) / 1e4);
}
};
timeAgo(date) {
const { interval, epoch } = getDuration(~~((new Date() - new Date(date)) / 1e3));
return `${interval} ${epoch}${interval === 1 ? "" : "s"} ago`;
}
};
md5(str) {
return crypto.createHash('md5').update(str).digest("hex");
}
};
getMode(mode) {
let tmp;
switch(mode) {
case 1: // nsfw
tmp = "id in (select item_id from tags_assign where tag_id = 2 group by item_id)";
break;
case 2: // untagged
tmp = "id not in (select item_id from tags_assign group by item_id)";
break;
case 3: // all
tmp = "";
break;
default: // sfw
tmp = "id in (select item_id from tags_assign where tag_id = 1 group by item_id)";
break;
}
return tmp;
};
createID() {
return crypto.randomBytes(16).toString("hex") + Date.now().toString(24);
};
// async funcs
async countf0cks() {
const tagged = (await sql("items").whereRaw("id in (select item_id from tags_assign group by item_id)").count("* as total"))[0].total;
const untagged = (await sql("items").whereRaw("id not in (select item_id from tags_assign group by item_id)").count("* as total"))[0].total;
const sfw = (await sql("items").whereRaw("id in (select item_id from tags_assign where tag_id = 1 group by item_id)").count("* as total"))[0].total;
const nsfw = (await sql("items").whereRaw("id in (select item_id from tags_assign where tag_id = 2 group by item_id)").count("* as total"))[0].total;
return {
tagged,
untagged,
total: tagged + untagged,
sfw,
nsfw
};
};
async hash(str) {
const salt = crypto.randomBytes(16).toString("hex");
const derivedKey = await scrypt(str, salt, 64);
return "$f0ck$" + salt + ":" + derivedKey.toString("hex");
};
async verify(str, hash) {
const [ salt, key ] = hash.substring(6).split(":");
const keyBuffer = Buffer.from(key, "hex");
const derivedKey = await scrypt(str, salt, 64);
return crypto.timingSafeEqual(keyBuffer, derivedKey);
};
};