diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-07-26 22:30:46 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-07-27 21:21:57 +0200 |
commit | 052937db8a8d282eccdbdf38d487ed8d85d3c0a7 (patch) | |
tree | da84e08fbe3569c2122c9b733e22768980750744 /server/models | |
parent | 71d3476b825571376b06498326d138685493fb62 (diff) | |
download | PeerTube-052937db8a8d282eccdbdf38d487ed8d85d3c0a7.tar.gz PeerTube-052937db8a8d282eccdbdf38d487ed8d85d3c0a7.tar.zst PeerTube-052937db8a8d282eccdbdf38d487ed8d85d3c0a7.zip |
First draft using only webseed for server
Diffstat (limited to 'server/models')
-rw-r--r-- | server/models/video.js | 56 |
1 files changed, 22 insertions, 34 deletions
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) { |