@ -636,18 +636,20 @@ async function autoUpdateYoutubeDL() {
}
}
let current _app _details = JSON . parse ( fs . readFileSync ( current _app _details _path ) ) ;
let current _app _details = JSON . parse ( fs . readFileSync ( current _app _details _path ) ) ;
let current _version = current _app _details [ 'version' ] ;
let current _version = current _app _details [ 'version' ] ;
let stored _binary _path = current _app _details [ 'path' ] ;
// got version, now let's check the latest version from the youtube-dl API
// got version, now let's check the latest version from the youtube-dl API
let youtubedl _api _path = 'https://api.github.com/repos/ytdl-org/youtube-dl/tags' ;
let youtubedl _api _path = 'https://api.github.com/repos/ytdl-org/youtube-dl/tags' ;
fetch ( youtubedl _api _path , { method : 'Get' } )
fetch ( youtubedl _api _path , { method : 'Get' } )
. then ( res => res . json ( ) )
. then ( async res => res . json ( ) )
. then ( ( json ) => {
. then ( async ( json ) => {
// check if the versions are different
// check if the versions are different
const latest _update _version = json [ 0 ] [ 'name' ] ;
const latest _update _version = json [ 0 ] [ 'name' ] ;
if ( current _version !== latest _update _version ) {
if ( current _version !== latest _update _version ) {
let binary _path = 'node_modules/youtube-dl/bin' ;
let binary _path = 'node_modules/youtube-dl/bin' ;
// versions different, download new update
// versions different, download new update
console . log ( 'INFO: Found new update for youtube-dl. Updating binary...' ) ;
console . log ( 'INFO: Found new update for youtube-dl. Updating binary...' ) ;
await checkExistsWithTimeout ( stored _binary _path , 10000 ) ;
downloader ( binary _path , function error ( err , done ) {
downloader ( binary _path , function error ( err , done ) {
'use strict'
'use strict'
if ( err ) {
if ( err ) {
@ -663,6 +665,34 @@ async function autoUpdateYoutubeDL() {
} ) ;
} ) ;
}
}
async function checkExistsWithTimeout ( filePath , timeout ) {
return new Promise ( function ( resolve , reject ) {
var timer = setTimeout ( function ( ) {
watcher . close ( ) ;
reject ( new Error ( 'File did not exists and was not created during the timeout.' ) ) ;
} , timeout ) ;
fs . access ( filePath , fs . constants . R _OK , function ( err ) {
if ( ! err ) {
clearTimeout ( timer ) ;
watcher . close ( ) ;
resolve ( ) ;
}
} ) ;
var dir = path . dirname ( filePath ) ;
var basename = path . basename ( filePath ) ;
var watcher = fs . watch ( dir , function ( eventType , filename ) {
if ( eventType === 'rename' && filename === basename ) {
clearTimeout ( timer ) ;
watcher . close ( ) ;
resolve ( ) ;
}
} ) ;
} ) ;
}
app . use ( function ( req , res , next ) {
app . use ( function ( req , res , next ) {
res . header ( "Access-Control-Allow-Origin" , getOrigin ( ) ) ;
res . header ( "Access-Control-Allow-Origin" , getOrigin ( ) ) ;
res . header ( "Access-Control-Allow-Headers" , "Origin, X-Requested-With, Content-Type, Accept" ) ;
res . header ( "Access-Control-Allow-Headers" , "Origin, X-Requested-With, Content-Type, Accept" ) ;