diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-10-07 10:10:00 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-10-07 10:10:00 +0200 |
commit | aff36eb063573532498979c83be1c279c84f2d53 (patch) | |
tree | 3f79dc7373689f0493e80a6e501225fd0cd7aa26 | |
parent | c3117f13d599f37693f937865b6e84eb1f72d5c4 (diff) | |
parent | a6375e69668ea42e19531c6bc68dcd37f3f7cbd7 (diff) | |
download | PeerTube-aff36eb063573532498979c83be1c279c84f2d53.tar.gz PeerTube-aff36eb063573532498979c83be1c279c84f2d53.tar.zst PeerTube-aff36eb063573532498979c83be1c279c84f2d53.zip |
Merge branch 'webseed'
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | client/src/app/shared/auth/auth.service.ts | 1 | ||||
-rw-r--r-- | client/src/app/videos/video-watch/video-watch.component.ts | 1 | ||||
-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 | 160 | ||||
-rw-r--r-- | server/models/video.js | 63 |
16 files changed, 69 insertions, 324 deletions
diff --git a/.gitignore b/.gitignore index 18a2e808d..7ca89dca8 100644 --- a/.gitignore +++ b/.gitignore | |||
@@ -14,7 +14,11 @@ uploads | |||
14 | thumbnails | 14 | thumbnails |
15 | config/production.yaml | 15 | config/production.yaml |
16 | ffmpeg | 16 | ffmpeg |
17 | <<<<<<< HEAD | ||
18 | torrents | ||
19 | ======= | ||
17 | .tags | 20 | .tags |
18 | *.sublime-project | 21 | *.sublime-project |
19 | *.sublime-workspace | 22 | *.sublime-workspace |
20 | torrents/ | 23 | torrents/ |
24 | >>>>>>> master | ||
diff --git a/client/src/app/shared/auth/auth.service.ts b/client/src/app/shared/auth/auth.service.ts index c57486d9a..a30c79c86 100644 --- a/client/src/app/shared/auth/auth.service.ts +++ b/client/src/app/shared/auth/auth.service.ts | |||
@@ -123,6 +123,7 @@ export class AuthService { | |||
123 | logout() { | 123 | logout() { |
124 | // TODO: make an HTTP request to revoke the tokens | 124 | // TODO: make an HTTP request to revoke the tokens |
125 | this.user = null; | 125 | this.user = null; |
126 | |||
126 | AuthUser.flush(); | 127 | AuthUser.flush(); |
127 | 128 | ||
128 | this.setStatus(AuthStatus.LoggedOut); | 129 | this.setStatus(AuthStatus.LoggedOut); |
diff --git a/client/src/app/videos/video-watch/video-watch.component.ts b/client/src/app/videos/video-watch/video-watch.component.ts index ada20fc96..239e24c99 100644 --- a/client/src/app/videos/video-watch/video-watch.component.ts +++ b/client/src/app/videos/video-watch/video-watch.component.ts | |||
@@ -68,6 +68,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy { | |||
68 | ngOnDestroy() { | 68 | ngOnDestroy() { |
69 | console.log('Removing video from webtorrent.'); | 69 | console.log('Removing video from webtorrent.'); |
70 | clearInterval(this.torrentInfosInterval); | 70 | clearInterval(this.torrentInfosInterval); |
71 | clearTimeout(this.errorTimer); | ||
71 | this.webTorrentService.remove(this.video.magnetUri); | 72 | this.webTorrentService.remove(this.video.magnetUri); |
72 | 73 | ||
73 | this.sub.unsubscribe(); | 74 | this.sub.unsubscribe(); |
diff --git a/config/default.yaml b/config/default.yaml index d32d3e17b..b44be31b0 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 | electron: | 22 | electron: |
22 | debug: false | 23 | debug: false |
diff --git a/config/test-1.yaml b/config/test-1.yaml index 0a8dd3937..a59566cc4 100644 --- a/config/test-1.yaml +++ b/config/test-1.yaml | |||
@@ -14,3 +14,4 @@ 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/' | ||
diff --git a/config/test-2.yaml b/config/test-2.yaml index 40f410559..1b937898f 100644 --- a/config/test-2.yaml +++ b/config/test-2.yaml | |||
@@ -14,3 +14,4 @@ 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/' | ||
diff --git a/config/test-3.yaml b/config/test-3.yaml index 87b335228..e522c13e7 100644 --- a/config/test-3.yaml +++ b/config/test-3.yaml | |||
@@ -14,3 +14,4 @@ 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/' | ||
diff --git a/config/test-4.yaml b/config/test-4.yaml index 22abc0a58..e30cd7978 100644 --- a/config/test-4.yaml +++ b/config/test-4.yaml | |||
@@ -14,3 +14,4 @@ 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/' | ||
diff --git a/config/test-5.yaml b/config/test-5.yaml index af5619994..3a54599f5 100644 --- a/config/test-5.yaml +++ b/config/test-5.yaml | |||
@@ -14,3 +14,4 @@ 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/' | ||
diff --git a/config/test-6.yaml b/config/test-6.yaml index 5b8bf306b..31608add2 100644 --- a/config/test-6.yaml +++ b/config/test-6.yaml | |||
@@ -14,3 +14,4 @@ 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/' | ||
diff --git a/package.json b/package.json index 9a844baaf..59c7a4332 100644 --- a/package.json +++ b/package.json | |||
@@ -40,27 +40,27 @@ | |||
40 | "body-parser": "^1.12.4", | 40 | "body-parser": "^1.12.4", |
41 | "concurrently": "^2.0.0", | 41 | "concurrently": "^2.0.0", |
42 | "config": "^1.14.0", | 42 | "config": "^1.14.0", |
43 | "create-torrent": "^3.24.5", | ||
43 | "debug": "^2.2.0", | 44 | "debug": "^2.2.0", |
44 | "dezalgo": "^1.0.3", | 45 | "dezalgo": "^1.0.3", |
45 | "electron-spawn": "https://github.com/Chocobozzz/electron-spawn", | ||
46 | "express": "^4.12.4", | 46 | "express": "^4.12.4", |
47 | "express-oauth-server": "https://github.com/Chocobozzz/express-oauth-server", | 47 | "express-oauth-server": "https://github.com/Chocobozzz/express-oauth-server", |
48 | "express-validator": "^2.11.0", | 48 | "express-validator": "^2.11.0", |
49 | "fluent-ffmpeg": "^2.1.0", | 49 | "fluent-ffmpeg": "^2.1.0", |
50 | "js-yaml": "^3.5.4", | 50 | "js-yaml": "^3.5.4", |
51 | "lodash": "^4.11.1", | 51 | "lodash": "^4.11.1", |
52 | "magnet-uri": "^5.1.4", | ||
52 | "mkdirp": "^0.5.1", | 53 | "mkdirp": "^0.5.1", |
53 | "mongoose": "^4.0.5", | 54 | "mongoose": "^4.0.5", |
54 | "morgan": "^1.5.3", | 55 | "morgan": "^1.5.3", |
55 | "multer": "^1.1.0", | 56 | "multer": "^1.1.0", |
56 | "node-ipc": "8.9.0", | ||
57 | "openssl-wrapper": "^0.3.4", | 57 | "openssl-wrapper": "^0.3.4", |
58 | "parse-torrent": "^5.8.0", | ||
58 | "password-generator": "^2.0.2", | 59 | "password-generator": "^2.0.2", |
59 | "request": "^2.57.0", | 60 | "request": "^2.57.0", |
60 | "request-replay": "^1.0.2", | 61 | "request-replay": "^1.0.2", |
61 | "scripty": "^1.5.0", | 62 | "scripty": "^1.5.0", |
62 | "ursa": "^0.9.1", | 63 | "ursa": "^0.9.1", |
63 | "webtorrent": "^0.96.0", | ||
64 | "winston": "^2.1.1", | 64 | "winston": "^2.1.1", |
65 | "ws": "^1.1.1" | 65 | "ws": "^1.1.1" |
66 | }, | 66 | }, |
@@ -35,10 +35,7 @@ const installer = require('./server/initializers/installer') | |||
35 | const migrator = require('./server/initializers/migrator') | 35 | const migrator = require('./server/initializers/migrator') |
36 | const mongoose = require('mongoose') | 36 | const mongoose = require('mongoose') |
37 | const routes = require('./server/controllers') | 37 | const routes = require('./server/controllers') |
38 | const utils = require('./server/helpers/utils') | ||
39 | const webtorrent = require('./server/lib/webtorrent') | ||
40 | const Request = mongoose.model('Request') | 38 | const Request = mongoose.model('Request') |
41 | const Video = mongoose.model('Video') | ||
42 | 39 | ||
43 | // Get configurations | 40 | // Get configurations |
44 | const port = config.get('listen.port') | 41 | const port = config.get('listen.port') |
@@ -76,9 +73,16 @@ app.use('/client/*', function (req, res, next) { | |||
76 | res.sendStatus(404) | 73 | res.sendStatus(404) |
77 | }) | 74 | }) |
78 | 75 | ||
76 | const torrentsPhysicalPath = path.join(__dirname, config.get('storage.torrents')) | ||
77 | app.use(constants.STATIC_PATHS.TORRENTS, express.static(torrentsPhysicalPath, { maxAge: 0 })) | ||
78 | |||
79 | // Uploads path for webseeding | ||
80 | const uploadsPhysicalPath = path.join(__dirname, config.get('storage.uploads')) | ||
81 | app.use(constants.STATIC_PATHS.WEBSEED, express.static(uploadsPhysicalPath, { maxAge: 0 })) | ||
82 | |||
79 | // Thumbnails path for express | 83 | // Thumbnails path for express |
80 | const thumbnailsPhysicalPath = path.join(__dirname, config.get('storage.thumbnails')) | 84 | const thumbnailsPhysicalPath = path.join(__dirname, config.get('storage.thumbnails')) |
81 | app.use(constants.THUMBNAILS_STATIC_PATH, express.static(thumbnailsPhysicalPath, { maxAge: 0 })) | 85 | app.use(constants.STATIC_PATHS.THUMBNAILS, express.static(thumbnailsPhysicalPath, { maxAge: 0 })) |
82 | 86 | ||
83 | // Client application | 87 | // Client application |
84 | app.use('/*', function (req, res, next) { | 88 | app.use('/*', function (req, res, next) { |
@@ -129,33 +133,14 @@ installer.installApplication(function (err) { | |||
129 | migrator.migrate(function (err) { | 133 | migrator.migrate(function (err) { |
130 | if (err) throw err | 134 | if (err) throw err |
131 | 135 | ||
132 | // Create/activate the webtorrent module | 136 | // ----------- Make the server listening ----------- |
133 | webtorrent.create(function () { | 137 | server.listen(port, function () { |
134 | function cleanForExit () { | 138 | // Activate the pool requests |
135 | utils.cleanForExit(webtorrent.app) | 139 | Request.activate() |
136 | } | 140 | |
137 | 141 | logger.info('Seeded all the videos') | |
138 | function exitGracefullyOnSignal () { | 142 | logger.info('Server listening on port %d', port) |
139 | process.exit(-1) | 143 | app.emit('ready') |
140 | } | ||
141 | |||
142 | process.on('exit', cleanForExit) | ||
143 | process.on('SIGINT', exitGracefullyOnSignal) | ||
144 | process.on('SIGTERM', exitGracefullyOnSignal) | ||
145 | |||
146 | // ----------- Make the server listening ----------- | ||
147 | server.listen(port, function () { | ||
148 | // Activate the pool requests | ||
149 | Request.activate() | ||
150 | |||
151 | Video.seedAllExisting(function (err) { | ||
152 | if (err) throw err | ||
153 | |||
154 | logger.info('Seeded all the videos') | ||
155 | logger.info('Server listening on port %d', port) | ||
156 | app.emit('ready') | ||
157 | }) | ||
158 | }) | ||
159 | }) | 144 | }) |
160 | }) | 145 | }) |
161 | }) | 146 | }) |
diff --git a/server/initializers/constants.js b/server/initializers/constants.js index b1d033377..be2e3e943 100644 --- a/server/initializers/constants.js +++ b/server/initializers/constants.js | |||
@@ -122,12 +122,16 @@ const RETRY_REQUESTS = 5 | |||
122 | // Password encryption | 122 | // Password encryption |
123 | const BCRYPT_SALT_SIZE = 10 | 123 | const BCRYPT_SALT_SIZE = 10 |
124 | 124 | ||
125 | // Express static paths (router) | ||
126 | const STATIC_PATHS = { | ||
127 | THUMBNAILS: '/static/thumbnails', | ||
128 | TORRENTS: '/static/torrents/', | ||
129 | WEBSEED: '/static/webseed/' | ||
130 | } | ||
131 | |||
125 | // Videos thumbnail size | 132 | // Videos thumbnail size |
126 | const THUMBNAILS_SIZE = '200x110' | 133 | const THUMBNAILS_SIZE = '200x110' |
127 | 134 | ||
128 | // Path for access to thumbnails with express router | ||
129 | const THUMBNAILS_STATIC_PATH = '/static/thumbnails' | ||
130 | |||
131 | const USER_ROLES = { | 135 | const USER_ROLES = { |
132 | ADMIN: 'admin', | 136 | ADMIN: 'admin', |
133 | USER: 'user' | 137 | USER: 'user' |
@@ -166,8 +170,8 @@ module.exports = { | |||
166 | SEARCHABLE_COLUMNS, | 170 | SEARCHABLE_COLUMNS, |
167 | SEEDS_IN_PARALLEL, | 171 | SEEDS_IN_PARALLEL, |
168 | SORTABLE_COLUMNS, | 172 | SORTABLE_COLUMNS, |
173 | STATIC_PATHS, | ||
169 | THUMBNAILS_SIZE, | 174 | THUMBNAILS_SIZE, |
170 | THUMBNAILS_STATIC_PATH, | ||
171 | USER_ROLES | 175 | USER_ROLES |
172 | } | 176 | } |
173 | 177 | ||
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 dde046828..000000000 --- a/server/lib/webtorrent.js +++ /dev/null | |||
@@ -1,160 +0,0 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const ipc = require('node-ipc') | ||
4 | const pathUtils = require('path') | ||
5 | const spawn = require('electron-spawn') | ||
6 | |||
7 | const constants = require('../initializers/constants') | ||
8 | const logger = require('../helpers/logger') | ||
9 | |||
10 | let host = constants.CONFIG.WEBSERVER.HOST | ||
11 | let port = constants.CONFIG.WEBSERVER.PORT | ||
12 | let nodeKey = 'webtorrentnode' + port | ||
13 | let processKey = 'webtorrentprocess' + port | ||
14 | ipc.config.silent = true | ||
15 | ipc.config.id = nodeKey | ||
16 | |||
17 | const webtorrent = { | ||
18 | add: add, | ||
19 | app: null, // Pid of the app | ||
20 | create: create, | ||
21 | remove: remove, | ||
22 | seed: seed, | ||
23 | silent: false // Useful for beautiful tests | ||
24 | } | ||
25 | |||
26 | function create (options, callback) { | ||
27 | if (typeof options === 'function') { | ||
28 | callback = options | ||
29 | options = {} | ||
30 | } | ||
31 | |||
32 | // Override options | ||
33 | if (options.host) host = options.host | ||
34 | if (options.port) { | ||
35 | port = options.port | ||
36 | nodeKey = 'webtorrentnode' + port | ||
37 | processKey = 'webtorrentprocess' + port | ||
38 | ipc.config.id = nodeKey | ||
39 | } | ||
40 | |||
41 | ipc.serve(function () { | ||
42 | if (!webtorrent.silent) logger.info('IPC server ready.') | ||
43 | |||
44 | // Run a timeout of 30s after which we exit the process | ||
45 | const timeoutWebtorrentProcess = setTimeout(function () { | ||
46 | throw new Error('Timeout : cannot run the webtorrent process. Please ensure you have electron npm package installed with xvfb-run.') | ||
47 | }, 30000) | ||
48 | |||
49 | ipc.server.on(processKey + '.ready', function () { | ||
50 | if (!webtorrent.silent) logger.info('Webtorrent process ready.') | ||
51 | clearTimeout(timeoutWebtorrentProcess) | ||
52 | callback() | ||
53 | }) | ||
54 | |||
55 | ipc.server.on(processKey + '.exception', function (data) { | ||
56 | throw new Error('Received exception error from webtorrent process : ' + data.exception) | ||
57 | }) | ||
58 | |||
59 | const webtorrentProcess = spawn(pathUtils.join(__dirname, 'webtorrent-process.js'), host, port, { detached: true }) | ||
60 | |||
61 | if (constants.CONFIG.ELECTRON.DEBUG === true) { | ||
62 | webtorrentProcess.stderr.on('data', function (data) { | ||
63 | logger.debug('Webtorrent process stderr: ', data.toString()) | ||
64 | }) | ||
65 | |||
66 | webtorrentProcess.stdout.on('data', function (data) { | ||
67 | logger.debug('Webtorrent process:', data.toString()) | ||
68 | }) | ||
69 | } | ||
70 | |||
71 | webtorrent.app = webtorrentProcess | ||
72 | }) | ||
73 | |||
74 | ipc.server.start() | ||
75 | } | ||
76 | |||
77 | function seed (path, callback) { | ||
78 | const extension = pathUtils.extname(path) | ||
79 | const basename = pathUtils.basename(path, extension) | ||
80 | const data = { | ||
81 | _id: basename, | ||
82 | args: { | ||
83 | path: path | ||
84 | } | ||
85 | } | ||
86 | |||
87 | if (!webtorrent.silent) logger.debug('Node wants to seed %s.', data._id) | ||
88 | |||
89 | // Finish signal | ||
90 | const eventKey = nodeKey + '.seedDone.' + data._id | ||
91 | ipc.server.on(eventKey, function listener (received) { | ||
92 | if (!webtorrent.silent) logger.debug('Process seeded torrent %s.', received.magnetUri) | ||
93 | |||
94 | // This is a fake object, we just use the magnetUri in this project | ||
95 | const torrent = { | ||
96 | magnetURI: received.magnetUri | ||
97 | } | ||
98 | |||
99 | ipc.server.off(eventKey, '*') | ||
100 | callback(torrent) | ||
101 | }) | ||
102 | |||
103 | ipc.server.broadcast(processKey + '.seed', data) | ||
104 | } | ||
105 | |||
106 | function add (magnetUri, callback) { | ||
107 | const data = { | ||
108 | _id: magnetUri, | ||
109 | args: { | ||
110 | magnetUri: magnetUri | ||
111 | } | ||
112 | } | ||
113 | |||
114 | if (!webtorrent.silent) logger.debug('Node wants to add ' + data._id) | ||
115 | |||
116 | // Finish signal | ||
117 | const eventKey = nodeKey + '.addDone.' + data._id | ||
118 | ipc.server.on(eventKey, function (received) { | ||
119 | if (!webtorrent.silent) logger.debug('Process added torrent.') | ||
120 | |||
121 | // This is a fake object, we just use the magnetUri in this project | ||
122 | const torrent = { | ||
123 | files: received.files | ||
124 | } | ||
125 | |||
126 | ipc.server.off(eventKey, '*') | ||
127 | callback(torrent) | ||
128 | }) | ||
129 | |||
130 | ipc.server.broadcast(processKey + '.add', data) | ||
131 | } | ||
132 | |||
133 | function remove (magnetUri, callback) { | ||
134 | const data = { | ||
135 | _id: magnetUri, | ||
136 | args: { | ||
137 | magnetUri: magnetUri | ||
138 | } | ||
139 | } | ||
140 | |||
141 | if (!webtorrent.silent) logger.debug('Node wants to stop seeding %s.', data._id) | ||
142 | |||
143 | // Finish signal | ||
144 | const eventKey = nodeKey + '.removeDone.' + data._id | ||
145 | ipc.server.on(eventKey, function (received) { | ||
146 | if (!webtorrent.silent) logger.debug('Process removed torrent %s.', data._id) | ||
147 | |||
148 | let err = null | ||
149 | if (received.err) err = received.err | ||
150 | |||
151 | ipc.server.off(eventKey, '*') | ||
152 | callback(err) | ||
153 | }) | ||
154 | |||
155 | ipc.server.broadcast(processKey + '.remove', data) | ||
156 | } | ||
157 | |||
158 | // --------------------------------------------------------------------------- | ||
159 | |||
160 | module.exports = webtorrent | ||
diff --git a/server/models/video.js b/server/models/video.js index b9999c8f6..7d073cffa 100644 --- a/server/models/video.js +++ b/server/models/video.js | |||
@@ -1,10 +1,13 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const eachLimit = require('async/eachLimit') | 3 | const config = require('config') |
4 | const createTorrent = require('create-torrent') | ||
4 | const ffmpeg = require('fluent-ffmpeg') | 5 | const ffmpeg = require('fluent-ffmpeg') |
5 | const fs = require('fs') | 6 | const fs = require('fs') |
6 | const parallel = require('async/parallel') | 7 | const parallel = require('async/parallel') |
8 | const parseTorrent = require('parse-torrent') | ||
7 | const pathUtils = require('path') | 9 | const pathUtils = require('path') |
10 | const magnet = require('magnet-uri') | ||
8 | const mongoose = require('mongoose') | 11 | const mongoose = require('mongoose') |
9 | 12 | ||
10 | const constants = require('../initializers/constants') | 13 | const constants = require('../initializers/constants') |
@@ -12,7 +15,14 @@ const customVideosValidators = require('../helpers/custom-validators').videos | |||
12 | const logger = require('../helpers/logger') | 15 | const logger = require('../helpers/logger') |
13 | const modelUtils = require('./utils') | 16 | const modelUtils = require('./utils') |
14 | const utils = require('../helpers/utils') | 17 | const utils = require('../helpers/utils') |
15 | const webtorrent = require('../lib/webtorrent') | 18 | |
19 | const http = config.get('webserver.https') === true ? 'https' : 'http' | ||
20 | const host = config.get('webserver.host') | ||
21 | const port = config.get('webserver.port') | ||
22 | const uploadsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.uploads')) | ||
23 | const thumbnailsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.thumbnails')) | ||
24 | const torrentsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.torrents')) | ||
25 | const webseedBaseUrl = http + '://' + host + ':' + port + constants.STATIC_PATHS.WEBSEED | ||
16 | 26 | ||
17 | // --------------------------------------------------------------------------- | 27 | // --------------------------------------------------------------------------- |
18 | 28 | ||
@@ -61,8 +71,7 @@ VideoSchema.statics = { | |||
61 | listOwnedByAuthor, | 71 | listOwnedByAuthor, |
62 | listRemotes, | 72 | listRemotes, |
63 | load, | 73 | load, |
64 | search, | 74 | search |
65 | seedAllExisting | ||
66 | } | 75 | } |
67 | 76 | ||
68 | VideoSchema.pre('remove', function (next) { | 77 | VideoSchema.pre('remove', function (next) { |
@@ -98,8 +107,21 @@ VideoSchema.pre('save', function (next) { | |||
98 | this.podUrl = constants.CONFIG.WEBSERVER.URL | 107 | this.podUrl = constants.CONFIG.WEBSERVER.URL |
99 | 108 | ||
100 | tasks.push( | 109 | tasks.push( |
110 | // TODO: refractoring | ||
101 | function (callback) { | 111 | function (callback) { |
102 | seed(videoPath, callback) | 112 | createTorrent(videoPath, { announceList: [ [ 'ws://' + host + ':' + port + '/tracker/socket' ] ], urlList: [ webseedBaseUrl + video.filename ] }, function (err, torrent) { |
113 | if (err) return callback(err) | ||
114 | |||
115 | fs.writeFile(torrentsDir + video.filename + '.torrent', torrent, function (err) { | ||
116 | if (err) return callback(err) | ||
117 | |||
118 | const parsedTorrent = parseTorrent(torrent) | ||
119 | parsedTorrent.xs = video.podUrl + constants.STATIC_PATHS.TORRENTS + video.filename + '.torrent' | ||
120 | video.magnetUri = magnet.encode(parsedTorrent) | ||
121 | |||
122 | callback(null) | ||
123 | }) | ||
124 | }) | ||
103 | }, | 125 | }, |
104 | function (callback) { | 126 | function (callback) { |
105 | createThumbnail(videoPath, callback) | 127 | createThumbnail(videoPath, callback) |
@@ -109,7 +131,6 @@ VideoSchema.pre('save', function (next) { | |||
109 | parallel(tasks, function (err, results) { | 131 | parallel(tasks, function (err, results) { |
110 | if (err) return next(err) | 132 | if (err) return next(err) |
111 | 133 | ||
112 | video.magnetUri = results[0].magnetURI | ||
113 | video.thumbnail = results[1] | 134 | video.thumbnail = results[1] |
114 | 135 | ||
115 | return next() | 136 | return next() |
@@ -144,7 +165,7 @@ function toFormatedJSON () { | |||
144 | author: this.author, | 165 | author: this.author, |
145 | duration: this.duration, | 166 | duration: this.duration, |
146 | tags: this.tags, | 167 | tags: this.tags, |
147 | thumbnailPath: constants.THUMBNAILS_STATIC_PATH + '/' + this.thumbnail, | 168 | thumbnailPath: constants.STATIC_PATHS.THUMBNAILS + '/' + this.thumbnail, |
148 | createdDate: this.createdDate | 169 | createdDate: this.createdDate |
149 | } | 170 | } |
150 | 171 | ||
@@ -230,17 +251,6 @@ function search (value, field, start, count, sort, callback) { | |||
230 | modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback) | 251 | modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback) |
231 | } | 252 | } |
232 | 253 | ||
233 | function seedAllExisting (callback) { | ||
234 | listOwned.call(this, function (err, videos) { | ||
235 | if (err) return callback(err) | ||
236 | |||
237 | eachLimit(videos, constants.SEEDS_IN_PARALLEL, function (video, callbackEach) { | ||
238 | const videoPath = pathUtils.join(constants.CONFIG.STORAGE.UPLOAD_DIR, video.filename) | ||
239 | seed(videoPath, callbackEach) | ||
240 | }, callback) | ||
241 | }) | ||
242 | } | ||
243 | |||
244 | // --------------------------------------------------------------------------- | 254 | // --------------------------------------------------------------------------- |
245 | 255 | ||
246 | function removeThumbnail (video, callback) { | 256 | function removeThumbnail (video, callback) { |
@@ -253,12 +263,7 @@ function removeFile (video, callback) { | |||
253 | 263 | ||
254 | // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process | 264 | // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process |
255 | function removeTorrent (video, callback) { | 265 | function removeTorrent (video, callback) { |
256 | try { | 266 | fs.unlink(torrentsDir + video.filename + '.torrent') |
257 | webtorrent.remove(video.magnetUri, callback) | ||
258 | } catch (err) { | ||
259 | logger.warn('Cannot remove the torrent from WebTorrent', { err: err }) | ||
260 | return callback(null) | ||
261 | } | ||
262 | } | 267 | } |
263 | 268 | ||
264 | function createThumbnail (videoPath, callback) { | 269 | function createThumbnail (videoPath, callback) { |
@@ -276,16 +281,6 @@ function createThumbnail (videoPath, callback) { | |||
276 | }) | 281 | }) |
277 | } | 282 | } |
278 | 283 | ||
279 | function seed (path, callback) { | ||
280 | logger.info('Seeding %s...', path) | ||
281 | |||
282 | webtorrent.seed(path, function (torrent) { | ||
283 | logger.info('%s seeded (%s).', path, torrent.magnetURI) | ||
284 | |||
285 | return callback(null, torrent) | ||
286 | }) | ||
287 | } | ||
288 | |||
289 | function generateThumbnailFromBase64 (data, callback) { | 284 | function generateThumbnailFromBase64 (data, callback) { |
290 | // Creating the thumbnail for this remote video | 285 | // Creating the thumbnail for this remote video |
291 | utils.generateRandomString(16, function (err, randomString) { | 286 | utils.generateRandomString(16, function (err, randomString) { |