diff --git a/backend/app.js b/backend/app.js index 3b3cac0..f7c1ee7 100644 --- a/backend/app.js +++ b/backend/app.js @@ -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 diff --git a/backend/package.json b/backend/package.json index e6f7f52..f015272 100644 --- a/backend/package.json +++ b/backend/package.json @@ -50,6 +50,7 @@ "passport-jwt": "^4.0.0", "passport-local": "^1.0.0", "progress": "^2.0.3", + "ps-node": "^0.1.6", "read-last-lines": "^1.7.2", "shortid": "^2.2.15", "unzipper": "^0.10.10", diff --git a/src/app/posts.services.ts b/src/app/posts.services.ts index b334c1e..64eb15e 100644 --- a/src/app/posts.services.ts +++ b/src/app/posts.services.ts @@ -163,12 +163,8 @@ export class PostsService implements CanActivate { ui_uid: ui_uid}, this.httpOptions); } - getFileStatusMp3(name: string) { - return this.http.post(this.path + 'fileStatusMp3', {name: name}, this.httpOptions); - } - - getFileStatusMp4(name: string) { - return this.http.post(this.path + 'fileStatusMp4', {name: name}, this.httpOptions); + killAllDownloads() { + return this.http.post(this.path + 'killAllDownloads', {}, this.httpOptions); } loadNavItems() { diff --git a/src/app/settings/settings.component.html b/src/app/settings/settings.component.html index b5cf98d..7a0ee52 100644 --- a/src/app/settings/settings.component.html +++ b/src/app/settings/settings.component.html @@ -153,9 +153,13 @@