aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/webtorrent-process.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-07-01 16:22:36 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-07-01 16:22:36 +0200
commit5f698b82c7055df763f3830882ac5bad1397db23 (patch)
tree97e2e8178a5c7eec8e8f0e0cfce9cff399452cdc /server/lib/webtorrent-process.js
parentfc51fde048f2c3ce1dd3e85f5528335040bae894 (diff)
downloadPeerTube-5f698b82c7055df763f3830882ac5bad1397db23.tar.gz
PeerTube-5f698b82c7055df763f3830882ac5bad1397db23.tar.zst
PeerTube-5f698b82c7055df763f3830882ac5bad1397db23.zip
Use dashes for filenames
Diffstat (limited to 'server/lib/webtorrent-process.js')
-rw-r--r--server/lib/webtorrent-process.js92
1 files changed, 92 insertions, 0 deletions
diff --git a/server/lib/webtorrent-process.js b/server/lib/webtorrent-process.js
new file mode 100644
index 000000000..be7ac5bb4
--- /dev/null
+++ b/server/lib/webtorrent-process.js
@@ -0,0 +1,92 @@
1'use strict'
2
3const WebTorrent = require('webtorrent')
4const ipc = require('node-ipc')
5
6function webtorrent (args) {
7 if (args.length !== 3) {
8 throw new Error('Wrong arguments number: ' + args.length + '/3')
9 }
10
11 const host = args[1]
12 const port = args[2]
13 const nodeKey = 'webtorrentnode' + port
14 const processKey = 'webtorrentprocess' + port
15
16 ipc.config.silent = true
17 ipc.config.id = processKey
18
19 if (host === 'client' && port === '1') global.WEBTORRENT_ANNOUNCE = []
20 else global.WEBTORRENT_ANNOUNCE = 'ws://' + host + ':' + port + '/tracker/socket'
21 const wt = new WebTorrent({ dht: false })
22
23 function seed (data) {
24 const args = data.args
25 const path = args.path
26 const _id = data._id
27
28 wt.seed(path, { announceList: '' }, function (torrent) {
29 const toSend = {
30 magnetUri: torrent.magnetURI
31 }
32
33 ipc.of[nodeKey].emit(nodeKey + '.seedDone.' + _id, toSend)
34 })
35 }
36
37 function add (data) {
38 const args = data.args
39 const magnetUri = args.magnetUri
40 const _id = data._id
41
42 wt.add(magnetUri, function (torrent) {
43 const toSend = {
44 files: []
45 }
46
47 torrent.files.forEach(function (file) {
48 toSend.files.push({ path: file.path })
49 })
50
51 ipc.of[nodeKey].emit(nodeKey + '.addDone.' + _id, toSend)
52 })
53 }
54
55 function remove (data) {
56 const args = data.args
57 const magnetUri = args.magnetUri
58 const _id = data._id
59
60 try {
61 wt.remove(magnetUri, callback)
62 } catch (err) {
63 console.log('Cannot remove the torrent from WebTorrent.')
64 return callback(null)
65 }
66
67 function callback () {
68 const toSend = {}
69 ipc.of[nodeKey].emit(nodeKey + '.removeDone.' + _id, toSend)
70 }
71 }
72
73 console.log('Configuration: ' + host + ':' + port)
74 console.log('Connecting to IPC...')
75
76 ipc.connectTo(nodeKey, function () {
77 ipc.of[nodeKey].on(processKey + '.seed', seed)
78 ipc.of[nodeKey].on(processKey + '.add', add)
79 ipc.of[nodeKey].on(processKey + '.remove', remove)
80
81 ipc.of[nodeKey].emit(processKey + '.ready')
82 console.log('Ready.')
83 })
84
85 process.on('uncaughtException', function (e) {
86 ipc.of[nodeKey].emit(processKey + '.exception', { exception: e.toString() })
87 })
88}
89
90// ---------------------------------------------------------------------------
91
92module.exports = webtorrent