From cf9166cf2fb7b51a1137a259eed9338798c73500 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 1 Oct 2018 12:00:05 +0200 Subject: Add timeout on youtube dl to cleaup files --- server/helpers/youtube-dl.ts | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'server/helpers/youtube-dl.ts') diff --git a/server/helpers/youtube-dl.ts b/server/helpers/youtube-dl.ts index 748c67e6c..70b4e1b78 100644 --- a/server/helpers/youtube-dl.ts +++ b/server/helpers/youtube-dl.ts @@ -4,7 +4,7 @@ import { logger } from './logger' import { generateVideoTmpPath } from './utils' import { join } from 'path' import { root } from './core-utils' -import { ensureDir, writeFile } from 'fs-extra' +import { ensureDir, writeFile, remove } from 'fs-extra' import * as request from 'request' import { createWriteStream } from 'fs' @@ -39,8 +39,9 @@ function getYoutubeDLInfo (url: string, opts?: string[]): Promise }) } -function downloadYoutubeDLVideo (url: string) { +function downloadYoutubeDLVideo (url: string, timeout: number) { const path = generateVideoTmpPath(url) + let timer logger.info('Importing youtubeDL video %s', url) @@ -49,10 +50,23 @@ function downloadYoutubeDLVideo (url: string) { return new Promise(async (res, rej) => { const youtubeDL = await safeGetYoutubeDL() youtubeDL.exec(url, options, processOptions, err => { - if (err) return rej(err) + clearTimeout(timer) + + if (err) { + remove(path) + .catch(err => logger.error('Cannot delete path on YoutubeDL error.', { err })) + + return rej(err) + } return res(path) }) + + timer = setTimeout(async () => { + await remove(path) + + return rej(new Error('YoutubeDL download timeout.')) + }, timeout) }) } -- cgit v1.2.3