diff options
Diffstat (limited to 'server/helpers/webtorrent.ts')
-rw-r--r-- | server/helpers/webtorrent.ts | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/server/helpers/webtorrent.ts b/server/helpers/webtorrent.ts index 8a5d030df..b25e44fcd 100644 --- a/server/helpers/webtorrent.ts +++ b/server/helpers/webtorrent.ts | |||
@@ -39,7 +39,7 @@ async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName | |||
39 | if (torrent.files.length !== 1) { | 39 | if (torrent.files.length !== 1) { |
40 | if (timer) clearTimeout(timer) | 40 | if (timer) clearTimeout(timer) |
41 | 41 | ||
42 | for (let file of torrent.files) { | 42 | for (const file of torrent.files) { |
43 | deleteDownloadedFile({ directoryPath, filepath: file.path }) | 43 | deleteDownloadedFile({ directoryPath, filepath: file.path }) |
44 | } | 44 | } |
45 | 45 | ||
@@ -47,15 +47,16 @@ async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName | |||
47 | .then(() => rej(new Error('Cannot import torrent ' + torrentId + ': there are multiple files in it'))) | 47 | .then(() => rej(new Error('Cannot import torrent ' + torrentId + ': there are multiple files in it'))) |
48 | } | 48 | } |
49 | 49 | ||
50 | file = torrent.files[ 0 ] | 50 | file = torrent.files[0] |
51 | 51 | ||
52 | // FIXME: avoid creating another stream when https://github.com/webtorrent/webtorrent/issues/1517 is fixed | 52 | // FIXME: avoid creating another stream when https://github.com/webtorrent/webtorrent/issues/1517 is fixed |
53 | const writeStream = createWriteStream(path) | 53 | const writeStream = createWriteStream(path) |
54 | writeStream.on('finish', () => { | 54 | writeStream.on('finish', () => { |
55 | if (timer) clearTimeout(timer) | 55 | if (timer) clearTimeout(timer) |
56 | 56 | ||
57 | return safeWebtorrentDestroy(webtorrent, torrentId, { directoryPath, filepath: file.path }, target.torrentName) | 57 | safeWebtorrentDestroy(webtorrent, torrentId, { directoryPath, filepath: file.path }, target.torrentName) |
58 | .then(() => res(path)) | 58 | .then(() => res(path)) |
59 | .catch(err => logger.error('Cannot destroy webtorrent.', { err })) | ||
59 | }) | 60 | }) |
60 | 61 | ||
61 | file.createReadStream().pipe(writeStream) | 62 | file.createReadStream().pipe(writeStream) |
@@ -63,9 +64,16 @@ async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName | |||
63 | 64 | ||
64 | torrent.on('error', err => rej(err)) | 65 | torrent.on('error', err => rej(err)) |
65 | 66 | ||
66 | timer = setTimeout(async () => { | 67 | timer = setTimeout(() => { |
67 | return safeWebtorrentDestroy(webtorrent, torrentId, file ? { directoryPath, filepath: file.path } : undefined, target.torrentName) | 68 | const err = new Error('Webtorrent download timeout.') |
68 | .then(() => rej(new Error('Webtorrent download timeout.'))) | 69 | |
70 | safeWebtorrentDestroy(webtorrent, torrentId, file ? { directoryPath, filepath: file.path } : undefined, target.torrentName) | ||
71 | .then(() => rej(err)) | ||
72 | .catch(destroyErr => { | ||
73 | logger.error('Cannot destroy webtorrent.', { err: destroyErr }) | ||
74 | rej(err) | ||
75 | }) | ||
76 | |||
69 | }, timeout) | 77 | }, timeout) |
70 | }) | 78 | }) |
71 | } | 79 | } |