diff options
Diffstat (limited to 'server/helpers/youtube-dl.ts')
-rw-r--r-- | server/helpers/youtube-dl.ts | 61 |
1 files changed, 36 insertions, 25 deletions
diff --git a/server/helpers/youtube-dl.ts b/server/helpers/youtube-dl.ts index 577a59dbf..fc9d416a1 100644 --- a/server/helpers/youtube-dl.ts +++ b/server/helpers/youtube-dl.ts | |||
@@ -24,20 +24,23 @@ const processOptions = { | |||
24 | } | 24 | } |
25 | 25 | ||
26 | function getYoutubeDLInfo (url: string, opts?: string[]): Promise<YoutubeDLInfo> { | 26 | function getYoutubeDLInfo (url: string, opts?: string[]): Promise<YoutubeDLInfo> { |
27 | return new Promise<YoutubeDLInfo>(async (res, rej) => { | 27 | return new Promise<YoutubeDLInfo>((res, rej) => { |
28 | let args = opts || [ '-j', '--flat-playlist' ] | 28 | let args = opts || [ '-j', '--flat-playlist' ] |
29 | args = wrapWithProxyOptions(args) | 29 | args = wrapWithProxyOptions(args) |
30 | 30 | ||
31 | const youtubeDL = await safeGetYoutubeDL() | 31 | safeGetYoutubeDL() |
32 | youtubeDL.getInfo(url, args, processOptions, (err, info) => { | 32 | .then(youtubeDL => { |
33 | if (err) return rej(err) | 33 | youtubeDL.getInfo(url, args, processOptions, (err, info) => { |
34 | if (info.is_live === true) return rej(new Error('Cannot download a live streaming.')) | 34 | if (err) return rej(err) |
35 | if (info.is_live === true) return rej(new Error('Cannot download a live streaming.')) | ||
35 | 36 | ||
36 | const obj = buildVideoInfo(normalizeObject(info)) | 37 | const obj = buildVideoInfo(normalizeObject(info)) |
37 | if (obj.name && obj.name.length < CONSTRAINTS_FIELDS.VIDEOS.NAME.min) obj.name += ' video' | 38 | if (obj.name && obj.name.length < CONSTRAINTS_FIELDS.VIDEOS.NAME.min) obj.name += ' video' |
38 | 39 | ||
39 | return res(obj) | 40 | return res(obj) |
40 | }) | 41 | }) |
42 | }) | ||
43 | .catch(err => rej(err)) | ||
41 | }) | 44 | }) |
42 | } | 45 | } |
43 | 46 | ||
@@ -54,26 +57,34 @@ function downloadYoutubeDLVideo (url: string, timeout: number) { | |||
54 | options = options.concat([ '--ffmpeg-location', process.env.FFMPEG_PATH ]) | 57 | options = options.concat([ '--ffmpeg-location', process.env.FFMPEG_PATH ]) |
55 | } | 58 | } |
56 | 59 | ||
57 | return new Promise<string>(async (res, rej) => { | 60 | return new Promise<string>((res, rej) => { |
58 | const youtubeDL = await safeGetYoutubeDL() | 61 | safeGetYoutubeDL() |
59 | youtubeDL.exec(url, options, processOptions, err => { | 62 | .then(youtubeDL => { |
60 | clearTimeout(timer) | 63 | youtubeDL.exec(url, options, processOptions, err => { |
64 | clearTimeout(timer) | ||
61 | 65 | ||
62 | if (err) { | 66 | if (err) { |
63 | remove(path) | 67 | remove(path) |
64 | .catch(err => logger.error('Cannot delete path on YoutubeDL error.', { err })) | 68 | .catch(err => logger.error('Cannot delete path on YoutubeDL error.', { err })) |
65 | 69 | ||
66 | return rej(err) | 70 | return rej(err) |
67 | } | 71 | } |
68 | 72 | ||
69 | return res(path) | 73 | return res(path) |
70 | }) | 74 | }) |
71 | 75 | ||
72 | timer = setTimeout(async () => { | 76 | timer = setTimeout(() => { |
73 | await remove(path) | 77 | const err = new Error('YoutubeDL download timeout.') |
74 | 78 | ||
75 | return rej(new Error('YoutubeDL download timeout.')) | 79 | remove(path) |
76 | }, timeout) | 80 | .finally(() => rej(err)) |
81 | .catch(err => { | ||
82 | logger.error('Cannot remove %s in youtubeDL timeout.', path, { err }) | ||
83 | return rej(err) | ||
84 | }) | ||
85 | }, timeout) | ||
86 | }) | ||
87 | .catch(err => rej(err)) | ||
77 | }) | 88 | }) |
78 | } | 89 | } |
79 | 90 | ||
@@ -103,7 +114,7 @@ async function updateYoutubeDLBinary () { | |||
103 | 114 | ||
104 | const url = result.headers.location | 115 | const url = result.headers.location |
105 | const downloadFile = request.get(url) | 116 | const downloadFile = request.get(url) |
106 | const newVersion = /yt-dl\.org\/downloads\/(\d{4}\.\d\d\.\d\d(\.\d)?)\/youtube-dl/.exec(url)[ 1 ] | 117 | const newVersion = /yt-dl\.org\/downloads\/(\d{4}\.\d\d\.\d\d(\.\d)?)\/youtube-dl/.exec(url)[1] |
107 | 118 | ||
108 | downloadFile.on('response', result => { | 119 | downloadFile.on('response', result => { |
109 | if (result.statusCode !== 200) { | 120 | if (result.statusCode !== 200) { |