diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | client/package.json | 2 | ||||
-rw-r--r-- | config/default.yaml | 1 | ||||
-rw-r--r-- | config/test-1.yaml | 1 | ||||
-rw-r--r-- | config/test-2.yaml | 1 | ||||
-rw-r--r-- | config/test-3.yaml | 1 | ||||
-rw-r--r-- | config/test-4.yaml | 1 | ||||
-rw-r--r-- | config/test-5.yaml | 1 | ||||
-rw-r--r-- | config/test-6.yaml | 1 | ||||
-rw-r--r-- | package.json | 6 | ||||
-rw-r--r-- | server.js | 47 | ||||
-rw-r--r-- | server/initializers/constants.js | 12 | ||||
-rw-r--r-- | server/lib/webtorrent-process.js | 92 | ||||
-rw-r--r-- | server/lib/webtorrent.js | 161 | ||||
-rw-r--r-- | server/models/video.js | 56 |
15 files changed, 58 insertions, 326 deletions
diff --git a/.gitignore b/.gitignore index debe118e1..ec5b4b2f0 100644 --- a/.gitignore +++ b/.gitignore | |||
@@ -14,3 +14,4 @@ uploads | |||
14 | thumbnails | 14 | thumbnails |
15 | config/production.yaml | 15 | config/production.yaml |
16 | ffmpeg | 16 | ffmpeg |
17 | torrents | ||
diff --git a/client/package.json b/client/package.json index b01e768b3..a5c5d092b 100644 --- a/client/package.json +++ b/client/package.json | |||
@@ -63,7 +63,7 @@ | |||
63 | "webpack-md5-hash": "0.0.5", | 63 | "webpack-md5-hash": "0.0.5", |
64 | "webpack-merge": "^0.13.0", | 64 | "webpack-merge": "^0.13.0", |
65 | "webpack-notifier": "^1.3.0", | 65 | "webpack-notifier": "^1.3.0", |
66 | "webtorrent": "^0.93.2", | 66 | "webtorrent": "^0.95.2", |
67 | "zone.js": "0.6.12" | 67 | "zone.js": "0.6.12" |
68 | }, | 68 | }, |
69 | "devDependencies": { | 69 | "devDependencies": { |
diff --git a/config/default.yaml b/config/default.yaml index b5fd7b06d..9a8a57879 100644 --- a/config/default.yaml +++ b/config/default.yaml | |||
@@ -17,6 +17,7 @@ storage: | |||
17 | uploads: 'uploads/' | 17 | uploads: 'uploads/' |
18 | logs: 'logs/' | 18 | logs: 'logs/' |
19 | thumbnails: 'thumbnails/' | 19 | thumbnails: 'thumbnails/' |
20 | torrents: 'torrents/' | ||
20 | 21 | ||
21 | network: | 22 | network: |
22 | friends: [] | 23 | friends: [] |
diff --git a/config/test-1.yaml b/config/test-1.yaml index 3afcb1b04..0998eaea1 100644 --- a/config/test-1.yaml +++ b/config/test-1.yaml | |||
@@ -14,6 +14,7 @@ storage: | |||
14 | uploads: 'test1/uploads/' | 14 | uploads: 'test1/uploads/' |
15 | logs: 'test1/logs/' | 15 | logs: 'test1/logs/' |
16 | thumbnails: 'test1/thumbnails/' | 16 | thumbnails: 'test1/thumbnails/' |
17 | torrents: 'test1/torrents/' | ||
17 | 18 | ||
18 | network: | 19 | network: |
19 | friends: | 20 | friends: |
diff --git a/config/test-2.yaml b/config/test-2.yaml index 51cc186eb..ec2cff811 100644 --- a/config/test-2.yaml +++ b/config/test-2.yaml | |||
@@ -14,6 +14,7 @@ storage: | |||
14 | uploads: 'test2/uploads/' | 14 | uploads: 'test2/uploads/' |
15 | logs: 'test2/logs/' | 15 | logs: 'test2/logs/' |
16 | thumbnails: 'test2/thumbnails/' | 16 | thumbnails: 'test2/thumbnails/' |
17 | torrents: 'test2/torrents/' | ||
17 | 18 | ||
18 | network: | 19 | network: |
19 | friends: | 20 | friends: |
diff --git a/config/test-3.yaml b/config/test-3.yaml index 7ef01ba4d..24f5533e0 100644 --- a/config/test-3.yaml +++ b/config/test-3.yaml | |||
@@ -14,6 +14,7 @@ storage: | |||
14 | uploads: 'test3/uploads/' | 14 | uploads: 'test3/uploads/' |
15 | logs: 'test3/logs/' | 15 | logs: 'test3/logs/' |
16 | thumbnails: 'test3/thumbnails/' | 16 | thumbnails: 'test3/thumbnails/' |
17 | torrents: 'test3/torrents/' | ||
17 | 18 | ||
18 | network: | 19 | network: |
19 | friends: | 20 | friends: |
diff --git a/config/test-4.yaml b/config/test-4.yaml index a4d3bb164..1f884dbf2 100644 --- a/config/test-4.yaml +++ b/config/test-4.yaml | |||
@@ -14,6 +14,7 @@ storage: | |||
14 | uploads: 'test4/uploads/' | 14 | uploads: 'test4/uploads/' |
15 | logs: 'test4/logs/' | 15 | logs: 'test4/logs/' |
16 | thumbnails: 'test4/thumbnails/' | 16 | thumbnails: 'test4/thumbnails/' |
17 | torrents: 'test4/torrents/' | ||
17 | 18 | ||
18 | network: | 19 | network: |
19 | friends: | 20 | friends: |
diff --git a/config/test-5.yaml b/config/test-5.yaml index 0435c17fe..08ed9f068 100644 --- a/config/test-5.yaml +++ b/config/test-5.yaml | |||
@@ -14,6 +14,7 @@ storage: | |||
14 | uploads: 'test5/uploads/' | 14 | uploads: 'test5/uploads/' |
15 | logs: 'test5/logs/' | 15 | logs: 'test5/logs/' |
16 | thumbnails: 'test5/thumbnails/' | 16 | thumbnails: 'test5/thumbnails/' |
17 | torrents: 'test5/torrents/' | ||
17 | 18 | ||
18 | network: | 19 | network: |
19 | friends: | 20 | friends: |
diff --git a/config/test-6.yaml b/config/test-6.yaml index b7dbd4bdd..a57784cca 100644 --- a/config/test-6.yaml +++ b/config/test-6.yaml | |||
@@ -14,6 +14,7 @@ storage: | |||
14 | uploads: 'test6/uploads/' | 14 | uploads: 'test6/uploads/' |
15 | logs: 'test6/logs/' | 15 | logs: 'test6/logs/' |
16 | thumbnails: 'test6/thumbnails/' | 16 | thumbnails: 'test6/thumbnails/' |
17 | torrents: 'test6/torrents/' | ||
17 | 18 | ||
18 | network: | 19 | network: |
19 | friends: | 20 | friends: |
diff --git a/package.json b/package.json index 9a0d4ecaa..63d014376 100644 --- a/package.json +++ b/package.json | |||
@@ -39,28 +39,28 @@ | |||
39 | "body-parser": "^1.12.4", | 39 | "body-parser": "^1.12.4", |
40 | "concurrently": "^2.0.0", | 40 | "concurrently": "^2.0.0", |
41 | "config": "^1.14.0", | 41 | "config": "^1.14.0", |
42 | "create-torrent": "^3.24.5", | ||
42 | "debug": "^2.2.0", | 43 | "debug": "^2.2.0", |
43 | "dezalgo": "^1.0.3", | 44 | "dezalgo": "^1.0.3", |
44 | "electron-spawn": "https://github.com/Chocobozzz/electron-spawn", | ||
45 | "express": "^4.12.4", | 45 | "express": "^4.12.4", |
46 | "express-oauth-server": "https://github.com/Chocobozzz/express-oauth-server", | 46 | "express-oauth-server": "https://github.com/Chocobozzz/express-oauth-server", |
47 | "express-validator": "^2.11.0", | 47 | "express-validator": "^2.11.0", |
48 | "fluent-ffmpeg": "^2.1.0", | 48 | "fluent-ffmpeg": "^2.1.0", |
49 | "js-yaml": "^3.5.4", | 49 | "js-yaml": "^3.5.4", |
50 | "lodash": "^4.11.1", | 50 | "lodash": "^4.11.1", |
51 | "magnet-uri": "^5.1.4", | ||
51 | "mkdirp": "^0.5.1", | 52 | "mkdirp": "^0.5.1", |
52 | "mongoose": "^4.0.5", | 53 | "mongoose": "^4.0.5", |
53 | "morgan": "^1.5.3", | 54 | "morgan": "^1.5.3", |
54 | "multer": "^1.1.0", | 55 | "multer": "^1.1.0", |
55 | "node-ipc": "^8.0.0", | ||
56 | "openssl-wrapper": "^0.3.4", | 56 | "openssl-wrapper": "^0.3.4", |
57 | "parse-torrent": "^5.8.0", | ||
57 | "password-generator": "^2.0.2", | 58 | "password-generator": "^2.0.2", |
58 | "request": "^2.57.0", | 59 | "request": "^2.57.0", |
59 | "request-replay": "^1.0.2", | 60 | "request-replay": "^1.0.2", |
60 | "scripty": "^1.5.0", | 61 | "scripty": "^1.5.0", |
61 | "segfault-handler": "^1.0.0", | 62 | "segfault-handler": "^1.0.0", |
62 | "ursa": "^0.9.1", | 63 | "ursa": "^0.9.1", |
63 | "webtorrent": "^0.93.2", | ||
64 | "winston": "^2.1.1", | 64 | "winston": "^2.1.1", |
65 | "ws": "^1.1.1" | 65 | "ws": "^1.1.1" |
66 | }, | 66 | }, |
@@ -34,10 +34,7 @@ const customValidators = require('./server/helpers/custom-validators') | |||
34 | const installer = require('./server/initializers/installer') | 34 | const installer = require('./server/initializers/installer') |
35 | const mongoose = require('mongoose') | 35 | const mongoose = require('mongoose') |
36 | const routes = require('./server/controllers') | 36 | const routes = require('./server/controllers') |
37 | const utils = require('./server/helpers/utils') | ||
38 | const webtorrent = require('./server/lib/webtorrent') | ||
39 | const Request = mongoose.model('Request') | 37 | const Request = mongoose.model('Request') |
40 | const Video = mongoose.model('Video') | ||
41 | 38 | ||
42 | // Get configurations | 39 | // Get configurations |
43 | const port = config.get('listen.port') | 40 | const port = config.get('listen.port') |
@@ -72,9 +69,16 @@ app.use('/client/*', function (req, res, next) { | |||
72 | res.sendStatus(404) | 69 | res.sendStatus(404) |
73 | }) | 70 | }) |
74 | 71 | ||
72 | const torrentsPhysicalPath = path.join(__dirname, config.get('storage.torrents')) | ||
73 | app.use(constants.STATIC_PATHS.TORRENTS, express.static(torrentsPhysicalPath, { maxAge: 0 })) | ||
74 | |||
75 | // Uploads path for webseeding | ||
76 | const uploadsPhysicalPath = path.join(__dirname, config.get('storage.uploads')) | ||
77 | app.use(constants.STATIC_PATHS.WEBSEED, express.static(uploadsPhysicalPath, { maxAge: 0 })) | ||
78 | |||
75 | // Thumbnails path for express | 79 | // Thumbnails path for express |
76 | const thumbnailsPhysicalPath = path.join(__dirname, config.get('storage.thumbnails')) | 80 | const thumbnailsPhysicalPath = path.join(__dirname, config.get('storage.thumbnails')) |
77 | app.use(constants.THUMBNAILS_STATIC_PATH, express.static(thumbnailsPhysicalPath, { maxAge: 0 })) | 81 | app.use(constants.STATIC_PATHS.THUMBNAILS, express.static(thumbnailsPhysicalPath, { maxAge: 0 })) |
78 | 82 | ||
79 | // Client application | 83 | // Client application |
80 | app.use('/*', function (req, res, next) { | 84 | app.use('/*', function (req, res, next) { |
@@ -121,33 +125,14 @@ app.use(function (err, req, res, next) { | |||
121 | installer.installApplication(function (err) { | 125 | installer.installApplication(function (err) { |
122 | if (err) throw err | 126 | if (err) throw err |
123 | 127 | ||
124 | // Create/activate the webtorrent module | 128 | // ----------- Make the server listening ----------- |
125 | webtorrent.create(function () { | 129 | server.listen(port, function () { |
126 | function cleanForExit () { | 130 | // Activate the pool requests |
127 | utils.cleanForExit(webtorrent.app) | 131 | Request.activate() |
128 | } | 132 | |
129 | 133 | logger.info('Seeded all the videos') | |
130 | function exitGracefullyOnSignal () { | 134 | logger.info('Server listening on port %d', port) |
131 | process.exit(-1) | 135 | app.emit('ready') |
132 | } | ||
133 | |||
134 | process.on('exit', cleanForExit) | ||
135 | process.on('SIGINT', exitGracefullyOnSignal) | ||
136 | process.on('SIGTERM', exitGracefullyOnSignal) | ||
137 | |||
138 | // ----------- Make the server listening ----------- | ||
139 | server.listen(port, function () { | ||
140 | // Activate the pool requests | ||
141 | Request.activate() | ||
142 | |||
143 | Video.seedAllExisting(function (err) { | ||
144 | if (err) throw err | ||
145 | |||
146 | logger.info('Seeded all the videos') | ||
147 | logger.info('Server listening on port %d', port) | ||
148 | app.emit('ready') | ||
149 | }) | ||
150 | }) | ||
151 | }) | 136 | }) |
152 | }) | 137 | }) |
153 | 138 | ||
diff --git a/server/initializers/constants.js b/server/initializers/constants.js index 467816f2c..e0ea188af 100644 --- a/server/initializers/constants.js +++ b/server/initializers/constants.js | |||
@@ -49,12 +49,16 @@ const SORTABLE_COLUMNS = { | |||
49 | VIDEOS: [ 'name', '-name', 'duration', '-duration', 'createdDate', '-createdDate' ] | 49 | VIDEOS: [ 'name', '-name', 'duration', '-duration', 'createdDate', '-createdDate' ] |
50 | } | 50 | } |
51 | 51 | ||
52 | // Express static paths (router) | ||
53 | const STATIC_PATHS = { | ||
54 | THUMBNAILS: '/static/thumbnails', | ||
55 | TORRENTS: '/static/torrents/', | ||
56 | WEBSEED: '/static/webseed/' | ||
57 | } | ||
58 | |||
52 | // Videos thumbnail size | 59 | // Videos thumbnail size |
53 | const THUMBNAILS_SIZE = '200x110' | 60 | const THUMBNAILS_SIZE = '200x110' |
54 | 61 | ||
55 | // Path for access to thumbnails with express router | ||
56 | const THUMBNAILS_STATIC_PATH = '/static/thumbnails' | ||
57 | |||
58 | const VIDEOS_CONSTRAINTS_FIELDS = { | 62 | const VIDEOS_CONSTRAINTS_FIELDS = { |
59 | NAME: { min: 3, max: 50 }, // Length | 63 | NAME: { min: 3, max: 50 }, // Length |
60 | DESCRIPTION: { min: 3, max: 250 }, // Length | 64 | DESCRIPTION: { min: 3, max: 250 }, // Length |
@@ -89,8 +93,8 @@ module.exports = { | |||
89 | SEARCHABLE_COLUMNS: SEARCHABLE_COLUMNS, | 93 | SEARCHABLE_COLUMNS: SEARCHABLE_COLUMNS, |
90 | SEEDS_IN_PARALLEL: SEEDS_IN_PARALLEL, | 94 | SEEDS_IN_PARALLEL: SEEDS_IN_PARALLEL, |
91 | SORTABLE_COLUMNS: SORTABLE_COLUMNS, | 95 | SORTABLE_COLUMNS: SORTABLE_COLUMNS, |
96 | STATIC_PATHS: STATIC_PATHS, | ||
92 | THUMBNAILS_SIZE: THUMBNAILS_SIZE, | 97 | THUMBNAILS_SIZE: THUMBNAILS_SIZE, |
93 | THUMBNAILS_STATIC_PATH: THUMBNAILS_STATIC_PATH, | ||
94 | VIDEOS_CONSTRAINTS_FIELDS: VIDEOS_CONSTRAINTS_FIELDS | 98 | VIDEOS_CONSTRAINTS_FIELDS: VIDEOS_CONSTRAINTS_FIELDS |
95 | } | 99 | } |
96 | 100 | ||
diff --git a/server/lib/webtorrent-process.js b/server/lib/webtorrent-process.js deleted file mode 100644 index be7ac5bb4..000000000 --- a/server/lib/webtorrent-process.js +++ /dev/null | |||
@@ -1,92 +0,0 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const WebTorrent = require('webtorrent') | ||
4 | const ipc = require('node-ipc') | ||
5 | |||
6 | function 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 | |||
92 | module.exports = webtorrent | ||
diff --git a/server/lib/webtorrent.js b/server/lib/webtorrent.js deleted file mode 100644 index bcd30139e..000000000 --- a/server/lib/webtorrent.js +++ /dev/null | |||
@@ -1,161 +0,0 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const config = require('config') | ||
4 | const ipc = require('node-ipc') | ||
5 | const pathUtils = require('path') | ||
6 | const spawn = require('electron-spawn') | ||
7 | |||
8 | const logger = require('../helpers/logger') | ||
9 | |||
10 | const electronDebug = config.get('electron.debug') | ||
11 | let host = config.get('webserver.host') | ||
12 | let port = config.get('webserver.port') | ||
13 | let nodeKey = 'webtorrentnode' + port | ||
14 | let processKey = 'webtorrentprocess' + port | ||
15 | ipc.config.silent = true | ||
16 | ipc.config.id = nodeKey | ||
17 | |||
18 | const webtorrent = { | ||
19 | add: add, | ||
20 | app: null, // Pid of the app | ||
21 | create: create, | ||
22 | remove: remove, | ||
23 | seed: seed, | ||
24 | silent: false // Useful for beautiful tests | ||
25 | } | ||
26 | |||
27 | function create (options, callback) { | ||
28 | if (typeof options === 'function') { | ||
29 | callback = options | ||
30 | options = {} | ||
31 | } | ||
32 | |||
33 | // Override options | ||
34 | if (options.host) host = options.host | ||
35 | if (options.port) { | ||
36 | port = options.port | ||
37 | nodeKey = 'webtorrentnode' + port | ||
38 | processKey = 'webtorrentprocess' + port | ||
39 | ipc.config.id = nodeKey | ||
40 | } | ||
41 | |||
42 | ipc.serve(function () { | ||
43 | if (!webtorrent.silent) logger.info('IPC server ready.') | ||
44 | |||
45 | // Run a timeout of 30s after which we exit the process | ||
46 | const timeoutWebtorrentProcess = setTimeout(function () { | ||
47 | throw new Error('Timeout : cannot run the webtorrent process. Please ensure you have electron-prebuilt npm package installed with xvfb-run.') | ||
48 | }, 30000) | ||
49 | |||
50 | ipc.server.on(processKey + '.ready', function () { | ||
51 | if (!webtorrent.silent) logger.info('Webtorrent process ready.') | ||
52 | clearTimeout(timeoutWebtorrentProcess) | ||
53 | callback() | ||
54 | }) | ||
55 | |||
56 | ipc.server.on(processKey + '.exception', function (data) { | ||
57 | throw new Error('Received exception error from webtorrent process : ' + data.exception) | ||
58 | }) | ||
59 | |||
60 | const webtorrentProcess = spawn(pathUtils.join(__dirname, 'webtorrent-process.js'), host, port, { detached: true }) | ||
61 | |||
62 | if (electronDebug === true) { | ||
63 | webtorrentProcess.stderr.on('data', function (data) { | ||
64 | logger.debug('Webtorrent process stderr: ', data.toString()) | ||
65 | }) | ||
66 | |||
67 | webtorrentProcess.stdout.on('data', function (data) { | ||
68 | logger.debug('Webtorrent process:', data.toString()) | ||
69 | }) | ||
70 | } | ||
71 | |||
72 | webtorrent.app = webtorrentProcess | ||
73 | }) | ||
74 | |||
75 | ipc.server.start() | ||
76 | } | ||
77 | |||
78 | function seed (path, callback) { | ||
79 | const extension = pathUtils.extname(path) | ||
80 | const basename = pathUtils.basename(path, extension) | ||
81 | const data = { | ||
82 | _id: basename, | ||
83 | args: { | ||
84 | path: path | ||
85 | } | ||
86 | } | ||
87 | |||
88 | if (!webtorrent.silent) logger.debug('Node wants to seed %s.', data._id) | ||
89 | |||
90 | // Finish signal | ||
91 | const eventKey = nodeKey + '.seedDone.' + data._id | ||
92 | ipc.server.on(eventKey, function listener (received) { | ||
93 | if (!webtorrent.silent) logger.debug('Process seeded torrent %s.', received.magnetUri) | ||
94 | |||
95 | // This is a fake object, we just use the magnetUri in this project | ||
96 | const torrent = { | ||
97 | magnetURI: received.magnetUri | ||
98 | } | ||
99 | |||
100 | ipc.server.off(eventKey, '*') | ||
101 | callback(torrent) | ||
102 | }) | ||
103 | |||
104 | ipc.server.broadcast(processKey + '.seed', data) | ||
105 | } | ||
106 | |||
107 | function add (magnetUri, callback) { | ||
108 | const data = { | ||
109 | _id: magnetUri, | ||
110 | args: { | ||
111 | magnetUri: magnetUri | ||
112 | } | ||
113 | } | ||
114 | |||
115 | if (!webtorrent.silent) logger.debug('Node wants to add ' + data._id) | ||
116 | |||
117 | // Finish signal | ||
118 | const eventKey = nodeKey + '.addDone.' + data._id | ||
119 | ipc.server.on(eventKey, function (received) { | ||
120 | if (!webtorrent.silent) logger.debug('Process added torrent.') | ||
121 | |||
122 | // This is a fake object, we just use the magnetUri in this project | ||
123 | const torrent = { | ||
124 | files: received.files | ||
125 | } | ||
126 | |||
127 | ipc.server.off(eventKey, '*') | ||
128 | callback(torrent) | ||
129 | }) | ||
130 | |||
131 | ipc.server.broadcast(processKey + '.add', data) | ||
132 | } | ||
133 | |||
134 | function remove (magnetUri, callback) { | ||
135 | const data = { | ||
136 | _id: magnetUri, | ||
137 | args: { | ||
138 | magnetUri: magnetUri | ||
139 | } | ||
140 | } | ||
141 | |||
142 | if (!webtorrent.silent) logger.debug('Node wants to stop seeding %s.', data._id) | ||
143 | |||
144 | // Finish signal | ||
145 | const eventKey = nodeKey + '.removeDone.' + data._id | ||
146 | ipc.server.on(eventKey, function (received) { | ||
147 | if (!webtorrent.silent) logger.debug('Process removed torrent %s.', data._id) | ||
148 | |||
149 | let err = null | ||
150 | if (received.err) err = received.err | ||
151 | |||
152 | ipc.server.off(eventKey, '*') | ||
153 | callback(err) | ||
154 | }) | ||
155 | |||
156 | ipc.server.broadcast(processKey + '.remove', data) | ||
157 | } | ||
158 | |||
159 | // --------------------------------------------------------------------------- | ||
160 | |||
161 | module.exports = webtorrent | ||
diff --git a/server/models/video.js b/server/models/video.js index 396aa505d..14e0df6f2 100644 --- a/server/models/video.js +++ b/server/models/video.js | |||
@@ -1,24 +1,27 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const config = require('config') | 3 | const config = require('config') |
4 | const eachLimit = require('async/eachLimit') | 4 | const createTorrent = require('create-torrent') |
5 | const ffmpeg = require('fluent-ffmpeg') | 5 | const ffmpeg = require('fluent-ffmpeg') |
6 | const fs = require('fs') | 6 | const fs = require('fs') |
7 | const parallel = require('async/parallel') | 7 | const parallel = require('async/parallel') |
8 | const parseTorrent = require('parse-torrent') | ||
8 | const pathUtils = require('path') | 9 | const pathUtils = require('path') |
10 | const magnet = require('magnet-uri') | ||
9 | const mongoose = require('mongoose') | 11 | const mongoose = require('mongoose') |
10 | 12 | ||
11 | const constants = require('../initializers/constants') | 13 | const constants = require('../initializers/constants') |
12 | const customValidators = require('../helpers/custom-validators') | 14 | const customValidators = require('../helpers/custom-validators') |
13 | const logger = require('../helpers/logger') | 15 | const logger = require('../helpers/logger') |
14 | const utils = require('../helpers/utils') | 16 | const utils = require('../helpers/utils') |
15 | const webtorrent = require('../lib/webtorrent') | ||
16 | 17 | ||
17 | const http = config.get('webserver.https') === true ? 'https' : 'http' | 18 | const http = config.get('webserver.https') === true ? 'https' : 'http' |
18 | const host = config.get('webserver.host') | 19 | const host = config.get('webserver.host') |
19 | const port = config.get('webserver.port') | 20 | const port = config.get('webserver.port') |
20 | const uploadsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.uploads')) | 21 | const uploadsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.uploads')) |
21 | const thumbnailsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.thumbnails')) | 22 | const thumbnailsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.thumbnails')) |
23 | const torrentsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.torrents')) | ||
24 | const webseedBaseUrl = http + '://' + host + ':' + port + constants.STATIC_PATHS.WEBSEED | ||
22 | 25 | ||
23 | // --------------------------------------------------------------------------- | 26 | // --------------------------------------------------------------------------- |
24 | 27 | ||
@@ -66,8 +69,7 @@ VideoSchema.statics = { | |||
66 | listOwned: listOwned, | 69 | listOwned: listOwned, |
67 | listRemotes: listRemotes, | 70 | listRemotes: listRemotes, |
68 | load: load, | 71 | load: load, |
69 | search: search, | 72 | search: search |
70 | seedAllExisting: seedAllExisting | ||
71 | } | 73 | } |
72 | 74 | ||
73 | VideoSchema.pre('remove', function (next) { | 75 | VideoSchema.pre('remove', function (next) { |
@@ -103,8 +105,21 @@ VideoSchema.pre('save', function (next) { | |||
103 | this.podUrl = http + '://' + host + ':' + port | 105 | this.podUrl = http + '://' + host + ':' + port |
104 | 106 | ||
105 | tasks.push( | 107 | tasks.push( |
108 | // TODO: refractoring | ||
106 | function (callback) { | 109 | function (callback) { |
107 | seed(videoPath, callback) | 110 | createTorrent(videoPath, { announceList: [ [ 'ws://' + host + ':' + port + '/tracker/socket' ] ], urlList: [ webseedBaseUrl + video.filename ] }, function (err, torrent) { |
111 | if (err) return callback(err) | ||
112 | |||
113 | fs.writeFile(torrentsDir + video.filename + '.torrent', torrent, function (err) { | ||
114 | if (err) return callback(err) | ||
115 | |||
116 | const parsedTorrent = parseTorrent(torrent) | ||
117 | parsedTorrent.xs = video.podUrl + constants.STATIC_PATHS.TORRENTS + video.filename + '.torrent' | ||
118 | video.magnetUri = magnet.encode(parsedTorrent) | ||
119 | |||
120 | callback(null) | ||
121 | }) | ||
122 | }) | ||
108 | }, | 123 | }, |
109 | function (callback) { | 124 | function (callback) { |
110 | createThumbnail(videoPath, callback) | 125 | createThumbnail(videoPath, callback) |
@@ -114,7 +129,6 @@ VideoSchema.pre('save', function (next) { | |||
114 | parallel(tasks, function (err, results) { | 129 | parallel(tasks, function (err, results) { |
115 | if (err) return next(err) | 130 | if (err) return next(err) |
116 | 131 | ||
117 | video.magnetUri = results[0].magnetURI | ||
118 | video.thumbnail = results[1] | 132 | video.thumbnail = results[1] |
119 | 133 | ||
120 | return next() | 134 | return next() |
@@ -149,7 +163,7 @@ function toFormatedJSON () { | |||
149 | author: this.author, | 163 | author: this.author, |
150 | duration: this.duration, | 164 | duration: this.duration, |
151 | tags: this.tags, | 165 | tags: this.tags, |
152 | thumbnailPath: constants.THUMBNAILS_STATIC_PATH + '/' + this.thumbnail, | 166 | thumbnailPath: constants.STATIC_PATHS.THUMBNAILS + '/' + this.thumbnail, |
153 | createdDate: this.createdDate | 167 | createdDate: this.createdDate |
154 | } | 168 | } |
155 | 169 | ||
@@ -231,17 +245,6 @@ function search (value, field, start, count, sort, callback) { | |||
231 | findWithCount.call(this, query, start, count, sort, callback) | 245 | findWithCount.call(this, query, start, count, sort, callback) |
232 | } | 246 | } |
233 | 247 | ||
234 | function seedAllExisting (callback) { | ||
235 | listOwned.call(this, function (err, videos) { | ||
236 | if (err) return callback(err) | ||
237 | |||
238 | eachLimit(videos, constants.SEEDS_IN_PARALLEL, function (video, callbackEach) { | ||
239 | const videoPath = pathUtils.join(uploadsDir, video.filename) | ||
240 | seed(videoPath, callbackEach) | ||
241 | }, callback) | ||
242 | }) | ||
243 | } | ||
244 | |||
245 | // --------------------------------------------------------------------------- | 248 | // --------------------------------------------------------------------------- |
246 | 249 | ||
247 | function findWithCount (query, start, count, sort, callback) { | 250 | function findWithCount (query, start, count, sort, callback) { |
@@ -273,12 +276,7 @@ function removeFile (video, callback) { | |||
273 | 276 | ||
274 | // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process | 277 | // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process |
275 | function removeTorrent (video, callback) { | 278 | function removeTorrent (video, callback) { |
276 | try { | 279 | fs.unlink(torrentsDir + video.filename + '.torrent') |
277 | webtorrent.remove(video.magnetUri, callback) | ||
278 | } catch (err) { | ||
279 | logger.warn('Cannot remove the torrent from WebTorrent', { err: err }) | ||
280 | return callback(null) | ||
281 | } | ||
282 | } | 280 | } |
283 | 281 | ||
284 | function createThumbnail (videoPath, callback) { | 282 | function createThumbnail (videoPath, callback) { |
@@ -296,16 +294,6 @@ function createThumbnail (videoPath, callback) { | |||
296 | }) | 294 | }) |
297 | } | 295 | } |
298 | 296 | ||
299 | function seed (path, callback) { | ||
300 | logger.info('Seeding %s...', path) | ||
301 | |||
302 | webtorrent.seed(path, function (torrent) { | ||
303 | logger.info('%s seeded (%s).', path, torrent.magnetURI) | ||
304 | |||
305 | return callback(null, torrent) | ||
306 | }) | ||
307 | } | ||
308 | |||
309 | function generateThumbnailFromBase64 (data, callback) { | 297 | function generateThumbnailFromBase64 (data, callback) { |
310 | // Creating the thumbnail for this remote video | 298 | // Creating the thumbnail for this remote video |
311 | utils.generateRandomString(16, function (err, randomString) { | 299 | utils.generateRandomString(16, function (err, randomString) { |