aboutsummaryrefslogtreecommitdiffhomepage
path: root/models/videos.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-01-31 11:23:52 +0100
committerChocobozzz <florian.bigard@gmail.com>2016-01-31 11:23:52 +0100
commitc45f7f84001c2731909db04dd82e1c1f290386eb (patch)
treeb7e57420a1f65dfbbacc1a532bf489c9bea6125d /models/videos.js
parentcda021079ff455cc0fd0eb95a5395fa808ab63d1 (diff)
downloadPeerTube-c45f7f84001c2731909db04dd82e1c1f290386eb.tar.gz
PeerTube-c45f7f84001c2731909db04dd82e1c1f290386eb.tar.zst
PeerTube-c45f7f84001c2731909db04dd82e1c1f290386eb.zip
Infile code reorganization
Diffstat (limited to 'models/videos.js')
-rw-r--r--models/videos.js222
1 files changed, 118 insertions, 104 deletions
diff --git a/models/videos.js b/models/videos.js
index 626c55819..5711c5657 100644
--- a/models/videos.js
+++ b/models/videos.js
@@ -11,51 +11,29 @@
11 var pods = require('./pods') 11 var pods = require('./pods')
12 var VideosDB = require('../initializers/database').VideosDB 12 var VideosDB = require('../initializers/database').VideosDB
13 13
14 var videos = {}
15
16 var http = config.get('webserver.https') === true ? 'https' : 'http' 14 var http = config.get('webserver.https') === true ? 'https' : 'http'
17 var host = config.get('webserver.host') 15 var host = config.get('webserver.host')
18 var port = config.get('webserver.port') 16 var port = config.get('webserver.port')
19 17
20 // ----------- Private functions ----------- 18 var videos = {
21 function seedVideo (path, callback) { 19 add: add,
22 logger.info('Seeding %s...', path) 20 addRemotes: addRemotes,
23 21 get: get,
24 webtorrent.seed(path, function (torrent) { 22 list: list,
25 logger.info('%s seeded (%s).', path, torrent.magnetURI) 23 listOwned: listOwned,
26 24 remove: remove,
27 return callback(null, torrent) 25 removeAllRemotes: removeAllRemotes,
28 }) 26 removeAllRemotesOf: removeAllRemotesOf,
27 removeRemotes: removeRemotes,
28 search: search,
29 seedAll: seedAll,
30 uploadDir: uploadDir
29 } 31 }
30 32
31 // ----------- Public attributes ---------- 33 // ----------- Public attributes ----------
32 videos.uploadDir = __dirname + '/../' + config.get('storage.uploads') 34 var uploadDir = __dirname + '/../' + config.get('storage.uploads')
33
34 // ----------- Public functions -----------
35 videos.list = function (callback) {
36 VideosDB.find(function (err, videos_list) {
37 if (err) {
38 logger.error('Cannot get list of the videos.', { error: err })
39 return callback(err)
40 }
41
42 return callback(null, videos_list)
43 })
44 }
45
46 videos.listOwned = function (callback) {
47 // If namePath is not null this is *our* video
48 VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) {
49 if (err) {
50 logger.error('Cannot get list of the videos.', { error: err })
51 return callback(err)
52 }
53 35
54 return callback(null, videos_list) 36 function add (data, callback) {
55 })
56 }
57
58 videos.add = function (data, callback) {
59 var video_file = data.video 37 var video_file = data.video
60 var video_data = data.data 38 var video_data = data.data
61 39
@@ -89,7 +67,74 @@
89 }) 67 })
90 } 68 }
91 69
92 videos.remove = function (id, callback) { 70 // TODO: avoid doublons
71 function addRemotes (videos, callback) {
72 if (callback === undefined) callback = function () {}
73
74 var to_add = []
75
76 async.each(videos, function (video, callback_each) {
77 callback_each = dz(callback_each)
78 logger.debug('Add remote video from pod: %s', video.podUrl)
79
80 var params = {
81 name: video.name,
82 namePath: null,
83 description: video.description,
84 magnetUri: video.magnetUri,
85 podUrl: video.podUrl
86 }
87
88 to_add.push(params)
89
90 callback_each()
91 }, function () {
92 VideosDB.create(to_add, function (err, videos) {
93 if (err) {
94 logger.error('Cannot insert this remote video.', { error: err })
95 return callback(err)
96 }
97
98 return callback(null, videos)
99 })
100 })
101 }
102
103 function get (id, callback) {
104 VideosDB.findById(id, function (err, video) {
105 if (err) {
106 logger.error('Cannot get this video.', { error: err })
107 return callback(err)
108 }
109
110 return callback(null, video)
111 })
112 }
113
114 function list (callback) {
115 VideosDB.find(function (err, videos_list) {
116 if (err) {
117 logger.error('Cannot get list of the videos.', { error: err })
118 return callback(err)
119 }
120
121 return callback(null, videos_list)
122 })
123 }
124
125 function listOwned (callback) {
126 // If namePath is not null this is *our* video
127 VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) {
128 if (err) {
129 logger.error('Cannot get list of the videos.', { error: err })
130 return callback(err)
131 }
132
133 return callback(null, videos_list)
134 })
135 }
136
137 function remove (id, callback) {
93 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process 138 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
94 function removeTorrent (magnetUri, callback) { 139 function removeTorrent (magnetUri, callback) {
95 try { 140 try {
@@ -122,7 +167,7 @@
122 return callback(err) 167 return callback(err)
123 } 168 }
124 169
125 fs.unlink(videos.uploadDir + video.namePath, function (err) { 170 fs.unlink(uploadDir + video.namePath, function (err) {
126 if (err) { 171 if (err) {
127 logger.error('Cannot remove this video file.', { error: err }) 172 logger.error('Cannot remove this video file.', { error: err })
128 return callback(err) 173 return callback(err)
@@ -141,8 +186,24 @@
141 }) 186 })
142 } 187 }
143 188
189 function removeAllRemotes (callback) {
190 VideosDB.remove({ namePath: null }, function (err) {
191 if (err) return callback(err)
192
193 callback(null)
194 })
195 }
196
197 function removeAllRemotesOf (fromUrl, callback) {
198 VideosDB.remove({ podUrl: fromUrl }, function (err) {
199 if (err) return callback(err)
200
201 callback(null)
202 })
203 }
204
144 // Use the magnet Uri because the _id field is not the same on different servers 205 // Use the magnet Uri because the _id field is not the same on different servers
145 videos.removeRemotes = function (fromUrl, magnetUris, callback) { 206 function removeRemotes (fromUrl, magnetUris, callback) {
146 if (callback === undefined) callback = function () {} 207 if (callback === undefined) callback = function () {}
147 208
148 VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) { 209 VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) {
@@ -176,68 +237,7 @@
176 }) 237 })
177 } 238 }
178 239
179 videos.removeAllRemotes = function (callback) { 240 function search (name, callback) {
180 VideosDB.remove({ namePath: null }, function (err) {
181 if (err) return callback(err)
182
183 callback(null)
184 })
185 }
186
187 videos.removeAllRemotesOf = function (fromUrl, callback) {
188 VideosDB.remove({ podUrl: fromUrl }, function (err) {
189 if (err) return callback(err)
190
191 callback(null)
192 })
193 }
194
195 // { name, magnetUri, podUrl }
196 // TODO: avoid doublons
197 videos.addRemotes = function (videos, callback) {
198 if (callback === undefined) callback = function () {}
199
200 var to_add = []
201
202 async.each(videos, function (video, callback_each) {
203 callback_each = dz(callback_each)
204 logger.debug('Add remote video from pod: %s', video.podUrl)
205
206 var params = {
207 name: video.name,
208 namePath: null,
209 description: video.description,
210 magnetUri: video.magnetUri,
211 podUrl: video.podUrl
212 }
213
214 to_add.push(params)
215
216 callback_each()
217 }, function () {
218 VideosDB.create(to_add, function (err, videos) {
219 if (err) {
220 logger.error('Cannot insert this remote video.', { error: err })
221 return callback(err)
222 }
223
224 return callback(null, videos)
225 })
226 })
227 }
228
229 videos.get = function (id, callback) {
230 VideosDB.findById(id, function (err, video) {
231 if (err) {
232 logger.error('Cannot get this video.', { error: err })
233 return callback(err)
234 }
235
236 return callback(null, video)
237 })
238 }
239
240 videos.search = function (name, callback) {
241 VideosDB.find({ name: new RegExp(name) }, function (err, videos) { 241 VideosDB.find({ name: new RegExp(name) }, function (err, videos) {
242 if (err) { 242 if (err) {
243 logger.error('Cannot search the videos.', { error: err }) 243 logger.error('Cannot search the videos.', { error: err })
@@ -248,7 +248,7 @@
248 }) 248 })
249 } 249 }
250 250
251 videos.seedAll = function (callback) { 251 function seedAll (callback) {
252 VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) { 252 VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) {
253 if (err) { 253 if (err) {
254 logger.error('Cannot get list of the videos to seed.', { error: err }) 254 logger.error('Cannot get list of the videos to seed.', { error: err })
@@ -256,7 +256,7 @@
256 } 256 }
257 257
258 async.each(videos_list, function (video, each_callback) { 258 async.each(videos_list, function (video, each_callback) {
259 seedVideo(videos.uploadDir + video.namePath, function (err) { 259 seedVideo(uploadDir + video.namePath, function (err) {
260 if (err) { 260 if (err) {
261 logger.error('Cannot seed this video.', { error: err }) 261 logger.error('Cannot seed this video.', { error: err })
262 return callback(err) 262 return callback(err)
@@ -268,5 +268,19 @@
268 }) 268 })
269 } 269 }
270 270
271 // ---------------------------------------------------------------------------
272
271 module.exports = videos 273 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 }
272})() 286})()