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 /server/models | |
parent | c3117f13d599f37693f937865b6e84eb1f72d5c4 (diff) | |
parent | a6375e69668ea42e19531c6bc68dcd37f3f7cbd7 (diff) | |
download | PeerTube-aff36eb063573532498979c83be1c279c84f2d53.tar.gz PeerTube-aff36eb063573532498979c83be1c279c84f2d53.tar.zst PeerTube-aff36eb063573532498979c83be1c279c84f2d53.zip |
Merge branch 'webseed'
Diffstat (limited to 'server/models')
-rw-r--r-- | server/models/video.js | 63 |
1 files changed, 29 insertions, 34 deletions
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) { |