add url invalid message

This commit is contained in:
=
2020-09-03 22:07:53 -03:00
parent 6fc1373db4
commit f89b27bd69
2 changed files with 31 additions and 9 deletions

View File

@@ -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);

View File

@@ -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');
}
}