From f89b27bd692bbd62bea27dbf95cf966128aa31fe Mon Sep 17 00:00:00 2001 From: = Date: Thu, 3 Sep 2020 22:07:53 -0300 Subject: [PATCH] add url invalid message --- Server/index.js | 12 +++++++++--- script.js | 28 ++++++++++++++++++++++------ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/Server/index.js b/Server/index.js index a801890..1b59be3 100644 --- a/Server/index.js +++ b/Server/index.js @@ -13,6 +13,9 @@ app.listen(PORT, () => { app.get('/downloadmp3', async (req, res, next) => { try { var url = req.query.url; + if(!ytdl.validateURL(url)) { + return res.sendStatus(400); + } let title = 'audio'; await ytdl.getBasicInfo(url, { @@ -35,17 +38,20 @@ app.get('/downloadmp3', async (req, res, next) => { app.get('/downloadmp4', async (req, res, next) => { try { - let URL = req.query.url; + let url = req.query.url; + if(!ytdl.validateURL(url)) { + return res.sendStatus(400); + } let title = 'video'; - await ytdl.getBasicInfo(URL, { + await ytdl.getBasicInfo(url, { format: 'mp4' }, (err, info) => { title = info.player_response.videoDetails.title.replace(/[^\x00-\x7F]/g, ""); }); res.header('Content-Disposition', `attachment; filename="${title}.mp4"`); - ytdl(URL, { + ytdl(url, { format: 'mp4', }).pipe(res); diff --git a/script.js b/script.js index 4d9610c..c593f3b 100644 --- a/script.js +++ b/script.js @@ -8,17 +8,33 @@ Btn.addEventListener('click', () => { alert('Enter YouTube URL'); } else { if (select.value == 'mp3') { - redirectMp3(URLinput.value); + downloadMp3(URLinput.value); } else if (select.value == 'mp4') { - redirectMp4(URLinput.value); + downloadMp4(URLinput.value); } } }); -function redirectMp3(query) { - window.location.href = `${serverURL}/downloadmp3?url=${query}`; +async function downloadMp3(query) { + const res = await fetch(`${serverURL}/downloadmp3?url=${query}`); + if(res.status == 200) { + var a = document.createElement('a'); + a.href = `${serverURL}/downloadmp3?url=${query}`; + a.setAttribute('download', ''); + a.click(); + } else if(res.status == 400) { + alert("Invalid url"); + } } -function redirectMp4(query) { - window.location.href = `${serverURL}/downloadmp4?url=${query}`; +async function downloadMp4(query) { + const res = await fetch(`${serverURL}/downloadmp4?url=${query}`); + if(res.status == 200) { + var a = document.createElement('a'); + a.href = `${serverURL}/downloadmp4?url=${query}`; + a.setAttribute('download', ''); + a.click(); + } else if(res.status == 400) { + alert('Invalid url'); + } } \ No newline at end of file