]>
Commit | Line | Data |
---|---|---|
ce33919c C |
1 | import { logger } from './logger' |
2 | import { generateVideoTmpPath } from './utils' | |
3 | import * as WebTorrent from 'webtorrent' | |
62689b94 | 4 | import { createWriteStream, remove } from 'fs-extra' |
990b6a0b C |
5 | import { CONFIG } from '../initializers' |
6 | import { join } from 'path' | |
ce33919c | 7 | |
c48e82b5 | 8 | function downloadWebTorrentVideo (target: { magnetUri: string, torrentName?: string }, timeout?: number) { |
990b6a0b | 9 | const id = target.magnetUri || target.torrentName |
c48e82b5 | 10 | let timer |
ce33919c | 11 | |
990b6a0b C |
12 | const path = generateVideoTmpPath(id) |
13 | logger.info('Importing torrent video %s', id) | |
ce33919c C |
14 | |
15 | return new Promise<string>((res, rej) => { | |
16 | const webtorrent = new WebTorrent() | |
c48e82b5 | 17 | let file: WebTorrent.TorrentFile |
ce33919c | 18 | |
990b6a0b | 19 | const torrentId = target.magnetUri || join(CONFIG.STORAGE.TORRENTS_DIR, target.torrentName) |
541006e3 C |
20 | |
21 | const options = { path: CONFIG.STORAGE.VIDEOS_DIR } | |
22 | const torrent = webtorrent.add(torrentId, options, torrent => { | |
c48e82b5 C |
23 | if (torrent.files.length !== 1) { |
24 | if (timer) clearTimeout(timer) | |
ce33919c | 25 | |
c48e82b5 | 26 | return safeWebtorrentDestroy(webtorrent, torrentId, file.name, target.torrentName) |
dae4a1c0 | 27 | .then(() => rej(new Error('Cannot import torrent ' + torrentId + ': there are multiple files in it'))) |
c48e82b5 C |
28 | } |
29 | ||
30 | file = torrent.files[ 0 ] | |
ce33919c | 31 | |
3e17515e | 32 | const writeStream = createWriteStream(path) |
541006e3 | 33 | writeStream.on('finish', () => { |
c48e82b5 | 34 | if (timer) clearTimeout(timer) |
541006e3 | 35 | |
c48e82b5 C |
36 | return safeWebtorrentDestroy(webtorrent, torrentId, file.name, target.torrentName) |
37 | .then(() => res(path)) | |
541006e3 | 38 | }) |
3e17515e C |
39 | |
40 | file.createReadStream().pipe(writeStream) | |
41 | }) | |
ce33919c C |
42 | |
43 | torrent.on('error', err => rej(err)) | |
c48e82b5 C |
44 | |
45 | if (timeout) { | |
46 | timer = setTimeout(async () => { | |
47 | return safeWebtorrentDestroy(webtorrent, torrentId, file ? file.name : undefined, target.torrentName) | |
48 | .then(() => rej(new Error('Webtorrent download timeout.'))) | |
49 | }, timeout) | |
50 | } | |
ce33919c C |
51 | }) |
52 | } | |
53 | ||
54 | // --------------------------------------------------------------------------- | |
55 | ||
56 | export { | |
57 | downloadWebTorrentVideo | |
58 | } | |
c48e82b5 C |
59 | |
60 | // --------------------------------------------------------------------------- | |
61 | ||
62 | function safeWebtorrentDestroy (webtorrent: WebTorrent.Instance, torrentId: string, filename?: string, torrentName?: string) { | |
63 | return new Promise(res => { | |
64 | webtorrent.destroy(err => { | |
65 | // Delete torrent file | |
66 | if (torrentName) { | |
67 | remove(torrentId) | |
68 | .catch(err => logger.error('Cannot remove torrent %s in webtorrent download.', torrentId, { err })) | |
69 | } | |
70 | ||
71 | // Delete downloaded file | |
72 | if (filename) { | |
73 | remove(join(CONFIG.STORAGE.VIDEOS_DIR, filename)) | |
74 | .catch(err => logger.error('Cannot remove torrent file %s in webtorrent download.', filename, { err })) | |
75 | } | |
76 | ||
77 | if (err) { | |
78 | logger.warn('Cannot destroy webtorrent in timeout.', { err }) | |
79 | } | |
80 | ||
81 | return res() | |
82 | }) | |
83 | }) | |
84 | } |