@ -30,6 +30,7 @@ var subscriptions_api = require('./subscriptions')
const CONSTS = require ( './consts' )
const { spawn } = require ( 'child_process' )
const read _last _lines = require ( 'read-last-lines' ) ;
var ps = require ( 'ps-node' ) ;
const is _windows = process . platform === 'win32' ;
@ -503,6 +504,43 @@ async function getLatestVersion() {
} ) ;
}
async function killAllDownloads ( ) {
return new Promise ( resolve => {
ps . lookup ( {
command : 'youtube-dl' ,
} , function ( err , resultList ) {
if ( err ) {
// failed to get list of processes
logger . error ( 'Failed to get a list of running youtube-dl processes.' ) ;
logger . error ( err ) ;
resolve ( {
details : err ,
success : false
} ) ;
}
// processes that contain the string 'youtube-dl' in the name will be looped
resultList . forEach ( function ( process ) {
if ( process ) {
ps . kill ( process . pid , 'SIGKILL' , function ( err ) {
if ( err ) {
// failed to kill, process may have ended on its own
logger . warn ( ` Failed to kill process with PID ${ process . pid } ` ) ;
logger . warn ( err ) ;
}
else {
logger . verbose ( ` Process ${ process . pid } has been killed! ` ) ;
}
} ) ;
}
} ) ;
resolve ( {
success : true
} ) ;
} ) ;
} ) ;
}
async function setPortItemFromENV ( ) {
return new Promise ( resolve => {
config _api . setConfigItem ( 'ytdl_port' , backendPort . toString ( ) ) ;
@ -1931,42 +1969,9 @@ app.post('/api/tomp4', optionalJwt, async function(req, res) {
}
} ) ;
// gets the status of the mp3 file that's being downloaded
app . post ( '/api/fileStatusMp3' , function ( req , res ) {
var name = decodeURIComponent ( req . body . name + "" ) ;
var exists = "" ;
var fullpath = audioFolderPath + name + ".mp3" ;
if ( fs . existsSync ( fullpath ) ) {
exists = [ basePath + audioFolderPath + name , getFileSizeMp3 ( name ) ] ;
}
else
{
var percent = 0 ;
var size = getFileSizeMp3 ( name ) ;
var downloaded = getAmountDownloadedMp3 ( name ) ;
if ( size > 0 )
percent = downloaded / size ;
exists = [ "failed" , getFileSizeMp3 ( name ) , percent ] ;
}
res . send ( exists ) ;
} ) ;
// gets the status of the mp4 file that's being downloaded
app . post ( '/api/fileStatusMp4' , function ( req , res ) {
var name = decodeURIComponent ( req . body . name ) ;
var exists = "" ;
var fullpath = videoFolderPath + name + ".mp4" ;
if ( fs . existsSync ( fullpath ) ) {
exists = [ basePath + videoFolderPath + name , getFileSizeMp4 ( name ) ] ;
} else {
var percent = 0 ;
var size = getFileSizeMp4 ( name ) ;
var downloaded = getAmountDownloadedMp4 ( name ) ;
if ( size > 0 )
percent = downloaded / size ;
exists = [ "failed" , getFileSizeMp4 ( name ) , percent ] ;
}
res . send ( exists ) ;
app . post ( '/api/killAllDownloads' , optionalJwt , async function ( req , res ) {
const result _obj = await killAllDownloads ( ) ;
res . send ( result _obj ) ;
} ) ;
// gets all download mp3s