diff options
author | Chocobozzz <me@florianbigard.com> | 2018-10-01 12:00:05 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-10-01 12:00:05 +0200 |
commit | cf9166cf2fb7b51a1137a259eed9338798c73500 (patch) | |
tree | 7cf4b5995d4a29cc335e1fd72c1572510f8432ad /server/helpers/youtube-dl.ts | |
parent | 2a27c451f78922107c3f056e7506be8a79b31e03 (diff) | |
download | PeerTube-cf9166cf2fb7b51a1137a259eed9338798c73500.tar.gz PeerTube-cf9166cf2fb7b51a1137a259eed9338798c73500.tar.zst PeerTube-cf9166cf2fb7b51a1137a259eed9338798c73500.zip |
Add timeout on youtube dl to cleaup files
Diffstat (limited to 'server/helpers/youtube-dl.ts')
-rw-r--r-- | server/helpers/youtube-dl.ts | 20 |
1 files changed, 17 insertions, 3 deletions
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' | |||
4 | import { generateVideoTmpPath } from './utils' | 4 | import { generateVideoTmpPath } from './utils' |
5 | import { join } from 'path' | 5 | import { join } from 'path' |
6 | import { root } from './core-utils' | 6 | import { root } from './core-utils' |
7 | import { ensureDir, writeFile } from 'fs-extra' | 7 | import { ensureDir, writeFile, remove } from 'fs-extra' |
8 | import * as request from 'request' | 8 | import * as request from 'request' |
9 | import { createWriteStream } from 'fs' | 9 | import { createWriteStream } from 'fs' |
10 | 10 | ||
@@ -39,8 +39,9 @@ function getYoutubeDLInfo (url: string, opts?: string[]): Promise<YoutubeDLInfo> | |||
39 | }) | 39 | }) |
40 | } | 40 | } |
41 | 41 | ||
42 | function downloadYoutubeDLVideo (url: string) { | 42 | function downloadYoutubeDLVideo (url: string, timeout: number) { |
43 | const path = generateVideoTmpPath(url) | 43 | const path = generateVideoTmpPath(url) |
44 | let timer | ||
44 | 45 | ||
45 | logger.info('Importing youtubeDL video %s', url) | 46 | logger.info('Importing youtubeDL video %s', url) |
46 | 47 | ||
@@ -49,10 +50,23 @@ function downloadYoutubeDLVideo (url: string) { | |||
49 | return new Promise<string>(async (res, rej) => { | 50 | return new Promise<string>(async (res, rej) => { |
50 | const youtubeDL = await safeGetYoutubeDL() | 51 | const youtubeDL = await safeGetYoutubeDL() |
51 | youtubeDL.exec(url, options, processOptions, err => { | 52 | youtubeDL.exec(url, options, processOptions, err => { |
52 | if (err) return rej(err) | 53 | clearTimeout(timer) |
54 | |||
55 | if (err) { | ||
56 | remove(path) | ||
57 | .catch(err => logger.error('Cannot delete path on YoutubeDL error.', { err })) | ||
58 | |||
59 | return rej(err) | ||
60 | } | ||
53 | 61 | ||
54 | return res(path) | 62 | return res(path) |
55 | }) | 63 | }) |
64 | |||
65 | timer = setTimeout(async () => { | ||
66 | await remove(path) | ||
67 | |||
68 | return rej(new Error('YoutubeDL download timeout.')) | ||
69 | }, timeout) | ||
56 | }) | 70 | }) |
57 | } | 71 | } |
58 | 72 | ||