aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/webtorrent.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-08-06 17:13:39 +0200
committerChocobozzz <me@florianbigard.com>2018-08-08 09:30:31 +0200
commitce33919c24e7402d92d81f3cd8e545df52d98240 (patch)
tree7e131a2f8df649899d0a71294665cf386ffb50d4 /server/helpers/webtorrent.ts
parent788487140c500abeb69ca44daf3a9e26efa8d36f (diff)
downloadPeerTube-ce33919c24e7402d92d81f3cd8e545df52d98240.tar.gz
PeerTube-ce33919c24e7402d92d81f3cd8e545df52d98240.tar.zst
PeerTube-ce33919c24e7402d92d81f3cd8e545df52d98240.zip
Import magnets with webtorrent
Diffstat (limited to 'server/helpers/webtorrent.ts')
-rw-r--r--server/helpers/webtorrent.ts31
1 files changed, 31 insertions, 0 deletions
diff --git a/server/helpers/webtorrent.ts b/server/helpers/webtorrent.ts
new file mode 100644
index 000000000..fce88a1f6
--- /dev/null
+++ b/server/helpers/webtorrent.ts
@@ -0,0 +1,31 @@
1import { logger } from './logger'
2import { generateVideoTmpPath } from './utils'
3import * as WebTorrent from 'webtorrent'
4import { createWriteStream } from 'fs'
5
6function downloadWebTorrentVideo (target: string) {
7 const path = generateVideoTmpPath(target)
8
9 logger.info('Importing torrent video %s', target)
10
11 return new Promise<string>((res, rej) => {
12 const webtorrent = new WebTorrent()
13
14 const torrent = webtorrent.add(target, torrent => {
15 if (torrent.files.length !== 1) throw new Error('The number of files is not equal to 1 for ' + target)
16
17 const file = torrent.files[ 0 ]
18 file.createReadStream().pipe(createWriteStream(path))
19 })
20
21 torrent.on('done', () => res(path))
22
23 torrent.on('error', err => rej(err))
24 })
25}
26
27// ---------------------------------------------------------------------------
28
29export {
30 downloadWebTorrentVideo
31}