aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/video.js')
-rw-r--r--server/models/video.js56
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
3const config = require('config') 3const config = require('config')
4const eachLimit = require('async/eachLimit') 4const createTorrent = require('create-torrent')
5const ffmpeg = require('fluent-ffmpeg') 5const ffmpeg = require('fluent-ffmpeg')
6const fs = require('fs') 6const fs = require('fs')
7const parallel = require('async/parallel') 7const parallel = require('async/parallel')
8const parseTorrent = require('parse-torrent')
8const pathUtils = require('path') 9const pathUtils = require('path')
10const magnet = require('magnet-uri')
9const mongoose = require('mongoose') 11const mongoose = require('mongoose')
10 12
11const constants = require('../initializers/constants') 13const constants = require('../initializers/constants')
12const customValidators = require('../helpers/custom-validators') 14const customValidators = require('../helpers/custom-validators')
13const logger = require('../helpers/logger') 15const logger = require('../helpers/logger')
14const utils = require('../helpers/utils') 16const utils = require('../helpers/utils')
15const webtorrent = require('../lib/webtorrent')
16 17
17const http = config.get('webserver.https') === true ? 'https' : 'http' 18const http = config.get('webserver.https') === true ? 'https' : 'http'
18const host = config.get('webserver.host') 19const host = config.get('webserver.host')
19const port = config.get('webserver.port') 20const port = config.get('webserver.port')
20const uploadsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.uploads')) 21const uploadsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.uploads'))
21const thumbnailsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.thumbnails')) 22const thumbnailsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.thumbnails'))
23const torrentsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.torrents'))
24const 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
73VideoSchema.pre('remove', function (next) { 75VideoSchema.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
234function 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
247function findWithCount (query, start, count, sort, callback) { 250function 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
275function removeTorrent (video, callback) { 278function 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
284function createThumbnail (videoPath, callback) { 282function createThumbnail (videoPath, callback) {
@@ -296,16 +294,6 @@ function createThumbnail (videoPath, callback) {
296 }) 294 })
297} 295}
298 296
299function 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
309function generateThumbnailFromBase64 (data, callback) { 297function 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) {