aboutsummaryrefslogtreecommitdiffhomepage
path: root/models/videos.js
diff options
context:
space:
mode:
Diffstat (limited to 'models/videos.js')
-rw-r--r--models/videos.js394
1 files changed, 196 insertions, 198 deletions
diff --git a/models/videos.js b/models/videos.js
index ea1823e27..4da389c24 100644
--- a/models/videos.js
+++ b/models/videos.js
@@ -1,237 +1,235 @@
1;(function () { 1'use strict'
2 'use strict' 2
3 3var async = require('async')
4 var async = require('async') 4var config = require('config')
5 var config = require('config') 5var dz = require('dezalgo')
6 var dz = require('dezalgo') 6var fs = require('fs')
7 var fs = require('fs') 7var mongoose = require('mongoose')
8 var mongoose = require('mongoose') 8var path = require('path')
9 var path = require('path') 9
10 10var logger = require('../helpers/logger')
11 var logger = require('../helpers/logger') 11
12 12var http = config.get('webserver.https') === true ? 'https' : 'http'
13 var http = config.get('webserver.https') === true ? 'https' : 'http' 13var host = config.get('webserver.host')
14 var host = config.get('webserver.host') 14var port = config.get('webserver.port')
15 var port = config.get('webserver.port') 15var uploadDir = path.join(__dirname, '..', config.get('storage.uploads'))
16 var uploadDir = path.join(__dirname, '..', config.get('storage.uploads')) 16
17 17// ---------------------------------------------------------------------------
18 // --------------------------------------------------------------------------- 18
19 19var videosSchema = mongoose.Schema({
20 var videosSchema = mongoose.Schema({ 20 name: String,
21 name: String, 21 namePath: String,
22 namePath: String, 22 description: String,
23 description: String, 23 magnetUri: String,
24 magnetUri: String, 24 podUrl: String
25 podUrl: String 25})
26var VideosDB = mongoose.model('videos', videosSchema)
27
28// ---------------------------------------------------------------------------
29
30var Videos = {
31 add: add,
32 addRemotes: addRemotes,
33 get: get,
34 getVideoState: getVideoState,
35 isOwned: isOwned,
36 list: list,
37 listOwned: listOwned,
38 removeOwned: removeOwned,
39 removeAllRemotes: removeAllRemotes,
40 removeAllRemotesOf: removeAllRemotesOf,
41 removeRemotesOfByMagnetUris: removeRemotesOfByMagnetUris,
42 search: search
43}
44
45function add (video, callback) {
46 logger.info('Adding %s video to database.', video.name)
47
48 var params = video
49 params.podUrl = http + '://' + host + ':' + port
50
51 VideosDB.create(params, function (err, video) {
52 if (err) {
53 logger.error('Cannot insert this video into database.')
54 return callback(err)
55 }
56
57 callback(null)
26 }) 58 })
27 var VideosDB = mongoose.model('videos', videosSchema) 59}
28
29 // ---------------------------------------------------------------------------
30
31 var Videos = {
32 add: add,
33 addRemotes: addRemotes,
34 get: get,
35 getVideoState: getVideoState,
36 isOwned: isOwned,
37 list: list,
38 listOwned: listOwned,
39 removeOwned: removeOwned,
40 removeAllRemotes: removeAllRemotes,
41 removeAllRemotesOf: removeAllRemotesOf,
42 removeRemotesOfByMagnetUris: removeRemotesOfByMagnetUris,
43 search: search
44 }
45
46 function add (video, callback) {
47 logger.info('Adding %s video to database.', video.name)
48
49 var params = video
50 params.podUrl = http + '://' + host + ':' + port
51
52 VideosDB.create(params, function (err, video) {
53 if (err) {
54 logger.error('Cannot insert this video into database.')
55 return callback(err)
56 }
57
58 callback(null)
59 })
60 }
61 60
62 // TODO: avoid doublons 61// TODO: avoid doublons
63 function addRemotes (videos, callback) { 62function addRemotes (videos, callback) {
64 if (!callback) callback = function () {} 63 if (!callback) callback = function () {}
65 64
66 var to_add = [] 65 var to_add = []
67 66
68 async.each(videos, function (video, callback_each) { 67 async.each(videos, function (video, callback_each) {
69 callback_each = dz(callback_each) 68 callback_each = dz(callback_each)
70 logger.debug('Add remote video from pod: %s', video.podUrl) 69 logger.debug('Add remote video from pod: %s', video.podUrl)
71
72 var params = {
73 name: video.name,
74 namePath: null,
75 description: video.description,
76 magnetUri: video.magnetUri,
77 podUrl: video.podUrl
78 }
79 70
80 to_add.push(params) 71 var params = {
72 name: video.name,
73 namePath: null,
74 description: video.description,
75 magnetUri: video.magnetUri,
76 podUrl: video.podUrl
77 }
81 78
82 callback_each() 79 to_add.push(params)
83 }, function () {
84 VideosDB.create(to_add, function (err, videos) {
85 if (err) {
86 logger.error('Cannot insert this remote video.')
87 return callback(err)
88 }
89
90 return callback(null, videos)
91 })
92 })
93 }
94 80
95 function get (id, callback) { 81 callback_each()
96 VideosDB.findById(id, function (err, video) { 82 }, function () {
83 VideosDB.create(to_add, function (err, videos) {
97 if (err) { 84 if (err) {
98 logger.error('Cannot get this video.') 85 logger.error('Cannot insert this remote video.')
99 return callback(err) 86 return callback(err)
100 } 87 }
101 88
102 return callback(null, video) 89 return callback(null, videos)
103 }) 90 })
104 } 91 })
92}
105 93
106 function getVideoState (id, callback) { 94function get (id, callback) {
107 get(id, function (err, video) { 95 VideosDB.findById(id, function (err, video) {
108 if (err) return callback(err) 96 if (err) {
97 logger.error('Cannot get this video.')
98 return callback(err)
99 }
109 100
110 var exist = (video !== null) 101 return callback(null, video)
111 var owned = false 102 })
112 if (exist === true) { 103}
113 owned = (video.namePath !== null)
114 }
115 104
116 return callback(null, { exist: exist, owned: owned }) 105function getVideoState (id, callback) {
117 }) 106 get(id, function (err, video) {
118 } 107 if (err) return callback(err)
119 108
120 function isOwned (id, callback) { 109 var exist = (video !== null)
121 VideosDB.findById(id, function (err, video) { 110 var owned = false
122 if (err || !video) { 111 if (exist === true) {
123 if (!err) err = new Error('Cannot find this video.') 112 owned = (video.namePath !== null)
124 logger.error('Cannot find this video.') 113 }
125 return callback(err)
126 }
127 114
128 if (video.namePath === null) { 115 return callback(null, { exist: exist, owned: owned })
129 var error_string = 'Cannot remove the video of another pod.' 116 })
130 logger.error(error_string) 117}
131 return callback(new Error(error_string), false, video) 118
132 } 119function 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.')
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(new Error(error_string), false, video)
131 }
132
133 callback(null, true, video)
134 })
135}
133 136
134 callback(null, true, video) 137function list (callback) {
135 }) 138 VideosDB.find(function (err, videos_list) {
136 } 139 if (err) {
140 logger.error('Cannot get the list of the videos.')
141 return callback(err)
142 }
137 143
138 function list (callback) { 144 return callback(null, videos_list)
139 VideosDB.find(function (err, videos_list) { 145 })
140 if (err) { 146}
141 logger.error('Cannot get the list of the videos.')
142 return callback(err)
143 }
144 147
145 return callback(null, videos_list) 148function listOwned (callback) {
146 }) 149 // If namePath is not null this is *our* video
147 } 150 VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) {
151 if (err) {
152 logger.error('Cannot get the list of owned videos.')
153 return callback(err)
154 }
155
156 return callback(null, videos_list)
157 })
158}
159
160function removeOwned (id, callback) {
161 VideosDB.findByIdAndRemove(id, function (err, video) {
162 if (err) {
163 logger.error('Cannot remove the torrent.')
164 return callback(err)
165 }
148 166
149 function listOwned (callback) { 167 fs.unlink(uploadDir + video.namePath, function (err) {
150 // If namePath is not null this is *our* video
151 VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) {
152 if (err) { 168 if (err) {
153 logger.error('Cannot get the list of owned videos.') 169 logger.error('Cannot remove this video file.')
154 return callback(err) 170 return callback(err)
155 } 171 }
156 172
157 return callback(null, videos_list) 173 callback(null)
158 }) 174 })
159 } 175 })
160 176}
161 function removeOwned (id, callback) { 177
162 VideosDB.findByIdAndRemove(id, function (err, video) { 178function removeAllRemotes (callback) {
163 if (err) { 179 VideosDB.remove({ namePath: null }, callback)
164 logger.error('Cannot remove the torrent.') 180}
165 return callback(err) 181
182function removeAllRemotesOf (fromUrl, callback) {
183 // TODO { podUrl: { $in: urls } }
184 VideosDB.remove({ podUrl: fromUrl }, callback)
185}
186
187// Use the magnet Uri because the _id field is not the same on different servers
188function removeRemotesOfByMagnetUris (fromUrl, magnetUris, callback) {
189 if (callback === undefined) callback = function () {}
190
191 VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) {
192 if (err || !videos) {
193 logger.error('Cannot find the torrent URI of these remote videos.')
194 return callback(err)
195 }
196
197 var to_remove = []
198 async.each(videos, function (video, callback_async) {
199 callback_async = dz(callback_async)
200
201 if (video.podUrl !== fromUrl) {
202 logger.error('The pod %s has not the rights on the video of %s.', fromUrl, video.podUrl)
203 } else {
204 to_remove.push(video._id)
166 } 205 }
167 206
168 fs.unlink(uploadDir + video.namePath, function (err) { 207 callback_async()
208 }, function () {
209 VideosDB.remove({ _id: { $in: to_remove } }, function (err) {
169 if (err) { 210 if (err) {
170 logger.error('Cannot remove this video file.') 211 logger.error('Cannot remove the remote videos.')
171 return callback(err) 212 return callback(err)
172 } 213 }
173 214
215 logger.info('Removed remote videos from %s.', fromUrl)
174 callback(null) 216 callback(null)
175 }) 217 })
176 }) 218 })
177 } 219 })
178 220}
179 function removeAllRemotes (callback) {
180 VideosDB.remove({ namePath: null }, callback)
181 }
182
183 function removeAllRemotesOf (fromUrl, callback) {
184 // TODO { podUrl: { $in: urls } }
185 VideosDB.remove({ podUrl: fromUrl }, callback)
186 }
187
188 // Use the magnet Uri because the _id field is not the same on different servers
189 function removeRemotesOfByMagnetUris (fromUrl, magnetUris, callback) {
190 if (callback === undefined) callback = function () {}
191
192 VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) {
193 if (err || !videos) {
194 logger.error('Cannot find the torrent URI of these remote videos.')
195 return callback(err)
196 }
197
198 var to_remove = []
199 async.each(videos, function (video, callback_async) {
200 callback_async = dz(callback_async)
201
202 if (video.podUrl !== fromUrl) {
203 logger.error('The pod %s has not the rights on the video of %s.', fromUrl, video.podUrl)
204 } else {
205 to_remove.push(video._id)
206 }
207
208 callback_async()
209 }, function () {
210 VideosDB.remove({ _id: { $in: to_remove } }, function (err) {
211 if (err) {
212 logger.error('Cannot remove the remote videos.')
213 return callback(err)
214 }
215
216 logger.info('Removed remote videos from %s.', fromUrl)
217 callback(null)
218 })
219 })
220 })
221 }
222 221
223 function search (name, callback) { 222function search (name, callback) {
224 VideosDB.find({ name: new RegExp(name) }, function (err, videos) { 223 VideosDB.find({ name: new RegExp(name) }, function (err, videos) {
225 if (err) { 224 if (err) {
226 logger.error('Cannot search the videos.') 225 logger.error('Cannot search the videos.')
227 return callback(err) 226 return callback(err)
228 } 227 }
229 228
230 return callback(null, videos) 229 return callback(null, videos)
231 }) 230 })
232 } 231}
233 232
234 // --------------------------------------------------------------------------- 233// ---------------------------------------------------------------------------
235 234
236 module.exports = Videos 235module.exports = Videos
237})()