aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/webtorrent.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-08-07 09:54:36 +0200
committerChocobozzz <me@florianbigard.com>2018-08-08 09:30:31 +0200
commit990b6a0b0c4fbebc165e5cf7cec8fbc1cbaa6c66 (patch)
tree8aaa0638798bfa14813f4d6ed5247242313b9ce6 /server/helpers/webtorrent.ts
parentce33919c24e7402d92d81f3cd8e545df52d98240 (diff)
downloadPeerTube-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.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))