diff options
author | Chocobozzz <me@florianbigard.com> | 2018-08-07 09:54:36 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-08-08 09:30:31 +0200 |
commit | 990b6a0b0c4fbebc165e5cf7cec8fbc1cbaa6c66 (patch) | |
tree | 8aaa0638798bfa14813f4d6ed5247242313b9ce6 /server/helpers/webtorrent.ts | |
parent | ce33919c24e7402d92d81f3cd8e545df52d98240 (diff) | |
download | PeerTube-990b6a0b0c4fbebc165e5cf7cec8fbc1cbaa6c66.tar.gz PeerTube-990b6a0b0c4fbebc165e5cf7cec8fbc1cbaa6c66.tar.zst PeerTube-990b6a0b0c4fbebc165e5cf7cec8fbc1cbaa6c66.zip |
Import torrents with webtorrent
Diffstat (limited to 'server/helpers/webtorrent.ts')
-rw-r--r-- | server/helpers/webtorrent.ts | 15 |
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' | |||
2 | import { generateVideoTmpPath } from './utils' | 2 | import { generateVideoTmpPath } from './utils' |
3 | import * as WebTorrent from 'webtorrent' | 3 | import * as WebTorrent from 'webtorrent' |
4 | import { createWriteStream } from 'fs' | 4 | import { createWriteStream } from 'fs' |
5 | import { Instance as ParseTorrent } from 'parse-torrent' | ||
6 | import { CONFIG } from '../initializers' | ||
7 | import { join } from 'path' | ||
5 | 8 | ||
6 | function downloadWebTorrentVideo (target: string) { | 9 | function 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)) |