aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/webtorrent.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/webtorrent.ts')
-rw-r--r--server/helpers/webtorrent.ts15
1 files changed, 10 insertions, 5 deletions
diff --git a/server/helpers/webtorrent.ts b/server/helpers/webtorrent.ts
index fce88a1f6..04b3ac71b 100644
--- a/server/helpers/webtorrent.ts
+++ b/server/helpers/webtorrent.ts
@@ -2,17 +2,22 @@ import { logger } from './logger'
2import { generateVideoTmpPath } from './utils' 2import { generateVideoTmpPath } from './utils'
3import * as WebTorrent from 'webtorrent' 3import * as WebTorrent from 'webtorrent'
4import { createWriteStream } from 'fs' 4import { createWriteStream } from 'fs'
5import { Instance as ParseTorrent } from 'parse-torrent'
6import { CONFIG } from '../initializers'
7import { join } from 'path'
5 8
6function downloadWebTorrentVideo (target: string) { 9function downloadWebTorrentVideo (target: { magnetUri: string, torrentName: string }) {
7 const path = generateVideoTmpPath(target) 10 const id = target.magnetUri || target.torrentName
8 11
9 logger.info('Importing torrent video %s', target) 12 const path = generateVideoTmpPath(id)
13 logger.info('Importing torrent video %s', id)
10 14
11 return new Promise<string>((res, rej) => { 15 return new Promise<string>((res, rej) => {
12 const webtorrent = new WebTorrent() 16 const webtorrent = new WebTorrent()
13 17
14 const torrent = webtorrent.add(target, torrent => { 18 const torrentId = target.magnetUri || join(CONFIG.STORAGE.TORRENTS_DIR, target.torrentName)
15 if (torrent.files.length !== 1) throw new Error('The number of files is not equal to 1 for ' + target) 19 const torrent = webtorrent.add(torrentId, torrent => {
20 if (torrent.files.length !== 1) return rej(new Error('The number of files is not equal to 1 for ' + torrentId))
16 21
17 const file = torrent.files[ 0 ] 22 const file = torrent.files[ 0 ]
18 file.createReadStream().pipe(createWriteStream(path)) 23 file.createReadStream().pipe(createWriteStream(path))