From 5d5d6a0f4701bf48cb387d86843e6ab310dd9f4b Mon Sep 17 00:00:00 2001 From: supositware Date: Mon, 20 Jun 2022 08:34:18 +0200 Subject: [PATCH] Fixed some very stupid mistake with child_process.exec --- commands/utility/download.js | 14 +++++++------- commands/utility/vid2gif.js | 11 ++++++++--- utils/videos.js | 15 +++++++++++++-- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/commands/utility/download.js b/commands/utility/download.js index 0230819..8592e18 100644 --- a/commands/utility/download.js +++ b/commands/utility/download.js @@ -11,7 +11,7 @@ export default { .setDescription('Download a video.') .addStringOption(option => option.setName('url') - .setDescription('URL of the video you want to download.') + .setDescription('url of the video you want to download.') .setRequired(true)) .addBooleanOption(option => option.setName('advanced') @@ -22,9 +22,9 @@ export default { await interaction.deferReply({ ephemeral: false }); const url = interaction.options.getString('url'); - const urlRE = new RegExp('([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?([^ ])+'); - if (!url.match(urlRE)) { - return interaction.editReply({ content: '❌ This does not look like a valid URL!', ephemeral: true }); + if (!await utils.stringIsAValidurl(url)) { + console.error(`Not a url!!! ${url}`); + return interaction.editReply({ content: '❌ This does not look like a valid url!', ephemeral: true }); } if (interaction.options.getBoolean('advanced')) { @@ -92,7 +92,7 @@ async function download(url, interaction) { let format = 'bestvideo*+bestaudio/best'; const Embed = new MessageEmbed() .setColor(interaction.member ? interaction.member.displayHexColor : 'NAVY') - .setAuthor({ name: `Downloaded by ${interaction.user.tag}`, iconURL: interaction.user.avatarURL(), url: url }) + .setAuthor({ name: `Downloaded by ${interaction.user.tag}`, iconurl: interaction.user.avatarURL(), url: url }) .setFooter({ text: `You can get the original video by clicking on the "Downloaded by ${interaction.user.tag}" message!` }); if (interaction.customId === 'downloadQuality') { @@ -113,12 +113,12 @@ async function download(url, interaction) { await interaction.followUp('Uh oh! The video you tried to download is too big!', { ephemeral: true }); } else if (fileSize > 8) { - const fileURL = await utils.upload(output) + const fileurl = await utils.upload(output) .catch(err => { console.error(err); }); await interaction.editReply({ content: 'File was bigger than 8 mb. It has been uploaded to an external site.', embeds: [Embed], ephemeral: false }); - await interaction.followUp({ content: fileURL, ephemeral: false }); + await interaction.followUp({ content: fileurl, ephemeral: false }); } else { await interaction.editReply({ embeds: [Embed], files: [output], ephemeral: false }); diff --git a/commands/utility/vid2gif.js b/commands/utility/vid2gif.js index 62ec70c..f6e5822 100644 --- a/commands/utility/vid2gif.js +++ b/commands/utility/vid2gif.js @@ -18,6 +18,11 @@ export default { await interaction.deferReply({ ephemeral: false }); const url = interaction.options.getString('url'); + if (!await utils.stringIsAValidurl(url)) { + console.error(`Not a url!!! ${url}`); + return interaction.editReply({ content: '❌ This does not look like a valid url!', ephemeral: true }); + } + utils.downloadVideo(url, interaction.id) .then(async () => { const file = fs.readdirSync(os.tmpdir()).filter(fn => fn.startsWith(interaction.id)); @@ -55,7 +60,7 @@ export default { async function gifski(output, input) { return await new Promise((resolve, reject) => { - exec(`gifski -o ${output} ${input}`, (err, stdout, stderr) => { + exec(`gifski --quality 70 -o ${output} ${input}`, (err, stdout, stderr) => { if (err) { reject(stderr); } @@ -69,7 +74,7 @@ async function gifski(output, input) { async function gifsicle(input, output) { return await new Promise((resolve, reject) => { - exec(`gifsicle --scale 0.5 -O3 -i ${input} -o ${output}`, (err, stdout, stderr) => { + exec(`gifsicle --colors 256 -i ${input} -o ${output}`, (err, stdout, stderr) => { if (err) { reject(stderr); } @@ -79,4 +84,4 @@ async function gifsicle(input, output) { resolve(stdout); }); }); -} \ No newline at end of file +} diff --git a/utils/videos.js b/utils/videos.js index 4b810bf..5723334 100644 --- a/utils/videos.js +++ b/utils/videos.js @@ -5,10 +5,11 @@ export default { downloadVideo, upload, ffmpeg, + stringIsAValidurl, }; -async function downloadVideo(url, output, format = 'bestvideo*+bestaudio/best') { +async function downloadVideo(urlArg, output, format = 'bestvideo*+bestaudio/best') { await new Promise((resolve, reject) => { - exec(`./bin/yt-dlp -f ${format} ${url} -o "${os.tmpdir()}/${output}.%(ext)s" --force-overwrites`, (err, stdout, stderr) => { + exec(`./bin/yt-dlp -f ${format} ${urlArg} -o "${os.tmpdir()}/${output}.%(ext)s" --force-overwrites`, (err, stdout, stderr) => { if (err) { reject(stderr); } @@ -46,4 +47,14 @@ async function ffmpeg(command) { resolve(stdout); }); }); +} + +async function stringIsAValidurl(s) { + try { + new URL(s); + return true; + } + catch (err) { + return false; + } } \ No newline at end of file