From 2c832f2014580106699430a45089619d2f0859be Mon Sep 17 00:00:00 2001 From: Flummi Date: Mon, 3 Sep 2018 18:20:27 +0200 Subject: [PATCH] parser die Zweite --- src/inc/trigger/parser_new.mjs | 44 ++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/src/inc/trigger/parser_new.mjs b/src/inc/trigger/parser_new.mjs index 520e71f..e09bed1 100644 --- a/src/inc/trigger/parser_new.mjs +++ b/src/inc/trigger/parser_new.mjs @@ -40,7 +40,18 @@ export default bot => { link: data.webpage_url, file: data.id, format: data.formats - .filter(f => (f.filesize <= cfg.main.maxFileSize.val || f.ext === "mp3") && ["webm","mp4","mp3"].includes(f.ext)) + .filter(f => { + if(f.hasOwnProperty("fragments")) + return false; + if(!["webm", "mp4", "mp3"].includes(f.ext)) + return false; + if(f.filesize > cfg.main.maxFileSize.val && f.ext !== "mp3") + return false; + if(/playlist/i.test(f.url)) + return false; + //e.reply(`${f.ext} - ${f.url} - ${f.format}`); + return f; + }) .splice(-1, 1) .map(f => ({ ext: f.ext, @@ -56,14 +67,39 @@ export default bot => { fetch(data.format[0].url, { size: cfg.main.maxFileSize.val }) .then(res => { const uuid = lib.getUUID(); - const dest = fs.createWriteStream(b + "/" + uuid + "." + data.format[0].ext); + const file = `${b}/${uuid}.${data.format[0].ext}`; + const dest = fs.createWriteStream(file); res.body.pipe(dest); - e.reply(`gef0ckt lol: ${data.file} -> ${uuid}.${data.format[0].ext}, ~${data.format[0].size}`); + let t; + dest.on("open", () => { + e.reply(`downloading ${data.file}...`); + t = setInterval(() => { + const size = fs.statSync(file).size; + if(size >= cfg.main.maxFileSize.val) { + e.reply(`file too large lol! D: (${lib.formatSize(size)})`); + res.body.unpipe(dest); + dest.destroy(); + fs.unlinkSync(file); + clearInterval(t); + t = false; + } + }, 2000); + }); + dest.on("close", blah => { + if(t) { + clearInterval(t); + e.reply(`gef0ckt lol: ${data.file} -> ${uuid}.${data.format[0].ext}, ~${lib.formatSize(fs.statSync(file).size)}`); + } + else { + // recursive + } + }); }) .catch(err => { console.error(err); }); - })); + }).catch(err => e.reply(JSON.stringify(err))) + ); } })); };