diff options
Diffstat (limited to 'server/helpers/webtorrent.ts')
-rw-r--r-- | server/helpers/webtorrent.ts | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/server/helpers/webtorrent.ts b/server/helpers/webtorrent.ts index 121cd0b41..6f2adb3cb 100644 --- a/server/helpers/webtorrent.ts +++ b/server/helpers/webtorrent.ts | |||
@@ -4,6 +4,7 @@ import * as WebTorrent from 'webtorrent' | |||
4 | import { createWriteStream } from 'fs' | 4 | import { createWriteStream } from 'fs' |
5 | import { CONFIG } from '../initializers' | 5 | import { CONFIG } from '../initializers' |
6 | import { join } from 'path' | 6 | import { join } from 'path' |
7 | import { unlinkPromise } from './core-utils' | ||
7 | 8 | ||
8 | function downloadWebTorrentVideo (target: { magnetUri: string, torrentName: string }) { | 9 | function downloadWebTorrentVideo (target: { magnetUri: string, torrentName: string }) { |
9 | const id = target.magnetUri || target.torrentName | 10 | const id = target.magnetUri || target.torrentName |
@@ -15,13 +16,29 @@ function downloadWebTorrentVideo (target: { magnetUri: string, torrentName: stri | |||
15 | const webtorrent = new WebTorrent() | 16 | const webtorrent = new WebTorrent() |
16 | 17 | ||
17 | const torrentId = target.magnetUri || join(CONFIG.STORAGE.TORRENTS_DIR, target.torrentName) | 18 | const torrentId = target.magnetUri || join(CONFIG.STORAGE.TORRENTS_DIR, target.torrentName) |
18 | const torrent = webtorrent.add(torrentId, torrent => { | 19 | |
20 | const options = { path: CONFIG.STORAGE.VIDEOS_DIR } | ||
21 | const torrent = webtorrent.add(torrentId, options, torrent => { | ||
19 | if (torrent.files.length !== 1) return rej(new Error('The number of files is not equal to 1 for ' + torrentId)) | 22 | if (torrent.files.length !== 1) return rej(new Error('The number of files is not equal to 1 for ' + torrentId)) |
20 | 23 | ||
21 | const file = torrent.files[ 0 ] | 24 | const file = torrent.files[ 0 ] |
22 | 25 | ||
23 | const writeStream = createWriteStream(path) | 26 | const writeStream = createWriteStream(path) |
24 | writeStream.on('finish', () => res(path)) | 27 | writeStream.on('finish', () => { |
28 | webtorrent.destroy(async err => { | ||
29 | if (err) return rej(err) | ||
30 | |||
31 | if (target.torrentName) { | ||
32 | unlinkPromise(torrentId) | ||
33 | .catch(err => logger.error('Cannot remove torrent %s in webtorrent download.', torrentId, { err })) | ||
34 | } | ||
35 | |||
36 | unlinkPromise(join(CONFIG.STORAGE.VIDEOS_DIR, file.name)) | ||
37 | .catch(err => logger.error('Cannot remove torrent file %s in webtorrent download.', file.name, { err })) | ||
38 | |||
39 | res(path) | ||
40 | }) | ||
41 | }) | ||
25 | 42 | ||
26 | file.createReadStream().pipe(writeStream) | 43 | file.createReadStream().pipe(writeStream) |
27 | }) | 44 | }) |