aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-10-07 10:10:00 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-10-07 10:10:00 +0200
commitaff36eb063573532498979c83be1c279c84f2d53 (patch)
tree3f79dc7373689f0493e80a6e501225fd0cd7aa26 /server/models
parentc3117f13d599f37693f937865b6e84eb1f72d5c4 (diff)
parenta6375e69668ea42e19531c6bc68dcd37f3f7cbd7 (diff)
downloadPeerTube-aff36eb063573532498979c83be1c279c84f2d53.tar.gz
PeerTube-aff36eb063573532498979c83be1c279c84f2d53.tar.zst
PeerTube-aff36eb063573532498979c83be1c279c84f2d53.zip
Merge branch 'webseed'
Diffstat (limited to 'server/models')
-rw-r--r--server/models/video.js63
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
3const eachLimit = require('async/eachLimit') 3const config = require('config')
4const createTorrent = require('create-torrent')
4const ffmpeg = require('fluent-ffmpeg') 5const ffmpeg = require('fluent-ffmpeg')
5const fs = require('fs') 6const fs = require('fs')
6const parallel = require('async/parallel') 7const parallel = require('async/parallel')
8const parseTorrent = require('parse-torrent')
7const pathUtils = require('path') 9const pathUtils = require('path')
10const magnet = require('magnet-uri')
8const mongoose = require('mongoose') 11const mongoose = require('mongoose')
9 12
10const constants = require('../initializers/constants') 13const constants = require('../initializers/constants')
@@ -12,7 +15,14 @@ const customVideosValidators = require('../helpers/custom-validators').videos
12const logger = require('../helpers/logger') 15const logger = require('../helpers/logger')
13const modelUtils = require('./utils') 16const modelUtils = require('./utils')
14const utils = require('../helpers/utils') 17const utils = require('../helpers/utils')
15const webtorrent = require('../lib/webtorrent') 18
19const http = config.get('webserver.https') === true ? 'https' : 'http'
20const host = config.get('webserver.host')
21const port = config.get('webserver.port')
22const uploadsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.uploads'))
23const thumbnailsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.thumbnails'))
24const torrentsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.torrents'))
25const 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
68VideoSchema.pre('remove', function (next) { 77VideoSchema.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
233function 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
246function removeThumbnail (video, callback) { 256function 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
255function removeTorrent (video, callback) { 265function 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
264function createThumbnail (videoPath, callback) { 269function createThumbnail (videoPath, callback) {
@@ -276,16 +281,6 @@ function createThumbnail (videoPath, callback) {
276 }) 281 })
277} 282}
278 283
279function 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
289function generateThumbnailFromBase64 (data, callback) { 284function 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) {