diff --git a/app/Controllers/Http/DownloadController.js b/app/Controllers/Http/DownloadController.js index 6fb46c6..b856975 100644 --- a/app/Controllers/Http/DownloadController.js +++ b/app/Controllers/Http/DownloadController.js @@ -100,12 +100,6 @@ class DownloadController { return; } - // Youtube-dl quality settings - if (data.quality == 'small') - option = 'worst' - else - option = 'best' - // If alt download ( Quality settings and file format option doesn't work here ) if (data.alt) { let altFolder; @@ -133,86 +127,83 @@ class DownloadController { if (ws) { ws.socket.emit('end', altFolder.slice(17)); } - return; }); } else { if (data.url.match( /^.*(youtu.be\/|list=)([^#\&\?]*).*/)) { - playlistDownload(data) + playlistDownload(data); } else { - // Download as mp4 if possible - let video = youtubedl(data.url, ['--format=mp4', '-f', option]); - - video.on('error', function(err) { - console.error(err); - if (ws) { - ws.socket.emit('error', err.toString()); - } - return; - }) - - let ext; - let size = 0 - video.on('info', function(info) { - size = info.size - // Set file name - ext = info.ext; - let title = info.title.slice(0,50); - DLFile = `${title.replace(/\s/g, '_')}.${ext}`; - DLFile = DLFile.replace(/[()]|[/]|[\\]|[?]|[!]/g, '_'); - - // If no title use the ID - if (title == '_') title = `_${info.id}`; - // If user want to hide from the feed - if (data.feed == 'on') - DLFile = `hidden/${title}.${ext}`; - - video.pipe(fs.createWriteStream(`./public/uploads/${DLFile}`)); - }); - - let pos = 0 - video.on('data', function data(chunk) { - pos += chunk.length - // `size` should not be 0 here. - if (size) { - let percent = (pos / size * 100).toFixed(2) - if (ws) { - ws.socket.emit('progress', percent); - } - } - }) - - video.on('end', function() { - console.log('end'); - if (ws) { - ws.socket.emit('message', 'end'); - } - if (data.format == 'mp4' || data.format == 'webm') { - // If user requested mp4 directly attach the file - if (ws) { - ws.socket.emit('end', DLFile); - } - return; - } else { - // If user requested an audio format, convert it - ffmpeg(`./public/uploads/${DLFile}`) - .noVideo() - .audioChannels('2') - .audioFrequency('44100') - .audioBitrate('320k') - .format(data.format) - .save(`./public/uploads/${DLFile.replace(`.${ext}`, `.${data.format}`)}`) - .on('progress', (progress) => { - wb.broadcast(progress.percent) - }) - .on('end', () => { - fs.unlinkSync(`./public/uploads/${DLFile}`); - if (ws) { - ws.socket.emit('end', DLFile.replace(`.${ext}`, `.${data.format}`)); - } - }); - } - }); + videoDownload(data); } + + } + + function videoDownload(data) { + // Download as mp4 if possible + let video = youtubedl(data.url, ['-f', data.quality]); + + video.on('error', function(err) { + console.error(err); + if (ws) { + ws.socket.emit('error', err.toString()); + } + }) + + let ext; + let size = 0 + video.on('info', function(info) { + size = info.size + // Set file name + ext = info.ext; + let title = info.title.slice(0,50); + DLFile = `${title.replace(/\s/g, '_')}.${ext}`; + DLFile = DLFile.replace(/[()]|[/]|[\\]|[?]|[!]|[#]/g, '_'); + + // If no title use the ID + if (title == '_') title = `_${info.id}`; + // If user want to hide from the feed + if (data.feed == 'on') + DLFile = `hidden/${DLFile}`; + + video.pipe(fs.createWriteStream(`./public/uploads/${DLFile}`)); + }); + + let pos = 0 + video.on('data', function data(chunk) { + pos += chunk.length + // `size` should not be 0 here. + if (size) { + let percent = (pos / size * 100).toFixed(2) + if (ws) { + ws.socket.emit('progress', percent); + } + } + }) + + video.on('end', function() { + if (data.format == 'mp3' || data.format == 'flac') { + // If user requested an audio format, convert it + ffmpeg(`./public/uploads/${DLFile}`) + .noVideo() + .audioChannels('2') + .audioFrequency('44100') + .audioBitrate('320k') + .format(data.format) + .save(`./public/uploads/${DLFile.replace(`.${ext}`, `.${data.format}`)}`) + .on('progress', (progress) => { + ws.socket.emit(progress.percent) + }) + .on('end', () => { + fs.unlinkSync(`./public/uploads/${DLFile}`); + if (ws) { + ws.socket.emit('end', `./public/uploads/${DLFile.replace(`.${ext}`, `.${data.format}`)}`); + } + }); + } else { + if (ws) { + ws.socket.emit('end', `./public/uploads/${DLFile}`); + } + } + }); } function playlistDownload(data) { @@ -223,7 +214,6 @@ class DownloadController { if (ws) { ws.socket.emit('error', err.toString()); } - return; }); let ext; @@ -235,15 +225,16 @@ class DownloadController { ext = info.ext; let title = info.title.slice(0,50); DLFile = `${title.replace(/\s/g, '_')}.${ext}`; - DLFile = DLFile.replace(/[()]|[/]|[\\]|[?]|[!]/g, '_'); + DLFile = DLFile.replace(/[()]|[/]|[\\]|[?]|[!]|[#]/g, '_'); // If no title use the ID if (title == '_') title = `_${info.id}`; // If user want to hide from the feed if (data.feed == 'on') - DLFile = `hidden/playlist/${title}.${ext}`; + DLFile = `hidden/playlist/${DLFile}`; - video.pipe(fs.createWriteStream(`./public/uploads/playlist/${DLFile}`)); + + video.pipe(fs.createWriteStream(`./public/uploads/playlist/${DLFile}`)); }); diff --git a/resources/views/index.edge b/resources/views/index.edge index 74ca6dc..08813ab 100644 --- a/resources/views/index.edge +++ b/resources/views/index.edge @@ -65,12 +65,12 @@ <div class="field is-horizontal"> <div class="control"> <label class="radio" for="small"> - <input class="radio" type="radio" name="quality" id="small" value="small"> + <input class="radio" type="radio" name="quality" id="small" value="worst"> {{ antl.formatMessage('messages.LQ') }} </label> <label class="radio" for="high"> - <input class="radio" type="radio" name="quality" id="high" value="high" checked> + <input class="radio" type="radio" name="quality" id="high" value="best" checked> {{ antl.formatMessage('messages.HQ') }} </label> @@ -221,77 +221,8 @@ <script src="https://unpkg.com/@adonisjs/websocket-client"></script> <script> const ws = adonis.Ws(); - - function submitDownload() { - document.getElementById('msg').innerHTML = '<div class="notification is-success fadein" id="notif"></button>{{ antl.formatMessage('messages.dlStart') }}</div>'; - setTimeout(() => { - fadeout('notif') - }, 2000); - - FD = new FormData(document.getElementsByName('download-form')[0]); - - fetch('/', { - method: 'POST', - body: FD - }); - - document.getElementById('progression').innerHTML = '<progress class="progress is-primary" max="100" id="progress">0%</progress>' - } - - function fadeout(id) { - document.getElementById(id).classList.add('fadeout'); - setTimeout(() => { - let element = document.getElementById(id); - element.parentNode.removeChild(element); - }, 2000); - } - - function toClipboard(text) { - navigator.clipboard.writeText(text) - .catch(err => { - console.error(err); - document.getElementById('msg').innerHTML = '<div class="notification is-error fadein" id="notif">{{ antl.formatMessage('messages.errorCopy') }}</div>'; - setTimeout(() => { - fadeout('notif') - }, 5000); - }); - document.getElementById('msg').innerHTML = '<div class="notification is-success fadein" id="notif">{{ antl.formatMessage('messages.successCopy') }}</div>'; - setTimeout(() => { - fadeout('notif') - }, 5000); - } - - // If alt download block other settings since they don't work anyway - document.getElementById('alt').onclick = function() { - if(document.getElementById('alt').checked) { - document.getElementById('small').disabled = true; - document.getElementById('small').checked = false; - - document.getElementById('mp3').disabled = true; - document.getElementById('mp3').checked = false; - document.getElementById('flac').disabled = true; - document.getElementById('flac').checked = false; - document.getElementById('mp4').checked = true; - document.getElementById('high').checked = true; - } else { - document.getElementById('small').disabled = false; - document.getElementById('mp3').disabled = false; - document.getElementById('flac').disabled = false; - } - } - - // If user press enter do same thing as if pressing the button - let input = document.getElementById("URL"); - input.addEventListener("keyup", function(event) { - if (event.keyCode === 13) { - event.preventDefault(); - document.getElementById("button").click(); - } - }); - - ws.connect(); - let channel = ws.subscribe('progress') + let channel = ws.subscribe('progress'); ws.on('open', () => { console.log('is open'); @@ -315,12 +246,84 @@ }); channel.on('end', (message) => { + let frm = document.getElementsByName('download-form')[0]; + frm.reset(); + link = document.createElement("a"); - link.setAttribute("href", message); //replace "file" with link to file you want to download - link.setAttribute("download", message);// replace "file" here too + link.setAttribute("href", message.substring(9)); + if (message.includes('/hidden/')) { + link.setAttribute("download", message.substring(24)); + } else { + link.setAttribute("download", message.substring(17)); + } link.click(); //virtually click <a> element to initiate download document.getElementById('progression').innerHTML = ''; }); + async function submitDownload() { + document.getElementById('msg').innerHTML = '<div class="notification is-success fadein" id="notif"></button>{{ antl.formatMessage('messages.dlStart') }}</div>'; + setTimeout(() => { + fadeout('notif') + }, 2000); + + FD = new FormData(document.getElementsByName('download-form')[0]); + fetch('/', { + method: 'POST', + body: FD + }); + + document.getElementById('progression').innerHTML = '<progress class="progress is-primary" max="100" id="progress">0%</progress>' + } + + async function fadeout(id) { + document.getElementById(id).classList.add('fadeout'); + setTimeout(() => { + let element = document.getElementById(id); + element.parentNode.removeChild(element); + }, 2000); + } + + async function toClipboard(text) { + navigator.clipboard.writeText(text) + .catch(err => { + console.error(err); + document.getElementById('msg').innerHTML = '<div class="notification is-error fadein" id="notif">{{ antl.formatMessage('messages.errorCopy') }}</div>'; + setTimeout(() => { + fadeout('notif') + }, 5000); + }); + document.getElementById('msg').innerHTML = '<div class="notification is-success fadein" id="notif">{{ antl.formatMessage('messages.successCopy') }}</div>'; + setTimeout(() => { + fadeout('notif') + }, 5000); + } + + // If alt download block other settings since they don't work anyway + document.getElementById('alt').onclick = async function() { + if(document.getElementById('alt').checked) { + document.getElementById('small').disabled = true; + document.getElementById('small').checked = false; + + document.getElementById('mp3').disabled = true; + document.getElementById('mp3').checked = false; + document.getElementById('flac').disabled = true; + document.getElementById('flac').checked = false; + document.getElementById('mp4').checked = true; + document.getElementById('high').checked = true; + } else { + document.getElementById('small').disabled = false; + document.getElementById('mp3').disabled = false; + document.getElementById('flac').disabled = false; + } + } + + // If user press enter do same thing as if pressing the button + let input = document.getElementById("URL"); + input.addEventListener("keyup", async function(event) { + if (event.keyCode === 13) { + event.preventDefault(); + document.getElementById("button").click(); + } + }); </script> </html>