]>
Commit | Line | Data |
---|---|---|
ce33919c | 1 | import { logger } from './logger' |
a71de50b | 2 | import { generateVideoTmpPath } from './utils' |
ce33919c | 3 | import * as WebTorrent from 'webtorrent' |
a71de50b | 4 | import { createWriteStream, ensureDir, remove } from 'fs-extra' |
990b6a0b | 5 | import { CONFIG } from '../initializers' |
d0b52b52 | 6 | import { dirname, join } from 'path' |
ce33919c | 7 | |
a71de50b | 8 | async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName?: string }, timeout?: number) { |
990b6a0b | 9 | const id = target.magnetUri || target.torrentName |
c48e82b5 | 10 | let timer |
ce33919c | 11 | |
a71de50b | 12 | const path = generateVideoTmpPath(id) |
990b6a0b | 13 | logger.info('Importing torrent video %s', id) |
ce33919c | 14 | |
a71de50b C |
15 | const directoryPath = join(CONFIG.STORAGE.VIDEOS_DIR, 'import') |
16 | await ensureDir(directoryPath) | |
17 | ||
ce33919c C |
18 | return new Promise<string>((res, rej) => { |
19 | const webtorrent = new WebTorrent() | |
c48e82b5 | 20 | let file: WebTorrent.TorrentFile |
ce33919c | 21 | |
990b6a0b | 22 | const torrentId = target.magnetUri || join(CONFIG.STORAGE.TORRENTS_DIR, target.torrentName) |
541006e3 | 23 | |
a71de50b | 24 | const options = { path: directoryPath } |
541006e3 | 25 | const torrent = webtorrent.add(torrentId, options, torrent => { |
c48e82b5 C |
26 | if (torrent.files.length !== 1) { |
27 | if (timer) clearTimeout(timer) | |
ce33919c | 28 | |
e37c85e9 C |
29 | for (let file of torrent.files) { |
30 | deleteDownloadedFile({ directoryPath, filepath: file.path }) | |
31 | } | |
32 | ||
33 | return safeWebtorrentDestroy(webtorrent, torrentId, undefined, target.torrentName) | |
dae4a1c0 | 34 | .then(() => rej(new Error('Cannot import torrent ' + torrentId + ': there are multiple files in it'))) |
c48e82b5 C |
35 | } |
36 | ||
a71de50b C |
37 | file = torrent.files[ 0 ] |
38 | ||
39 | // FIXME: avoid creating another stream when https://github.com/webtorrent/webtorrent/issues/1517 is fixed | |
40 | const writeStream = createWriteStream(path) | |
41 | writeStream.on('finish', () => { | |
42 | if (timer) clearTimeout(timer) | |
43 | ||
d0b52b52 | 44 | return safeWebtorrentDestroy(webtorrent, torrentId, { directoryPath, filepath: file.path }, target.torrentName) |
a71de50b | 45 | .then(() => res(path)) |
69fa54a0 | 46 | }) |
a71de50b C |
47 | |
48 | file.createReadStream().pipe(writeStream) | |
3e17515e | 49 | }) |
ce33919c C |
50 | |
51 | torrent.on('error', err => rej(err)) | |
c48e82b5 C |
52 | |
53 | if (timeout) { | |
54 | timer = setTimeout(async () => { | |
d0b52b52 | 55 | return safeWebtorrentDestroy(webtorrent, torrentId, file ? { directoryPath, filepath: file.path } : undefined, target.torrentName) |
c48e82b5 C |
56 | .then(() => rej(new Error('Webtorrent download timeout.'))) |
57 | }, timeout) | |
58 | } | |
ce33919c C |
59 | }) |
60 | } | |
61 | ||
62 | // --------------------------------------------------------------------------- | |
63 | ||
64 | export { | |
65 | downloadWebTorrentVideo | |
66 | } | |
c48e82b5 C |
67 | |
68 | // --------------------------------------------------------------------------- | |
69 | ||
d0b52b52 C |
70 | function safeWebtorrentDestroy ( |
71 | webtorrent: WebTorrent.Instance, | |
72 | torrentId: string, | |
73 | downloadedFile?: { directoryPath: string, filepath: string }, | |
74 | torrentName?: string | |
75 | ) { | |
c48e82b5 C |
76 | return new Promise(res => { |
77 | webtorrent.destroy(err => { | |
78 | // Delete torrent file | |
79 | if (torrentName) { | |
d0b52b52 | 80 | logger.debug('Removing %s torrent after webtorrent download.', torrentId) |
c48e82b5 C |
81 | remove(torrentId) |
82 | .catch(err => logger.error('Cannot remove torrent %s in webtorrent download.', torrentId, { err })) | |
83 | } | |
84 | ||
85 | // Delete downloaded file | |
e37c85e9 | 86 | if (downloadedFile) deleteDownloadedFile(downloadedFile) |
c48e82b5 | 87 | |
e37c85e9 | 88 | if (err) logger.warn('Cannot destroy webtorrent in timeout.', { err }) |
c48e82b5 C |
89 | |
90 | return res() | |
91 | }) | |
92 | }) | |
93 | } | |
e37c85e9 C |
94 | |
95 | function deleteDownloadedFile (downloadedFile: { directoryPath: string, filepath: string }) { | |
96 | // We want to delete the base directory | |
97 | let pathToDelete = dirname(downloadedFile.filepath) | |
98 | if (pathToDelete === '.') pathToDelete = downloadedFile.filepath | |
99 | ||
100 | const toRemovePath = join(downloadedFile.directoryPath, pathToDelete) | |
101 | ||
102 | logger.debug('Removing %s after webtorrent download.', toRemovePath) | |
103 | remove(toRemovePath) | |
104 | .catch(err => logger.error('Cannot remove torrent file %s in webtorrent download.', toRemovePath, { err })) | |
105 | } |