2019-12-03 21:18:40 +01:00
'use strict'
const youtubedl = require ( 'youtube-dl' )
const fs = require ( 'fs' )
const ffmpeg = require ( 'fluent-ffmpeg' )
2019-12-15 13:52:53 +01:00
const pJson = require ( '../../../package.json' ) ;
2019-12-03 21:18:40 +01:00
let viewCounter = 0 ;
let files = [ ] ;
let day ;
let month ;
2019-12-19 15:14:54 +01:00
let announcementArray = [ 'Twitter download seems to work fine now!' , 'u lookin hot today vro' , 'I am not responsible for what you download' , 'If you want to support me you can donate through my paypal at the bottom of the page' , 'Did you know this website is open source?' , 'Did you know this website can download from other website than youtube?' , 'You can mouse hover a video to see a preview of it!' ]
2019-12-18 19:30:30 +01:00
let announcement
2019-12-15 13:52:53 +01:00
let title = ` le epic downloader v ${ pJson . version } ` ;
2019-12-03 21:18:40 +01:00
class DownloadController {
async index ( { view , response } ) {
2019-12-18 19:30:30 +01:00
// Get random announcement
announcement = announcementArray [ Math . floor ( Math . random ( ) * announcementArray . length ) ] ;
2019-12-03 21:18:40 +01:00
// Get date for some event
let today = new Date ( ) ;
day = today . getDay ( ) ;
month = today . getMonth ( ) ;
viewCounter ++ ;
2019-12-15 13:52:53 +01:00
if ( response . request . url == '/legacy' ) return view . render ( 'legacy' , { title : title , viewCounter : viewCounter , day : day , month : month , announcement : announcement } ) ;
2019-12-03 21:18:40 +01:00
files = [ ] ;
let file = [ ]
for ( let f of fs . readdirSync ( './public/uploads' ) ) {
file . push ( f )
}
// get the 5 most recent files
file = file . sort ( function ( a , b ) {
2019-12-18 19:30:30 +01:00
if ( ( ( a || b ) . endsWith ( '.mp4' ) || ( a || b ) . endsWith ( '.webm' ) || ( a || b ) . endsWith ( '.mp3' ) || ( a || b ) . endsWith ( '.flac' ) ) && ! ( a || b ) . startsWith ( 'HIDE' ) ) {
2019-12-03 21:18:40 +01:00
let time1 = fs . statSync ( ` ./public/uploads/ ${ b } ` ) . ctime ;
let time2 = fs . statSync ( ` ./public/uploads/ ${ a } ` ) . ctime ;
if ( time1 < time2 ) return - 1 ;
if ( time1 > time2 ) return 1 ;
}
return 0 ;
} ) . slice ( 0 , 5 )
file . forEach ( ( file ) => {
// If mp4 and is not to be hidden from the recent feed
2019-12-18 19:30:30 +01:00
if ( ( file . endsWith ( '.mp4' ) || file . endsWith ( '.webm' ) ) && ! file . startsWith ( 'HIDE' ) ) {
2019-12-03 21:18:40 +01:00
let fileInfo = fs . statSync ( ` ./public/uploads/ ${ file } ` ) ;
2019-12-19 15:14:54 +01:00
/ *
2019-12-03 21:18:40 +01:00
// Take screenshot at the first frame of the mp4 file
2019-12-18 19:30:30 +01:00
2019-12-03 21:18:40 +01:00
ffmpeg ( ` ./public/uploads/ ${ file } ` )
. takeScreenshots ( { count : 1 , timemarks : [ 1 ] , size : '720x480' , filename : file + '.png' } , 'public/thumbnail' )
. on ( 'error' , ( err ) => {
console . error ( err ) ;
return ;
} ) ;
2019-12-19 15:14:54 +01:00
* /
2019-12-03 21:18:40 +01:00
2019-12-19 15:20:00 +01:00
// Send file name, file size in MB relative path for the file
files . push ( { name : file , size : ( fileInfo . size / 1000000.0 ) . toFixed ( 2 ) , location : ` uploads/ ${ file } ` } ) ;
2019-12-03 21:18:40 +01:00
// If mp3 or flac and not to be hidden from the recent feed
} else if ( ( file . endsWith ( '.mp3' ) || file . endsWith ( '.flac' ) ) && ! file . startsWith ( 'HIDE' ) ) {
let fileInfo = fs . statSync ( ` ./public/uploads/ ${ file } ` ) ;
2019-12-19 15:14:54 +01:00
// Send file name, file size in MB relative path for the file and relative path of music.png
files . push ( { name : file , size : ( fileInfo . size / 1000000.0 ) . toFixed ( 2 ) , location : ` uploads/ ${ file } ` , img : ` /asset/music.png ` } ) ;
2019-12-03 21:18:40 +01:00
}
} ) ;
2019-12-15 13:52:53 +01:00
return view . render ( 'index' , { title : title , viewCounter : viewCounter , file : files , day : day , month : month , announcement : announcement } ) ;
2019-12-03 21:18:40 +01:00
}
async download ( { view , request , response } ) {
let page = 'index' ;
if ( response . request . url == '/legacy' ) page = 'legacy' ;
// To be honest i forgot what it does, but i think i need it
response . implicitEnd = false
let option , DLFile
// Get form input
let data = {
url : request . input ( 'URL' ) ,
quality : request . input ( 'quality' ) ,
format : request . input ( 'format' ) ,
alt : request . input ( 'alt' ) ,
feed : request . input ( 'feed' )
}
if ( ! data . url ) {
return view . render ( page , {
2019-12-15 13:52:53 +01:00
title : title ,
2019-12-03 21:18:40 +01:00
viewCounter : viewCounter ,
file : files ,
day : day , month : month , announcement : announcement ,
error : true ,
errormsg : 'bruh moment, you didin\'t input a link.'
} ) ;
}
// 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 ) {
if ( fs . existsSync ( './public/uploads/alt.mp4' ) ) {
fs . unlink ( './public/uploads/alt.mp4' , ( err ) => {
if ( err ) ;
} ) ;
}
return youtubedl . exec ( data . url , [ '--format=mp4' , '-o' , ` public/uploads/alt.mp4 ` ] , { } , function ( err , output ) {
if ( err ) {
return view . render ( page , {
2019-12-15 13:52:53 +01:00
title : title ,
2019-12-03 21:18:40 +01:00
viewCounter : viewCounter ,
file : files ,
day : day , month : month , announcement : announcement ,
error : true ,
errormsg : 'bruh moment, you didin\'t input a valid link.'
} ) ;
}
return response . attachment ( './public/uploads/alt.mp4' ) ;
} ) ;
} else {
2019-12-18 19:30:30 +01:00
// Download as mp4 if possible
2019-12-03 21:18:40 +01:00
let video = youtubedl ( data . url , [ '--format=mp4' , '-f' , option ] ) ;
video . on ( 'error' , function ( err ) {
2019-12-15 13:52:53 +01:00
console . error ( err ) ;
2019-12-03 21:18:40 +01:00
return view . render ( page , {
2019-12-15 13:52:53 +01:00
title : title ,
2019-12-03 21:18:40 +01:00
viewCounter : viewCounter ,
file : files ,
day : day , month : month , announcement : announcement ,
error : true ,
errormsg : 'bruh moment, you didin\'t input a valid link.'
} ) ;
} )
2019-12-18 19:30:30 +01:00
let ext ;
2019-12-03 21:18:40 +01:00
video . on ( 'info' , function ( info ) {
// Set file name
2019-12-18 19:30:30 +01:00
ext = info . ext ;
2019-12-15 13:52:53 +01:00
let title = info . title . slice ( 0 , 80 ) ;
2019-12-18 19:30:30 +01:00
DLFile = ` ${ title . replace ( /\s/g , '_' ) } . ${ ext } ` ;
2019-12-15 13:52:53 +01:00
2019-12-03 21:18:40 +01:00
// If no title use the ID
if ( title == '_' ) title = ` _ ${ info . id } ` ;
// If user want to hide from the feed
if ( data . feed == 'on' )
2019-12-18 19:30:30 +01:00
DLFile = ` HIDE ${ title . replace ( /\s/g , '_' ) } . ${ ext } ` ;
2019-12-03 21:18:40 +01:00
DLFile = DLFile . replace ( /[()]/g , '_' ) ;
video . pipe ( fs . createWriteStream ( ` ./public/uploads/ ${ DLFile } ` ) ) ;
} ) ;
video . on ( 'end' , function ( ) {
2019-12-18 19:30:30 +01:00
if ( data . format == 'mp4' || data . format == 'webm' ) {
2019-12-03 21:18:40 +01:00
// If user requested mp4 directly attach the file
return response . attachment ( ` ./public/uploads/ ${ DLFile } ` )
} else {
// If user requested an audio format, convert it
ffmpeg ( ` ./public/uploads/ ${ DLFile } ` )
. noVideo ( )
. audioChannels ( '2' )
. audioFrequency ( '44100' )
. audioBitrate ( '320k' )
. format ( data . format )
2019-12-18 19:30:30 +01:00
. save ( ` ./public/uploads/ ${ DLFile . replace ( ext , ` . ${ data . format } ` ) } ` )
2019-12-03 21:18:40 +01:00
. on ( 'end' , ( ) => {
fs . unlinkSync ( ` ./public/uploads/ ${ DLFile } ` ) ;
2019-12-18 19:30:30 +01:00
return response . attachment ( ` ./public/uploads/ ${ DLFile . replace ( ext , ` . ${ data . format } ` ) } ` ) ;
2019-12-03 21:18:40 +01:00
} )
}
} ) ;
}
}
}
module . exports = DownloadController