From a6d1adf657ff8ef5fb1103fd34f9a26a47c94b97 Mon Sep 17 00:00:00 2001 From: loicbersier Date: Mon, 25 Nov 2019 23:41:44 +0100 Subject: [PATCH] Check for file size --- commands/general/tweet.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/commands/general/tweet.js b/commands/general/tweet.js index 9fdcdf72..4e658ae3 100644 --- a/commands/general/tweet.js +++ b/commands/general/tweet.js @@ -98,6 +98,15 @@ class tweetCommand extends Command { const dest = fs.createWriteStream(`${os.tmpdir()}/${Attachment[0].name}`); res.body.pipe(dest); dest.on('finish', () => { + let file = fs.statSync(`${os.tmpdir()}/${Attachment[0].name}`); + let fileSize = file.size / 1000000.0; + + if ((Attachment[0].name.endsWith('.jpg') || Attachment[0].name.endsWith('.png')) && fileSize > 5) { + return message.channel.send('Images can\'t be larger than 5 MB!'); + } else if (Attachment[0].name.endsWith('.gif') && fileSize > 15) { + return message.channel.send('Gifs can\'t be larger than 15 MB!'); + } + let b64Image = fs.readFileSync(`${os.tmpdir()}/${Attachment[0].name}`, { encoding: 'base64'}); T.post('media/upload', { media_data: b64Image }, function (err, data) { if (err) {