aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--controllers/api/v1/pods.js56
-rw-r--r--controllers/api/v1/remoteVideos.js5
-rw-r--r--controllers/api/v1/videos.js77
-rw-r--r--initializers/database.js58
-rw-r--r--lib/friends.js218
-rw-r--r--lib/poolRequests.js61
-rw-r--r--lib/videos.js51
-rw-r--r--lib/webtorrent.js2
-rw-r--r--middlewares/misc.js9
-rw-r--r--middlewares/reqValidators/pods.js15
-rw-r--r--middlewares/reqValidators/videos.js32
-rw-r--r--models/pods.js276
-rw-r--r--models/poolRequests.js67
-rw-r--r--models/videos.js202
-rw-r--r--server.js8
15 files changed, 629 insertions, 508 deletions
diff --git a/controllers/api/v1/pods.js b/controllers/api/v1/pods.js
index b073e85af..82d8d7f08 100644
--- a/controllers/api/v1/pods.js
+++ b/controllers/api/v1/pods.js
@@ -2,18 +2,23 @@
2 'use strict' 2 'use strict'
3 3
4 var express = require('express') 4 var express = require('express')
5 var fs = require('fs')
5 6
7 var logger = require('../../../helpers/logger')
8 var friends = require('../../../lib/friends')
6 var middleware = require('../../../middlewares') 9 var middleware = require('../../../middlewares')
7 var miscMiddleware = middleware.misc 10 var miscMiddleware = middleware.misc
8 var pods = require('../../../models/pods') 11 var Pods = require('../../../models/pods')
9 var reqValidator = middleware.reqValidators.pods 12 var reqValidator = middleware.reqValidators.pods
10 var secureRequest = middleware.reqValidators.remote.secureRequest 13 var secureRequest = middleware.reqValidators.remote.secureRequest
14 var utils = require('../../../helpers/utils')
15 var Videos = require('../../../models/videos')
11 16
12 var router = express.Router() 17 var router = express.Router()
13 18
14 router.get('/', miscMiddleware.cache(false), listPods) 19 router.get('/', miscMiddleware.cache(false), listPods)
15 router.post('/', reqValidator.podsAdd, miscMiddleware.cache(false), addPods) 20 router.post('/', reqValidator.podsAdd, miscMiddleware.cache(false), addPods)
16 router.get('/makefriends', miscMiddleware.cache(false), makeFriends) 21 router.get('/makefriends', reqValidator.makeFriends, miscMiddleware.cache(false), makeFriends)
17 router.get('/quitfriends', miscMiddleware.cache(false), quitFriends) 22 router.get('/quitfriends', miscMiddleware.cache(false), quitFriends)
18 // Post because this is a secured request 23 // Post because this is a secured request
19 router.post('/remove', secureRequest, miscMiddleware.decryptBody, removePods) 24 router.post('/remove', secureRequest, miscMiddleware.decryptBody, removePods)
@@ -25,15 +30,32 @@
25 // --------------------------------------------------------------------------- 30 // ---------------------------------------------------------------------------
26 31
27 function addPods (req, res, next) { 32 function addPods (req, res, next) {
28 pods.add(req.body.data, function (err, json) { 33 var informations = req.body.data
34 Pods.add(informations, function (err) {
29 if (err) return next(err) 35 if (err) return next(err)
30 36
31 res.json(json) 37 Videos.addRemotes(informations.videos)
38
39 fs.readFile(utils.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) {
40 if (err) {
41 logger.error('Cannot read cert file.', { error: err })
42 return next(err)
43 }
44
45 Videos.listOwned(function (err, videos_list) {
46 if (err) {
47 logger.error('Cannot get the list of owned videos.', { error: err })
48 return next(err)
49 }
50
51 res.json({ cert: cert, videos: videos_list })
52 })
53 })
32 }) 54 })
33 } 55 }
34 56
35 function listPods (req, res, next) { 57 function listPods (req, res, next) {
36 pods.list(function (err, pods_list) { 58 Pods.list(function (err, pods_list) {
37 if (err) return next(err) 59 if (err) return next(err)
38 60
39 res.json(pods_list) 61 res.json(pods_list)
@@ -41,32 +63,28 @@
41 } 63 }
42 64
43 function makeFriends (req, res, next) { 65 function makeFriends (req, res, next) {
44 pods.hasFriends(function (err, has_friends) { 66 friends.makeFriends(function (err) {
45 if (err) return next(err) 67 if (err) return next(err)
46 68
47 if (has_friends === true) { 69 res.sendStatus(204)
48 // We need to quit our friends before make new ones
49 res.sendStatus(409)
50 } else {
51 pods.makeFriends(function (err) {
52 if (err) return next(err)
53
54 res.sendStatus(204)
55 })
56 }
57 }) 70 })
58 } 71 }
59 72
60 function removePods (req, res, next) { 73 function removePods (req, res, next) {
61 pods.remove(req.body.signature.url, function (err) { 74 var url = req.body.signature.url
75 Pods.remove(url, function (err) {
62 if (err) return next(err) 76 if (err) return next(err)
63 77
64 res.sendStatus(204) 78 Videos.removeAllRemotesOf(url, function (err) {
79 if (err) logger.error('Cannot remove all remote videos of %s.', url)
80 logger.info('%s pod removed.', url)
81 res.sendStatus(204)
82 })
65 }) 83 })
66 } 84 }
67 85
68 function quitFriends (req, res, next) { 86 function quitFriends (req, res, next) {
69 pods.quitFriends(function (err) { 87 friends.quitFriends(function (err) {
70 if (err) return next(err) 88 if (err) return next(err)
71 89
72 res.sendStatus(204) 90 res.sendStatus(204)
diff --git a/controllers/api/v1/remoteVideos.js b/controllers/api/v1/remoteVideos.js
index 2be2fc87e..d72db9836 100644
--- a/controllers/api/v1/remoteVideos.js
+++ b/controllers/api/v1/remoteVideos.js
@@ -42,7 +42,10 @@
42 } 42 }
43 43
44 function removeRemoteVideo (req, res, next) { 44 function removeRemoteVideo (req, res, next) {
45 videos.removeRemotes(req.body.signature.url, pluck(req.body.data, 'magnetUri'), function (err) { 45 var url = req.body.signature.url
46 var magnetUris = pluck(req.body.data, 'magnetUri')
47
48 videos.removeRemotesOfByMagnetUris(url, magnetUris, function (err) {
46 if (err) return next(err) 49 if (err) return next(err)
47 50
48 res.sendStatus(204) 51 res.sendStatus(204)
diff --git a/controllers/api/v1/videos.js b/controllers/api/v1/videos.js
index 64b05e32b..d2e7e8825 100644
--- a/controllers/api/v1/videos.js
+++ b/controllers/api/v1/videos.js
@@ -6,10 +6,14 @@
6 var express = require('express') 6 var express = require('express')
7 var multer = require('multer') 7 var multer = require('multer')
8 8
9 var logger = require('../../../helpers/logger')
10 var friends = require('../../../lib/friends')
9 var middleware = require('../../../middlewares') 11 var middleware = require('../../../middlewares')
10 var miscMiddleware = middleware.misc 12 var miscMiddleware = middleware.misc
11 var reqValidator = middleware.reqValidators.videos 13 var reqValidator = middleware.reqValidators.videos
12 var videos = require('../../../models/videos') 14 var Videos = require('../../../models/videos') // model
15 var videos = require('../../../lib/videos')
16 var webtorrent = require('../../../lib/webTorrentNode')
13 17
14 var router = express.Router() 18 var router = express.Router()
15 var uploads = config.get('storage.uploads') 19 var uploads = config.get('storage.uploads')
@@ -35,7 +39,7 @@
35 var reqFiles = multer({ storage: storage }).fields([{ name: 'input_video', maxCount: 1 }]) 39 var reqFiles = multer({ storage: storage }).fields([{ name: 'input_video', maxCount: 1 }])
36 40
37 router.get('/', miscMiddleware.cache(false), listVideos) 41 router.get('/', miscMiddleware.cache(false), listVideos)
38 router.post('/', reqFiles, reqValidator.videosAdd, miscMiddleware.cache(false), addVideos) 42 router.post('/', reqFiles, reqValidator.videosAdd, miscMiddleware.cache(false), addVideo)
39 router.get('/:id', reqValidator.videosGet, miscMiddleware.cache(false), getVideos) 43 router.get('/:id', reqValidator.videosGet, miscMiddleware.cache(false), getVideos)
40 router.delete('/:id', reqValidator.videosRemove, miscMiddleware.cache(false), removeVideo) 44 router.delete('/:id', reqValidator.videosRemove, miscMiddleware.cache(false), removeVideo)
41 router.get('/search/:name', reqValidator.videosSearch, miscMiddleware.cache(false), searchVideos) 45 router.get('/search/:name', reqValidator.videosSearch, miscMiddleware.cache(false), searchVideos)
@@ -46,17 +50,41 @@
46 50
47 // --------------------------------------------------------------------------- 51 // ---------------------------------------------------------------------------
48 52
49 function addVideos (req, res, next) { 53 function addVideo (req, res, next) {
50 videos.add({ video: req.files.input_video[0], data: req.body }, function (err) { 54 var video_file = req.files.input_video[0]
51 if (err) return next(err) 55 var video_infos = req.body
56
57 videos.seed(video_file.path, function (err, torrent) {
58 if (err) {
59 logger.error('Cannot seed this video.', { error: err })
60 return next(err)
61 }
62
63 var video_data = {
64 name: video_infos.name,
65 namePath: video_file.filename,
66 description: video_infos.description,
67 magnetUri: torrent.magnetURI
68 }
69
70 Videos.add(video_data, function (err) {
71 if (err) {
72 // TODO unseed the video
73 logger.error('Cannot insert this video in the database.', { error: err })
74 return next(err)
75 }
52 76
53 // TODO : include Location of the new video 77 // Now we'll add the video's meta data to our friends
54 res.sendStatus(201) 78 friends.addVideoToFriends(video_data)
79
80 // TODO : include Location of the new video
81 res.sendStatus(201)
82 })
55 }) 83 })
56 } 84 }
57 85
58 function getVideos (req, res, next) { 86 function getVideos (req, res, next) {
59 videos.get(req.params.id, function (err, video) { 87 Videos.get(req.params.id, function (err, video) {
60 if (err) return next(err) 88 if (err) return next(err)
61 89
62 if (video === null) { 90 if (video === null) {
@@ -68,7 +96,7 @@
68 } 96 }
69 97
70 function listVideos (req, res, next) { 98 function listVideos (req, res, next) {
71 videos.list(function (err, videos_list) { 99 Videos.list(function (err, videos_list) {
72 if (err) return next(err) 100 if (err) return next(err)
73 101
74 res.json(videos_list) 102 res.json(videos_list)
@@ -76,18 +104,43 @@
76 } 104 }
77 105
78 function removeVideo (req, res, next) { 106 function removeVideo (req, res, next) {
79 videos.remove(req.params.id, function (err) { 107 var video_id = req.params.id
108 Videos.get(video_id, function (err, video) {
80 if (err) return next(err) 109 if (err) return next(err)
81 110
82 res.sendStatus(204) 111 removeTorrent(video.magnetUri, function () {
112 Videos.removeOwned(req.params.id, function (err) {
113 if (err) return next(err)
114
115 var params = {
116 name: video.name,
117 magnetUri: video.magnetUri
118 }
119
120 friends.removeVideoToFriends(params)
121 res.sendStatus(204)
122 })
123 })
83 }) 124 })
84 } 125 }
85 126
86 function searchVideos (req, res, next) { 127 function searchVideos (req, res, next) {
87 videos.search(req.params.name, function (err, videos_list) { 128 Videos.search(req.params.name, function (err, videos_list) {
88 if (err) return next(err) 129 if (err) return next(err)
89 130
90 res.json(videos_list) 131 res.json(videos_list)
91 }) 132 })
92 } 133 }
134
135 // ---------------------------------------------------------------------------
136
137 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
138 function removeTorrent (magnetUri, callback) {
139 try {
140 webtorrent.remove(magnetUri, callback)
141 } catch (err) {
142 logger.warn('Cannot remove the torrent from WebTorrent', { err: err })
143 return callback(null)
144 }
145 }
93})() 146})()
diff --git a/initializers/database.js b/initializers/database.js
index e041d5c4b..96c172637 100644
--- a/initializers/database.js
+++ b/initializers/database.js
@@ -4,59 +4,29 @@
4 var config = require('config') 4 var config = require('config')
5 var mongoose = require('mongoose') 5 var mongoose = require('mongoose')
6 6
7 var constants = require('./constants')
8 var logger = require('../helpers/logger') 7 var logger = require('../helpers/logger')
9 8
10 var dbname = 'peertube' + config.get('database.suffix') 9 var dbname = 'peertube' + config.get('database.suffix')
11 var host = config.get('database.host') 10 var host = config.get('database.host')
12 var port = config.get('database.port') 11 var port = config.get('database.port')
13 12
14 // ----------- Pods ----------- 13 var database = {
15 var podsSchema = mongoose.Schema({ 14 connect: connect
16 url: String, 15 }
17 publicKey: String,
18 score: { type: Number, max: constants.FRIEND_BASE_SCORE }
19 })
20
21 var PodsDB = mongoose.model('pods', podsSchema)
22
23 // ----------- PoolRequests -----------
24 var poolRequestsSchema = mongoose.Schema({
25 type: String,
26 id: String, // Special id to find duplicates (video created we want to remove...)
27 request: mongoose.Schema.Types.Mixed
28 })
29
30 var PoolRequestsDB = mongoose.model('poolRequests', poolRequestsSchema)
31
32 // ----------- Videos -----------
33 var videosSchema = mongoose.Schema({
34 name: String,
35 namePath: String,
36 description: String,
37 magnetUri: String,
38 podUrl: String
39 })
40
41 var VideosDB = mongoose.model('videos', videosSchema)
42 16
43 // --------------------------------------------------------------------------- 17 function connect () {
18 mongoose.connect('mongodb://' + host + ':' + port + '/' + dbname)
19 mongoose.connection.on('error', function () {
20 logger.error('Mongodb connection error.')
21 process.exit(0)
22 })
44 23
45 module.exports = { 24 mongoose.connection.on('open', function () {
46 PodsDB: PodsDB, 25 logger.info('Connected to mongodb.')
47 PoolRequestsDB: PoolRequestsDB, 26 })
48 VideosDB: VideosDB
49 } 27 }
50 28
51 // ----------- Connection ----------- 29 // ---------------------------------------------------------------------------
52
53 mongoose.connect('mongodb://' + host + ':' + port + '/' + dbname)
54 mongoose.connection.on('error', function () {
55 logger.error('Mongodb connection error.')
56 process.exit(0)
57 })
58 30
59 mongoose.connection.on('open', function () { 31 module.exports = database
60 logger.info('Connected to mongodb.')
61 })
62})() 32})()
diff --git a/lib/friends.js b/lib/friends.js
new file mode 100644
index 000000000..e093c85c4
--- /dev/null
+++ b/lib/friends.js
@@ -0,0 +1,218 @@
1;(function () {
2 'use strict'
3
4 var async = require('async')
5 var config = require('config')
6 var fs = require('fs')
7 var request = require('request')
8
9 var constants = require('../initializers/constants')
10 var logger = require('../helpers/logger')
11 var Pods = require('../models/pods')
12 var PoolRequests = require('../models/poolRequests')
13 var poolRequests = require('../lib/poolRequests')
14 var utils = require('../helpers/utils')
15 var Videos = require('../models/videos')
16
17 var http = config.get('webserver.https') ? 'https' : 'http'
18 var host = config.get('webserver.host')
19 var port = config.get('webserver.port')
20
21 var pods = {
22 addVideoToFriends: addVideoToFriends,
23 hasFriends: hasFriends,
24 makeFriends: makeFriends,
25 quitFriends: quitFriends,
26 removeVideoToFriends: removeVideoToFriends
27 }
28
29 function addVideoToFriends (video) {
30 // To avoid duplicates
31 var id = video.name + video.magnetUri
32 // namePath is null
33 // TODO
34 video.namePath = null
35 PoolRequests.addRequest(id, 'add', video)
36 }
37
38 function hasFriends (callback) {
39 Pods.count(function (err, count) {
40 if (err) return callback(err)
41
42 var has_friends = (count !== 0)
43 callback(null, has_friends)
44 })
45 }
46
47 function makeFriends (callback) {
48 var pods_score = {}
49
50 logger.info('Make friends!')
51 fs.readFile(utils.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) {
52 if (err) {
53 logger.error('Cannot read public cert.', { error: err })
54 return callback(err)
55 }
56
57 var urls = config.get('network.friends')
58
59 async.each(urls, computeForeignPodsList, function () {
60 logger.debug('Pods scores computed.', { pods_score: pods_score })
61 var pods_list = computeWinningPods(urls, pods_score)
62 logger.debug('Pods that we keep computed.', { pods_to_keep: pods_list })
63
64 makeRequestsToWinningPods(cert, pods_list)
65 })
66 })
67
68 // -----------------------------------------------------------------------
69
70 function computeForeignPodsList (url, callback) {
71 // Let's give 1 point to the pod we ask the friends list
72 pods_score[url] = 1
73
74 getForeignPodsList(url, function (foreign_pods_list) {
75 if (foreign_pods_list.length === 0) return callback()
76
77 async.each(foreign_pods_list, function (foreign_pod, callback_each) {
78 var foreign_url = foreign_pod.url
79
80 if (pods_score[foreign_url]) pods_score[foreign_url]++
81 else pods_score[foreign_url] = 1
82
83 callback_each()
84 }, function () {
85 callback()
86 })
87 })
88 }
89
90 function computeWinningPods (urls, pods_score) {
91 // Build the list of pods to add
92 // Only add a pod if it exists in more than a half base pods
93 var pods_list = []
94 var base_score = urls.length / 2
95 Object.keys(pods_score).forEach(function (pod) {
96 if (pods_score[pod] > base_score) pods_list.push({ url: pod })
97 })
98
99 return pods_list
100 }
101
102 function makeRequestsToWinningPods (cert, pods_list) {
103 // Stop pool requests
104 poolRequests.deactivate()
105 // Flush pool requests
106 poolRequests.forceSend()
107
108 // Get the list of our videos to send to our new friends
109 Videos.listOwned(function (err, videos_list) {
110 if (err) throw err
111
112 var data = {
113 url: http + '://' + host + ':' + port,
114 publicKey: cert,
115 videos: videos_list
116 }
117
118 utils.makeMultipleRetryRequest(
119 { method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data },
120
121 pods_list,
122
123 function eachRequest (err, response, body, url, pod, callback_each_request) {
124 // We add the pod if it responded correctly with its public certificate
125 if (!err && response.statusCode === 200) {
126 Pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) {
127 if (err) logger.error('Error with adding %s pod.', pod.url, { error: err })
128
129 Videos.addRemotes(body.videos, function (err) {
130 if (err) logger.error('Error with adding videos of pod.', pod.url, { error: err })
131
132 logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos })
133 return callback_each_request()
134 })
135 })
136 } else {
137 logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') })
138 return callback_each_request()
139 }
140 },
141
142 function endRequests (err) {
143 // Now we made new friends, we can re activate the pool of requests
144 poolRequests.activate()
145
146 if (err) {
147 logger.error('There was some errors when we wanted to make friends.', { error: err })
148 return callback(err)
149 }
150
151 logger.debug('makeRequestsToWinningPods finished.')
152 return callback(null)
153 }
154 )
155 })
156 }
157 }
158
159 function quitFriends (callback) {
160 // Stop pool requests
161 poolRequests.deactivate()
162 // Flush pool requests
163 poolRequests.forceSend()
164
165 Pods.list(function (err, pods) {
166 if (err) return callback(err)
167
168 var request = {
169 method: 'POST',
170 path: '/api/' + constants.API_VERSION + '/pods/remove',
171 sign: true,
172 encrypt: true,
173 data: {
174 url: 'me' // Fake data
175 }
176 }
177
178 // Announce we quit them
179 utils.makeMultipleRetryRequest(request, pods, function () {
180 Pods.removeAll(function (err) {
181 poolRequests.activate()
182
183 if (err) return callback(err)
184
185 logger.info('Broke friends, so sad :(')
186
187 Videos.removeAllRemotes(function (err) {
188 if (err) return callback(err)
189
190 logger.info('Removed all remote videos.')
191 callback(null)
192 })
193 })
194 })
195 })
196 }
197
198 function removeVideoToFriends (video) {
199 // To avoid duplicates
200 var id = video.name + video.magnetUri
201 PoolRequests.addRequest(id, 'remove', video)
202 }
203
204 // ---------------------------------------------------------------------------
205
206 module.exports = pods
207
208 // ---------------------------------------------------------------------------
209
210 function getForeignPodsList (url, callback) {
211 var path = '/api/' + constants.API_VERSION + '/pods'
212
213 request.get(url + path, function (err, response, body) {
214 if (err) throw err
215 callback(JSON.parse(body))
216 })
217 }
218})()
diff --git a/lib/poolRequests.js b/lib/poolRequests.js
index 53f47d629..796f06149 100644
--- a/lib/poolRequests.js
+++ b/lib/poolRequests.js
@@ -5,18 +5,16 @@
5 var pluck = require('lodash-node/compat/collection/pluck') 5 var pluck = require('lodash-node/compat/collection/pluck')
6 6
7 var constants = require('../initializers/constants') 7 var constants = require('../initializers/constants')
8 var database = require('../initializers/database')
9 var logger = require('../helpers/logger') 8 var logger = require('../helpers/logger')
10 var PodsDB = database.PodsDB 9 var Pods = require('../models/pods')
11 var PoolRequestsDB = database.PoolRequestsDB 10 var PoolRequests = require('../models/poolRequests')
12 var utils = require('../helpers/utils') 11 var utils = require('../helpers/utils')
13 var VideosDB = database.VideosDB 12 var Videos = require('../models/videos')
14 13
15 var timer = null 14 var timer = null
16 15
17 var poolRequests = { 16 var poolRequests = {
18 activate: activate, 17 activate: activate,
19 addToPoolRequests: addToPoolRequests,
20 deactivate: deactivate, 18 deactivate: deactivate,
21 forceSend: forceSend 19 forceSend: forceSend
22 } 20 }
@@ -36,30 +34,6 @@
36 timer = setInterval(makePoolRequests, constants.INTERVAL) 34 timer = setInterval(makePoolRequests, constants.INTERVAL)
37 } 35 }
38 36
39 function addToPoolRequests (id, type, request) {
40 logger.debug('Add request to the pool requests.', { id: id, type: type, request: request })
41
42 PoolRequestsDB.findOne({ id: id }, function (err, entity) {
43 if (err) logger.error(err)
44
45 if (entity) {
46 if (entity.type === type) {
47 logger.error(new Error('Cannot insert two same requests.'))
48 return
49 }
50
51 // Remove the request of the other type
52 PoolRequestsDB.remove({ id: id }, function (err) {
53 if (err) logger.error(err)
54 })
55 } else {
56 PoolRequestsDB.create({ id: id, type: type, request: request }, function (err) {
57 if (err) logger.error(err)
58 })
59 }
60 })
61 }
62
63 // --------------------------------------------------------------------------- 37 // ---------------------------------------------------------------------------
64 38
65 module.exports = poolRequests 39 module.exports = poolRequests
@@ -69,7 +43,7 @@
69 function makePoolRequest (type, requests, callback) { 43 function makePoolRequest (type, requests, callback) {
70 if (!callback) callback = function () {} 44 if (!callback) callback = function () {}
71 45
72 PodsDB.find({}, { _id: 1, url: 1, publicKey: 1 }).exec(function (err, pods) { 46 Pods.list(function (err, pods) {
73 if (err) throw err 47 if (err) throw err
74 48
75 var params = { 49 var params = {
@@ -116,7 +90,7 @@
116 function makePoolRequests () { 90 function makePoolRequests () {
117 logger.info('Making pool requests to friends.') 91 logger.info('Making pool requests to friends.')
118 92
119 PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, function (err, pool_requests) { 93 PoolRequests.list(function (err, pool_requests) {
120 if (err) throw err 94 if (err) throw err
121 95
122 if (pool_requests.length === 0) return 96 if (pool_requests.length === 0) return
@@ -150,7 +124,7 @@
150 makePoolRequest('add', requests.add.requests, function (err) { 124 makePoolRequest('add', requests.add.requests, function (err) {
151 if (err) logger.error('Errors when sent add pool requests.', { error: err }) 125 if (err) logger.error('Errors when sent add pool requests.', { error: err })
152 126
153 removePoolRequestsFromDB(requests.add.ids) 127 PoolRequests.removeRequests(requests.add.ids)
154 }) 128 })
155 } 129 }
156 130
@@ -159,7 +133,7 @@
159 makePoolRequest('remove', requests.remove.requests, function (err) { 133 makePoolRequest('remove', requests.remove.requests, function (err) {
160 if (err) logger.error('Errors when sent remove pool requests.', { error: err }) 134 if (err) logger.error('Errors when sent remove pool requests.', { error: err })
161 135
162 removePoolRequestsFromDB(requests.remove.ids) 136 PoolRequests.removeRequests(requests.remove.ids)
163 }) 137 })
164 } 138 }
165 }) 139 })
@@ -167,7 +141,7 @@
167 } 141 }
168 142
169 function removeBadPods () { 143 function removeBadPods () {
170 PodsDB.find({ score: 0 }, { _id: 1, url: 1 }, function (err, pods) { 144 Pods.findBadPods(function (err, pods) {
171 if (err) throw err 145 if (err) throw err
172 146
173 if (pods.length === 0) return 147 if (pods.length === 0) return
@@ -175,12 +149,12 @@
175 var urls = pluck(pods, 'url') 149 var urls = pluck(pods, 'url')
176 var ids = pluck(pods, '_id') 150 var ids = pluck(pods, '_id')
177 151
178 VideosDB.remove({ podUrl: { $in: urls } }, function (err, r) { 152 Videos.removeAllRemotesOf(urls, function (err, r) {
179 if (err) logger.error('Cannot remove videos from a pod that we removing.', { error: err }) 153 if (err) logger.error('Cannot remove videos from a pod that we removing.', { error: err })
180 var videos_removed = r.result.n 154 var videos_removed = r.result.n
181 logger.info('Removed %d videos.', videos_removed) 155 logger.info('Removed %d videos.', videos_removed)
182 156
183 PodsDB.remove({ _id: { $in: ids } }, function (err, r) { 157 Pods.removeAllByIds(ids, function (err, r) {
184 if (err) logger.error('Cannot remove bad pods.', { error: err }) 158 if (err) logger.error('Cannot remove bad pods.', { error: err })
185 159
186 var pods_removed = r.result.n 160 var pods_removed = r.result.n
@@ -190,22 +164,11 @@
190 }) 164 })
191 } 165 }
192 166
193 function removePoolRequestsFromDB (ids) {
194 PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) {
195 if (err) {
196 logger.error('Cannot remove requests from the pool requests database.', { error: err })
197 return
198 }
199
200 logger.info('Pool requests flushed.')
201 })
202 }
203
204 function updatePodsScore (good_pods, bad_pods) { 167 function updatePodsScore (good_pods, bad_pods) {
205 logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length) 168 logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length)
206 169
207 PodsDB.update({ _id: { $in: good_pods } }, { $inc: { score: constants.PODS_SCORE.BONUS } }, { multi: true }).exec() 170 Pods.incrementScores(good_pods, constants.PODS_SCORE.BONUS)
208 PodsDB.update({ _id: { $in: bad_pods } }, { $inc: { score: constants.PODS_SCORE.MALUS } }, { multi: true }, function (err) { 171 Pods.incrementScores(bad_pods, constants.PODS_SCORE.MALUS, function (err) {
209 if (err) throw err 172 if (err) throw err
210 removeBadPods() 173 removeBadPods()
211 }) 174 })
diff --git a/lib/videos.js b/lib/videos.js
new file mode 100644
index 000000000..5d23070a7
--- /dev/null
+++ b/lib/videos.js
@@ -0,0 +1,51 @@
1;(function () {
2 'use strict'
3
4 var async = require('async')
5 var config = require('config')
6 var webtorrent = require('../lib/webTorrentNode')
7
8 var logger = require('../helpers/logger')
9 var Videos = require('../models/videos')
10
11 var uploadDir = __dirname + '/../' + config.get('storage.uploads')
12
13 var videos = {
14 seed: seed,
15 seedAllExisting: seedAllExisting
16 }
17
18 function seed (path, callback) {
19 logger.info('Seeding %s...', path)
20
21 webtorrent.seed(path, function (torrent) {
22 logger.info('%s seeded (%s).', path, torrent.magnetURI)
23
24 return callback(null, torrent)
25 })
26 }
27
28 function seedAllExisting (callback) {
29 Videos.listOwned(function (err, videos_list) {
30 if (err) {
31 logger.error('Cannot get list of the videos to seed.', { error: err })
32 return callback(err)
33 }
34
35 async.each(videos_list, function (video, each_callback) {
36 seed(uploadDir + video.namePath, function (err) {
37 if (err) {
38 logger.error('Cannot seed this video.', { error: err })
39 return callback(err)
40 }
41
42 each_callback(null)
43 })
44 }, callback)
45 })
46 }
47
48 // ---------------------------------------------------------------------------
49
50 module.exports = videos
51})()
diff --git a/lib/webtorrent.js b/lib/webtorrent.js
index 41e60499f..d1ca3c9f2 100644
--- a/lib/webtorrent.js
+++ b/lib/webtorrent.js
@@ -62,7 +62,7 @@
62 try { 62 try {
63 wt.remove(magnetUri, callback) 63 wt.remove(magnetUri, callback)
64 } catch (err) { 64 } catch (err) {
65 console.log('Cannot remove the torrent from WebTorrent', { err: err }) 65 console.log('Cannot remove the torrent from WebTorrent')
66 return callback(null) 66 return callback(null)
67 } 67 }
68 68
diff --git a/middlewares/misc.js b/middlewares/misc.js
index dbb604db3..cc4e2e8a4 100644
--- a/middlewares/misc.js
+++ b/middlewares/misc.js
@@ -5,7 +5,7 @@
5 var ursa = require('ursa') 5 var ursa = require('ursa')
6 6
7 var logger = require('../helpers/logger') 7 var logger = require('../helpers/logger')
8 var PodsDB = require('../initializers/database').PodsDB 8 var Pods = require('../models/pods')
9 var utils = require('../helpers/utils') 9 var utils = require('../helpers/utils')
10 10
11 var miscMiddleware = { 11 var miscMiddleware = {
@@ -28,18 +28,19 @@
28 } 28 }
29 29
30 function decryptBody (req, res, next) { 30 function decryptBody (req, res, next) {
31 PodsDB.findOne({ url: req.body.signature.url }, function (err, pod) { 31 var url = req.body.signature.url
32 Pods.findByUrl(url, function (err, pod) {
32 if (err) { 33 if (err) {
33 logger.error('Cannot get signed url in decryptBody.', { error: err }) 34 logger.error('Cannot get signed url in decryptBody.', { error: err })
34 return res.sendStatus(500) 35 return res.sendStatus(500)
35 } 36 }
36 37
37 if (pod === null) { 38 if (pod === null) {
38 logger.error('Unknown pod %s.', req.body.signature.url) 39 logger.error('Unknown pod %s.', url)
39 return res.sendStatus(403) 40 return res.sendStatus(403)
40 } 41 }
41 42
42 logger.debug('Decrypting body from %s.', req.body.signature.url) 43 logger.debug('Decrypting body from %s.', url)
43 44
44 var crt = ursa.createPublicKey(pod.publicKey) 45 var crt = ursa.createPublicKey(pod.publicKey)
45 var signature_ok = crt.hashAndVerify('sha256', new Buffer(req.body.signature.url).toString('hex'), req.body.signature.signature, 'hex') 46 var signature_ok = crt.hashAndVerify('sha256', new Buffer(req.body.signature.url).toString('hex'), req.body.signature.signature, 'hex')
diff --git a/middlewares/reqValidators/pods.js b/middlewares/reqValidators/pods.js
index 6ccfd7361..499cafd8f 100644
--- a/middlewares/reqValidators/pods.js
+++ b/middlewares/reqValidators/pods.js
@@ -2,12 +2,27 @@
2 'use strict' 2 'use strict'
3 3
4 var checkErrors = require('./utils').checkErrors 4 var checkErrors = require('./utils').checkErrors
5 var friends = require('../../lib/friends')
5 var logger = require('../../helpers/logger') 6 var logger = require('../../helpers/logger')
6 7
7 var reqValidatorsPod = { 8 var reqValidatorsPod = {
9 makeFriends: makeFriends,
8 podsAdd: podsAdd 10 podsAdd: podsAdd
9 } 11 }
10 12
13 function makeFriends (req, res, next) {
14 friends.hasFriends(function (err, has_friends) {
15 if (err) return next(err)
16
17 if (has_friends === true) {
18 // We need to quit our friends before make new ones
19 res.sendStatus(409)
20 } else {
21 next()
22 }
23 })
24 }
25
11 function podsAdd (req, res, next) { 26 function podsAdd (req, res, next) {
12 req.checkBody('data.url', 'Should have an url').notEmpty().isURL({ require_protocol: true }) 27 req.checkBody('data.url', 'Should have an url').notEmpty().isURL({ require_protocol: true })
13 req.checkBody('data.publicKey', 'Should have a public key').notEmpty() 28 req.checkBody('data.publicKey', 'Should have a public key').notEmpty()
diff --git a/middlewares/reqValidators/videos.js b/middlewares/reqValidators/videos.js
index 3479c47c3..f7bd24658 100644
--- a/middlewares/reqValidators/videos.js
+++ b/middlewares/reqValidators/videos.js
@@ -3,7 +3,7 @@
3 3
4 var checkErrors = require('./utils').checkErrors 4 var checkErrors = require('./utils').checkErrors
5 var logger = require('../../helpers/logger') 5 var logger = require('../../helpers/logger')
6 var VideosDB = require('../../initializers/database').VideosDB 6 var Videos = require('../../models/videos')
7 7
8 var reqValidatorsVideos = { 8 var reqValidatorsVideos = {
9 videosAdd: videosAdd, 9 videosAdd: videosAdd,
@@ -29,8 +29,13 @@
29 logger.debug('Checking videosGet parameters', { parameters: req.params }) 29 logger.debug('Checking videosGet parameters', { parameters: req.params })
30 30
31 checkErrors(req, res, function () { 31 checkErrors(req, res, function () {
32 findVideoById(req.params.id, function (video) { 32 Videos.getVideoState(req.params.id, function (err, state) {
33 if (!video) return res.status(404).send('Video not found') 33 if (err) {
34 logger.error('Error in videosGet request validator.', { error: err })
35 res.sendStatus(500)
36 }
37
38 if (state.exist === false) return res.status(404).send('Video not found')
34 39
35 next() 40 next()
36 }) 41 })
@@ -43,9 +48,14 @@
43 logger.debug('Checking videosRemove parameters', { parameters: req.params }) 48 logger.debug('Checking videosRemove parameters', { parameters: req.params })
44 49
45 checkErrors(req, res, function () { 50 checkErrors(req, res, function () {
46 findVideoById(req.params.id, function (video) { 51 Videos.getVideoState(req.params.id, function (err, state) {
47 if (!video) return res.status(404).send('Video not found') 52 if (err) {
48 else if (video.namePath === null) return res.status(403).send('Cannot remove video of another pod') 53 logger.error('Error in videosRemove request validator.', { error: err })
54 res.sendStatus(500)
55 }
56
57 if (state.exist === false) return res.status(404).send('Video not found')
58 else if (state.owned === false) return res.status(403).send('Cannot remove video of another pod')
49 59
50 next() 60 next()
51 }) 61 })
@@ -63,14 +73,4 @@
63 // --------------------------------------------------------------------------- 73 // ---------------------------------------------------------------------------
64 74
65 module.exports = reqValidatorsVideos 75 module.exports = reqValidatorsVideos
66
67 // ---------------------------------------------------------------------------
68
69 function findVideoById (id, callback) {
70 VideosDB.findById(id, { _id: 1, namePath: 1 }).limit(1).exec(function (err, video) {
71 if (err) throw err
72
73 callback(video)
74 })
75 }
76})() 76})()
diff --git a/models/pods.js b/models/pods.js
index ed2f0d8ee..395b1e0b1 100644
--- a/models/pods.js
+++ b/models/pods.js
@@ -1,73 +1,61 @@
1;(function () { 1;(function () {
2 'use strict' 2 'use strict'
3 3
4 var async = require('async') 4 var mongoose = require('mongoose')
5 var config = require('config')
6 var fs = require('fs')
7 var request = require('request')
8 5
9 var constants = require('../initializers/constants') 6 var constants = require('../initializers/constants')
10 var logger = require('../helpers/logger') 7 var logger = require('../helpers/logger')
11 var PodsDB = require('../initializers/database').PodsDB
12 var poolRequests = require('../lib/poolRequests')
13 var utils = require('../helpers/utils')
14 8
15 var http = config.get('webserver.https') ? 'https' : 'http' 9 // ---------------------------------------------------------------------------
16 var host = config.get('webserver.host') 10
17 var port = config.get('webserver.port') 11 var podsSchema = mongoose.Schema({
12 url: String,
13 publicKey: String,
14 score: { type: Number, max: constants.FRIEND_BASE_SCORE }
15 })
16 var PodsDB = mongoose.model('pods', podsSchema)
18 17
19 var pods = { 18 // ---------------------------------------------------------------------------
19
20 var Pods = {
20 add: add, 21 add: add,
21 addVideoToFriends: addVideoToFriends, 22 count: count,
23 findByUrl: findByUrl,
24 findBadPods: findBadPods,
25 incrementScores: incrementScores,
22 list: list, 26 list: list,
23 hasFriends: hasFriends,
24 makeFriends: makeFriends,
25 quitFriends: quitFriends,
26 remove: remove, 27 remove: remove,
27 removeVideoToFriends 28 removeAll: removeAll,
29 removeAllByIds: removeAllByIds
28 } 30 }
29 31
30 // TODO: check if the pod is not already a friend 32 // TODO: check if the pod is not already a friend
31 function add (data, callback) { 33 function add (data, callback) {
32 var videos = require('./videos') 34 if (!callback) callback = function () {}
33 logger.info('Adding pod: %s', data.url)
34
35 var params = { 35 var params = {
36 url: data.url, 36 url: data.url,
37 publicKey: data.publicKey, 37 publicKey: data.publicKey,
38 score: constants.FRIEND_BASE_SCORE 38 score: constants.FRIEND_BASE_SCORE
39 } 39 }
40 40
41 PodsDB.create(params, function (err, pod) { 41 PodsDB.create(params, callback)
42 if (err) { 42 }
43 logger.error('Cannot insert the pod.', { error: err })
44 return callback(err)
45 }
46
47 videos.addRemotes(data.videos)
48 43
49 fs.readFile(utils.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) { 44 function count (callback) {
50 if (err) { 45 return PodsDB.count(callback)
51 logger.error('Cannot read cert file.', { error: err }) 46 }
52 return callback(err)
53 }
54 47
55 videos.listOwned(function (err, videos_list) { 48 function findBadPods (callback) {
56 if (err) { 49 PodsDB.find({ score: 0 }, callback)
57 logger.error('Cannot get the list of owned videos.', { error: err }) 50 }
58 return callback(err)
59 }
60 51
61 return callback(null, { cert: cert, videos: videos_list }) 52 function findByUrl (url, callback) {
62 }) 53 PodsDB.findOne({ url: url }, callback)
63 })
64 })
65 } 54 }
66 55
67 function addVideoToFriends (video) { 56 function incrementScores (ids, value, callback) {
68 // To avoid duplicates 57 if (!callback) callback = function () {}
69 var id = video.name + video.magnetUri 58 PodsDB.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback)
70 poolRequests.addToPoolRequests(id, 'add', video)
71 } 59 }
72 60
73 function list (callback) { 61 function list (callback) {
@@ -81,202 +69,22 @@
81 }) 69 })
82 } 70 }
83 71
84 function hasFriends (callback) {
85 PodsDB.count(function (err, count) {
86 if (err) return callback(err)
87
88 var has_friends = (count !== 0)
89 callback(null, has_friends)
90 })
91 }
92
93 function makeFriends (callback) {
94 var videos = require('./videos')
95 var pods_score = {}
96
97 logger.info('Make friends!')
98 fs.readFile(utils.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) {
99 if (err) {
100 logger.error('Cannot read public cert.', { error: err })
101 return callback(err)
102 }
103
104 var urls = config.get('network.friends')
105
106 async.each(urls, computeForeignPodsList, function () {
107 logger.debug('Pods scores computed.', { pods_score: pods_score })
108 var pods_list = computeWinningPods(urls, pods_score)
109 logger.debug('Pods that we keep computed.', { pods_to_keep: pods_list })
110
111 makeRequestsToWinningPods(cert, pods_list)
112 })
113 })
114
115 // -----------------------------------------------------------------------
116
117 function computeForeignPodsList (url, callback) {
118 // Let's give 1 point to the pod we ask the friends list
119 pods_score[url] = 1
120
121 getForeignPodsList(url, function (foreign_pods_list) {
122 if (foreign_pods_list.length === 0) return callback()
123
124 async.each(foreign_pods_list, function (foreign_pod, callback_each) {
125 var foreign_url = foreign_pod.url
126
127 if (pods_score[foreign_url]) pods_score[foreign_url]++
128 else pods_score[foreign_url] = 1
129
130 callback_each()
131 }, function () {
132 callback()
133 })
134 })
135 }
136
137 function computeWinningPods (urls, pods_score) {
138 // Build the list of pods to add
139 // Only add a pod if it exists in more than a half base pods
140 var pods_list = []
141 var base_score = urls.length / 2
142 Object.keys(pods_score).forEach(function (pod) {
143 if (pods_score[pod] > base_score) pods_list.push({ url: pod })
144 })
145
146 return pods_list
147 }
148
149 function makeRequestsToWinningPods (cert, pods_list) {
150 // Stop pool requests
151 poolRequests.deactivate()
152 // Flush pool requests
153 poolRequests.forceSend()
154
155 // Get the list of our videos to send to our new friends
156 videos.listOwned(function (err, videos_list) {
157 if (err) throw err
158
159 var data = {
160 url: http + '://' + host + ':' + port,
161 publicKey: cert,
162 videos: videos_list
163 }
164
165 utils.makeMultipleRetryRequest(
166 { method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data },
167
168 pods_list,
169
170 function eachRequest (err, response, body, url, pod, callback_each_request) {
171 // We add the pod if it responded correctly with its public certificate
172 if (!err && response.statusCode === 200) {
173 add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) {
174 if (err) logger.error('Error with adding %s pod.', pod.url, { error: err })
175
176 videos.addRemotes(body.videos, function (err) {
177 if (err) logger.error('Error with adding videos of pod.', pod.url, { error: err })
178
179 logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos })
180 return callback_each_request()
181 })
182 })
183 } else {
184 logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') })
185 return callback_each_request()
186 }
187 },
188
189 function endRequests (err) {
190 // Now we made new friends, we can re activate the pool of requests
191 poolRequests.activate()
192
193 if (err) {
194 logger.error('There was some errors when we wanted to make friends.', { error: err })
195 return callback(err)
196 }
197
198 logger.debug('makeRequestsToWinningPods finished.')
199 return callback(null)
200 }
201 )
202 })
203 }
204 }
205
206 function quitFriends (callback) {
207 // Stop pool requests
208 poolRequests.deactivate()
209 // Flush pool requests
210 poolRequests.forceSend()
211
212 PodsDB.find(function (err, pods) {
213 if (err) return callback(err)
214
215 var request = {
216 method: 'POST',
217 path: '/api/' + constants.API_VERSION + '/pods/remove',
218 sign: true,
219 encrypt: true,
220 data: {
221 url: 'me' // Fake data
222 }
223 }
224
225 // Announce we quit them
226 utils.makeMultipleRetryRequest(request, pods, function () {
227 PodsDB.remove(function (err) {
228 poolRequests.activate()
229
230 if (err) return callback(err)
231
232 logger.info('Broke friends, so sad :(')
233
234 var videos = require('./videos')
235 videos.removeAllRemotes(function (err) {
236 if (err) return callback(err)
237
238 logger.info('Removed all remote videos.')
239 callback(null)
240 })
241 })
242 })
243 })
244 }
245
246 function remove (url, callback) { 72 function remove (url, callback) {
247 var videos = require('./videos') 73 if (!callback) callback = function () {}
248 logger.info('Removing %s pod.', url) 74 PodsDB.remove({ url: url }, callback)
249
250 videos.removeAllRemotesOf(url, function (err) {
251 if (err) logger.error('Cannot remove all remote videos of %s.', url)
252
253 PodsDB.remove({ url: url }, function (err) {
254 if (err) return callback(err)
255
256 logger.info('%s pod removed.', url)
257 callback(null)
258 })
259 })
260 } 75 }
261 76
262 function removeVideoToFriends (video) { 77 function removeAll (callback) {
263 // To avoid duplicates 78 if (!callback) callback = function () {}
264 var id = video.name + video.magnetUri 79 PodsDB.remove(callback)
265 poolRequests.addToPoolRequests(id, 'remove', video)
266 } 80 }
267 81
268 // --------------------------------------------------------------------------- 82 function removeAllByIds (ids, callback) {
269 83 if (!callback) callback = function () {}
270 module.exports = pods 84 PodsDB.remove({ _id: { $in: ids } }, callback)
85 }
271 86
272 // --------------------------------------------------------------------------- 87 // ---------------------------------------------------------------------------
273 88
274 function getForeignPodsList (url, callback) { 89 module.exports = Pods
275 var path = '/api/' + constants.API_VERSION + '/pods'
276
277 request.get(url + path, function (err, response, body) {
278 if (err) throw err
279 callback(JSON.parse(body))
280 })
281 }
282})() 90})()
diff --git a/models/poolRequests.js b/models/poolRequests.js
new file mode 100644
index 000000000..0f488ef04
--- /dev/null
+++ b/models/poolRequests.js
@@ -0,0 +1,67 @@
1;(function () {
2 'use strict'
3
4 var mongoose = require('mongoose')
5
6 var logger = require('../helpers/logger')
7
8 // ---------------------------------------------------------------------------
9
10 var poolRequestsSchema = mongoose.Schema({
11 type: String,
12 id: String, // Special id to find duplicates (video created we want to remove...)
13 request: mongoose.Schema.Types.Mixed
14 })
15 var PoolRequestsDB = mongoose.model('poolRequests', poolRequestsSchema)
16
17 // ---------------------------------------------------------------------------
18
19 var PoolRequests = {
20 addRequest: addRequest,
21 list: list,
22 removeRequests: removeRequests
23 }
24
25 function addRequest (id, type, request) {
26 logger.debug('Add request to the pool requests.', { id: id, type: type, request: request })
27
28 PoolRequestsDB.findOne({ id: id }, function (err, entity) {
29 if (err) logger.error(err)
30
31 if (entity) {
32 if (entity.type === type) {
33 logger.error(new Error('Cannot insert two same requests.'))
34 return
35 }
36
37 // Remove the request of the other type
38 PoolRequestsDB.remove({ id: id }, function (err) {
39 if (err) logger.error(err)
40 })
41 } else {
42 PoolRequestsDB.create({ id: id, type: type, request: request }, function (err) {
43 if (err) logger.error(err)
44 })
45 }
46 })
47 }
48
49 function list (callback) {
50 PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, callback)
51 }
52
53 function removeRequests (ids) {
54 PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) {
55 if (err) {
56 logger.error('Cannot remove requests from the pool requests database.', { error: err })
57 return
58 }
59
60 logger.info('Pool requests flushed.')
61 })
62 }
63
64 // ---------------------------------------------------------------------------
65
66 module.exports = PoolRequests
67})()
diff --git a/models/videos.js b/models/videos.js
index 5711c5657..10abee6e7 100644
--- a/models/videos.js
+++ b/models/videos.js
@@ -5,71 +5,62 @@
5 var config = require('config') 5 var config = require('config')
6 var dz = require('dezalgo') 6 var dz = require('dezalgo')
7 var fs = require('fs') 7 var fs = require('fs')
8 var webtorrent = require('../lib/webTorrentNode') 8 var mongoose = require('mongoose')
9 9
10 var logger = require('../helpers/logger') 10 var logger = require('../helpers/logger')
11 var pods = require('./pods')
12 var VideosDB = require('../initializers/database').VideosDB
13 11
14 var http = config.get('webserver.https') === true ? 'https' : 'http' 12 var http = config.get('webserver.https') === true ? 'https' : 'http'
15 var host = config.get('webserver.host') 13 var host = config.get('webserver.host')
16 var port = config.get('webserver.port') 14 var port = config.get('webserver.port')
15 var uploadDir = __dirname + '/../' + config.get('storage.uploads')
16
17 // ---------------------------------------------------------------------------
18
19 var videosSchema = mongoose.Schema({
20 name: String,
21 namePath: String,
22 description: String,
23 magnetUri: String,
24 podUrl: String
25 })
26 var VideosDB = mongoose.model('videos', videosSchema)
27
28 // ---------------------------------------------------------------------------
17 29
18 var videos = { 30 var Videos = {
19 add: add, 31 add: add,
20 addRemotes: addRemotes, 32 addRemotes: addRemotes,
21 get: get, 33 get: get,
34 getVideoState: getVideoState,
35 isOwned: isOwned,
22 list: list, 36 list: list,
23 listOwned: listOwned, 37 listOwned: listOwned,
24 remove: remove, 38 removeOwned: removeOwned,
25 removeAllRemotes: removeAllRemotes, 39 removeAllRemotes: removeAllRemotes,
26 removeAllRemotesOf: removeAllRemotesOf, 40 removeAllRemotesOf: removeAllRemotesOf,
27 removeRemotes: removeRemotes, 41 removeRemotesOfByMagnetUris: removeRemotesOfByMagnetUris,
28 search: search, 42 search: search
29 seedAll: seedAll,
30 uploadDir: uploadDir
31 } 43 }
32 44
33 // ----------- Public attributes ---------- 45 function add (video, callback) {
34 var uploadDir = __dirname + '/../' + config.get('storage.uploads') 46 logger.info('Adding %s video to database.', video.name)
35 47
36 function add (data, callback) { 48 var params = video
37 var video_file = data.video 49 params.podUrl = http + '://' + host + ':' + port
38 var video_data = data.data
39 50
40 logger.info('Adding %s video.', video_file.path) 51 VideosDB.create(params, function (err, video) {
41 seedVideo(video_file.path, function (err, torrent) {
42 if (err) { 52 if (err) {
43 logger.error('Cannot seed this video.', { error: err }) 53 logger.error('Cannot insert this video into database.', { error: err })
44 return callback(err) 54 return callback(err)
45 } 55 }
46 56
47 var params = { 57 callback(null)
48 name: video_data.name,
49 namePath: video_file.filename,
50 description: video_data.description,
51 magnetUri: torrent.magnetURI,
52 podUrl: http + '://' + host + ':' + port
53 }
54
55 VideosDB.create(params, function (err, video) {
56 if (err) {
57 logger.error('Cannot insert this video.', { error: err })
58 return callback(err)
59 }
60
61 // Now we'll add the video's meta data to our friends
62 params.namePath = null
63
64 pods.addVideoToFriends(params)
65 callback(null)
66 })
67 }) 58 })
68 } 59 }
69 60
70 // TODO: avoid doublons 61 // TODO: avoid doublons
71 function addRemotes (videos, callback) { 62 function addRemotes (videos, callback) {
72 if (callback === undefined) callback = function () {} 63 if (!callback) callback = function () {}
73 64
74 var to_add = [] 65 var to_add = []
75 66
@@ -111,6 +102,38 @@
111 }) 102 })
112 } 103 }
113 104
105 function getVideoState (id, callback) {
106 get(id, function (err, video) {
107 if (err) return callback(err)
108
109 var exist = (video !== null)
110 var owned = false
111 if (exist === true) {
112 owned = (video.namePath !== null)
113 }
114
115 return callback(null, { exist: exist, owned: owned })
116 })
117 }
118
119 function isOwned (id, callback) {
120 VideosDB.findById(id, function (err, video) {
121 if (err || !video) {
122 if (!err) err = new Error('Cannot find this video.')
123 logger.error('Cannot find this video.', { error: err })
124 return callback(err)
125 }
126
127 if (video.namePath === null) {
128 var error_string = 'Cannot remove the video of another pod.'
129 logger.error(error_string)
130 return callback(null, false, video)
131 }
132
133 callback(null, true, video)
134 })
135 }
136
114 function list (callback) { 137 function list (callback) {
115 VideosDB.find(function (err, videos_list) { 138 VideosDB.find(function (err, videos_list) {
116 if (err) { 139 if (err) {
@@ -134,76 +157,35 @@
134 }) 157 })
135 } 158 }
136 159
137 function remove (id, callback) { 160 function removeOwned (id, callback) {
138 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process 161 VideosDB.findByIdAndRemove(id, function (err, video) {
139 function removeTorrent (magnetUri, callback) { 162 if (err) {
140 try { 163 logger.error('Cannot remove the torrent.', { error: err })
141 webtorrent.remove(magnetUri, callback)
142 } catch (err) {
143 logger.warn('Cannot remove the torrent from WebTorrent', { err: err })
144 return callback(null)
145 }
146 }
147
148 VideosDB.findById(id, function (err, video) {
149 if (err || !video) {
150 if (!err) err = new Error('Cannot find this video.')
151 logger.error('Cannot find this video.', { error: err })
152 return callback(err) 164 return callback(err)
153 } 165 }
154 166
155 if (video.namePath === null) { 167 fs.unlink(uploadDir + video.namePath, function (err) {
156 var error_string = 'Cannot remove the video of another pod.' 168 if (err) {
157 logger.error(error_string) 169 logger.error('Cannot remove this video file.', { error: err })
158 return callback(new Error(error_string)) 170 return callback(err)
159 } 171 }
160
161 logger.info('Removing %s video', video.name)
162
163 removeTorrent(video.magnetUri, function () {
164 VideosDB.findByIdAndRemove(id, function (err) {
165 if (err) {
166 logger.error('Cannot remove the torrent.', { error: err })
167 return callback(err)
168 }
169
170 fs.unlink(uploadDir + video.namePath, function (err) {
171 if (err) {
172 logger.error('Cannot remove this video file.', { error: err })
173 return callback(err)
174 }
175
176 var params = {
177 name: video.name,
178 magnetUri: video.magnetUri
179 }
180 172
181 pods.removeVideoToFriends(params) 173 callback(null)
182 callback(null)
183 })
184 })
185 }) 174 })
186 }) 175 })
187 } 176 }
188 177
189 function removeAllRemotes (callback) { 178 function removeAllRemotes (callback) {
190 VideosDB.remove({ namePath: null }, function (err) { 179 VideosDB.remove({ namePath: null }, callback)
191 if (err) return callback(err)
192
193 callback(null)
194 })
195 } 180 }
196 181
197 function removeAllRemotesOf (fromUrl, callback) { 182 function removeAllRemotesOf (fromUrl, callback) {
198 VideosDB.remove({ podUrl: fromUrl }, function (err) { 183 // TODO { podUrl: { $in: urls } }
199 if (err) return callback(err) 184 VideosDB.remove({ podUrl: fromUrl }, callback)
200
201 callback(null)
202 })
203 } 185 }
204 186
205 // Use the magnet Uri because the _id field is not the same on different servers 187 // Use the magnet Uri because the _id field is not the same on different servers
206 function removeRemotes (fromUrl, magnetUris, callback) { 188 function removeRemotesOfByMagnetUris (fromUrl, magnetUris, callback) {
207 if (callback === undefined) callback = function () {} 189 if (callback === undefined) callback = function () {}
208 190
209 VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) { 191 VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) {
@@ -248,39 +230,7 @@
248 }) 230 })
249 } 231 }
250 232
251 function seedAll (callback) {
252 VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) {
253 if (err) {
254 logger.error('Cannot get list of the videos to seed.', { error: err })
255 return callback(err)
256 }
257
258 async.each(videos_list, function (video, each_callback) {
259 seedVideo(uploadDir + video.namePath, function (err) {
260 if (err) {
261 logger.error('Cannot seed this video.', { error: err })
262 return callback(err)
263 }
264
265 each_callback(null)
266 })
267 }, callback)
268 })
269 }
270
271 // --------------------------------------------------------------------------- 233 // ---------------------------------------------------------------------------
272 234
273 module.exports = videos 235 module.exports = Videos
274
275 // ---------------------------------------------------------------------------
276
277 function seedVideo (path, callback) {
278 logger.info('Seeding %s...', path)
279
280 webtorrent.seed(path, function (torrent) {
281 logger.info('%s seeded (%s).', path, torrent.magnetURI)
282
283 return callback(null, torrent)
284 })
285 }
286})() 236})()
diff --git a/server.js b/server.js
index 1e0222f4f..1153c9ef6 100644
--- a/server.js
+++ b/server.js
@@ -30,16 +30,20 @@
30 var config = require('config') 30 var config = require('config')
31 var constants = require('./initializers/constants') 31 var constants = require('./initializers/constants')
32 var customValidators = require('./helpers/customValidators') 32 var customValidators = require('./helpers/customValidators')
33 var database = require('./initializers/database')
33 var logger = require('./helpers/logger') 34 var logger = require('./helpers/logger')
34 var poolRequests = require('./lib/poolRequests') 35 var poolRequests = require('./lib/poolRequests')
35 var routes = require('./controllers') 36 var routes = require('./controllers')
36 var utils = require('./helpers/utils') 37 var utils = require('./helpers/utils')
37 var videos = require('./models/videos') 38 var videos = require('./lib/videos')
38 var webtorrent = require('./lib/webTorrentNode') 39 var webtorrent = require('./lib/webTorrentNode')
39 40
40 // Get configurations 41 // Get configurations
41 var port = config.get('listen.port') 42 var port = config.get('listen.port')
42 43
44 // ----------- Database -----------
45 database.connect()
46
43 // ----------- Command line ----------- 47 // ----------- Command line -----------
44 48
45 // ----------- App ----------- 49 // ----------- App -----------
@@ -153,7 +157,7 @@
153 // Activate the pool requests 157 // Activate the pool requests
154 poolRequests.activate() 158 poolRequests.activate()
155 159
156 videos.seedAll(function () { 160 videos.seedAllExisting(function () {
157 logger.info('Seeded all the videos') 161 logger.info('Seeded all the videos')
158 logger.info('Server listening on port %d', port) 162 logger.info('Server listening on port %d', port)
159 app.emit('ready') 163 app.emit('ready')