This commit is contained in:
Flummi
2020-04-09 18:44:09 +02:00
parent 46082780e1
commit 97096357e5
17 changed files with 39 additions and 8829 deletions

View File

@ -2,4 +2,7 @@ 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).toFixed(2)} Mbit/s`;
}
};

View File

@ -84,23 +84,15 @@ router.get(/^\/api\/p\/([0-9]+)/, async (req, res) => { // legacy
router.get(/^\/api\/item\/[0-9]+$/, async (req, res) => {
try {
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`,
srchost: parse(rows[0][0].src).hostname,
next: rows[1].length ? rows[1][0].id : null,
prev: rows[2].length ? rows[2][0].id : null,
}
} : {
error: true
};
res
.writeHead(200, { "Content-Type": "application/json" })
.end(JSON.stringify(data), "utf-8");
res.reply({
type: "application/json",
body: JSON.stringify(rows?.shift() || [])
});
} catch(err) {
res
.writeHead(500)
.end(JSON.stringify(err), "utf-8");
res.reply({
code: 500,
body: JSON.stringify(err)
});
}
});
@ -109,13 +101,14 @@ router.get(/^\/api\/user\/.*(\/[0-9]+)?$/, async (req, res) => { // auf qs umste
const eps = Math.min(req.url.split[3] || 50, 50);
try {
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");
res.reply({
type: "application/json",
body: JSON.stringify(rows.length > 0 ? rows : [])
});
} catch(err) {
res
.writeHead(500)
.end(JSON.stringify(err), "utf-8");
res.reply({
code: 500,
body: JSON.stringify(err)
});
}
db.end();
});

View File

@ -9,9 +9,10 @@ export const queries = {
main: "select id, mime, size, username, userchannel, usernetwork, stamp, dest, src from items ",
where: where => `${where?`where ${where}`:""} order by rand() limit 1`
},
item: "select id, mime, dest, size, src, stamp, userchannel, username, usernetwork from items where id = ? limit 1;"
+ "select id from items where id = (select min(id) from items where id > ?);"
+ "select id from items where id = (select max(id) from items where id < ?)",
item: "select id, mime, dest, size, src, stamp, userchannel, username, usernetwork, "
+ "(select id from items where id = (select min(id) from items where id > ?)) as next, "
+ "(select id from items where id = (select max(id) from items where id < ?)) as prev "
+ "from items where items.id = ? limit 1",
p: (trend, order) => `select id, mime from items where id ${trend} ? order by id ${order} limit ?`,
user: "select id, mime, size, src, stamp, userchannel, username, usernetwork from items where username = ? limit ?"
};

View File

@ -10,7 +10,7 @@ export default new class {
[ "elseif", t => `}else if(${t.slice(6).trim()}){` ],
[ "else", () => "}else{" ],
[ "/if", () => "}" ],
[ "include", (t, data) => `html+='${this.render(t.slice(7).trim(), data)}';` ], //`html+=util.include("${t.slice(7).trim()}", data);`], // parse them aswell
[ "include", (t, data) => `html+='${this.render(t.slice(7).trim(), data)}';` ],
[ "=", t => `html+=${t.slice(1).trim()};` ]
];
readdir(dir, root = dir, rel = dir.replace(`${root}/`, "")) {

View File

@ -35,7 +35,7 @@ export default async bot => {
e.reply(`${tmp.length}, ${rows.length}, ${dir.length}`);
break;
default:
return e.reply("lul");
return;
}
}
}]

View File

@ -1,5 +1,6 @@
import cfg from "../../../config.json";
import sql from "../sql.mjs";
import lib from "../lib.mjs";
import fs from "fs";
import { exec as _exec } from "child_process";
@ -8,6 +9,10 @@ const exec = promisify(_exec);
const regex = /https?:\/\/[\w\S(\.|:|/)]+/gi;
console.log(
lib.calcSpeed(123456, 56432)
);
export default async bot => {
return [{
@ -40,9 +45,11 @@ export default async bot => {
const filename = `${uuid}.${meta.ext}`;
// download data
const source = (await exec(`youtube-dl "${link}" --max-filesize 100m -o ./tmp/${filename}`)).stdout.trim();
const start = new Date();
const source = (await exec(`youtube-dl "${link}" --max-filesize 10000m -o ./tmp/${filename}`)).stdout.trim();
if(source.match(/larger than/))
return e.reply("too large lol");
const end = ~~((new Date() - start) / 1e3);
// generate checksum
const checksum = (await exec(`sha256sum ./tmp/${filename}`)).stdout.trim().split(" ")[0];
@ -81,8 +88,8 @@ export default async bot => {
e.reply([
`title: ${meta.fulltitle}`,
`name: ${filename}`,
`size: ${size}`,
`size: ${lib.formatSize(size)}`,
`speed: ${lib.calcSpeed(size, end)}`,
`link: ${cfg.main.url}/${insertq.insertId}`
]);