]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - models/videos.js
Error handling mini refractoring
[github/Chocobozzz/PeerTube.git] / models / videos.js
CommitLineData
8c308c2b
C
1;(function () {
2 'use strict'
3
a1860380
C
4 var async = require('async')
5 var config = require('config')
0b697522 6 var dz = require('dezalgo')
8c308c2b 7 var fs = require('fs')
c173e565 8 var mongoose = require('mongoose')
8c308c2b 9
cda02107 10 var logger = require('../helpers/logger')
8c308c2b 11
8c308c2b
C
12 var http = config.get('webserver.https') === true ? 'https' : 'http'
13 var host = config.get('webserver.host')
14 var port = config.get('webserver.port')
c173e565
C
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 // ---------------------------------------------------------------------------
8c308c2b 29
c173e565 30 var Videos = {
c45f7f84
C
31 add: add,
32 addRemotes: addRemotes,
33 get: get,
c173e565
C
34 getVideoState: getVideoState,
35 isOwned: isOwned,
c45f7f84
C
36 list: list,
37 listOwned: listOwned,
c173e565 38 removeOwned: removeOwned,
c45f7f84
C
39 removeAllRemotes: removeAllRemotes,
40 removeAllRemotesOf: removeAllRemotesOf,
c173e565
C
41 removeRemotesOfByMagnetUris: removeRemotesOfByMagnetUris,
42 search: search
8c308c2b
C
43 }
44
c173e565
C
45 function add (video, callback) {
46 logger.info('Adding %s video to database.', video.name)
45239549 47
c173e565
C
48 var params = video
49 params.podUrl = http + '://' + host + ':' + port
8c308c2b 50
c173e565 51 VideosDB.create(params, function (err, video) {
8c308c2b 52 if (err) {
8425cb89 53 logger.error('Cannot insert this video into database.')
8c308c2b
C
54 return callback(err)
55 }
56
c173e565 57 callback(null)
8c308c2b
C
58 })
59 }
60
c45f7f84
C
61 // TODO: avoid doublons
62 function addRemotes (videos, callback) {
c173e565 63 if (!callback) callback = function () {}
c45f7f84
C
64
65 var to_add = []
66
67 async.each(videos, function (video, callback_each) {
68 callback_each = dz(callback_each)
69 logger.debug('Add remote video from pod: %s', video.podUrl)
70
71 var params = {
72 name: video.name,
73 namePath: null,
74 description: video.description,
75 magnetUri: video.magnetUri,
76 podUrl: video.podUrl
77 }
78
79 to_add.push(params)
80
81 callback_each()
82 }, function () {
83 VideosDB.create(to_add, function (err, videos) {
84 if (err) {
8425cb89 85 logger.error('Cannot insert this remote video.')
c45f7f84
C
86 return callback(err)
87 }
88
89 return callback(null, videos)
90 })
91 })
92 }
93
94 function get (id, callback) {
95 VideosDB.findById(id, function (err, video) {
96 if (err) {
8425cb89 97 logger.error('Cannot get this video.')
c45f7f84
C
98 return callback(err)
99 }
100
101 return callback(null, video)
102 })
103 }
104
c173e565
C
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.')
8425cb89 123 logger.error('Cannot find this video.')
c173e565
C
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)
8425cb89 130 return callback(new Error(error_string), false, video)
c173e565
C
131 }
132
133 callback(null, true, video)
134 })
135 }
136
c45f7f84
C
137 function list (callback) {
138 VideosDB.find(function (err, videos_list) {
139 if (err) {
8425cb89 140 logger.error('Cannot get the list of the videos.')
c45f7f84
C
141 return callback(err)
142 }
143
144 return callback(null, videos_list)
145 })
146 }
147
148 function listOwned (callback) {
149 // If namePath is not null this is *our* video
150 VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) {
151 if (err) {
8425cb89 152 logger.error('Cannot get the list of owned videos.')
c45f7f84
C
153 return callback(err)
154 }
155
156 return callback(null, videos_list)
157 })
158 }
159
c173e565
C
160 function removeOwned (id, callback) {
161 VideosDB.findByIdAndRemove(id, function (err, video) {
162 if (err) {
8425cb89 163 logger.error('Cannot remove the torrent.')
8c308c2b
C
164 return callback(err)
165 }
166
c173e565
C
167 fs.unlink(uploadDir + video.namePath, function (err) {
168 if (err) {
8425cb89 169 logger.error('Cannot remove this video file.')
c173e565
C
170 return callback(err)
171 }
8c308c2b 172
c173e565 173 callback(null)
8c308c2b
C
174 })
175 })
176 }
177
c45f7f84 178 function removeAllRemotes (callback) {
c173e565 179 VideosDB.remove({ namePath: null }, callback)
c45f7f84
C
180 }
181
182 function removeAllRemotesOf (fromUrl, callback) {
c173e565
C
183 // TODO { podUrl: { $in: urls } }
184 VideosDB.remove({ podUrl: fromUrl }, callback)
c45f7f84
C
185 }
186
8c308c2b 187 // Use the magnet Uri because the _id field is not the same on different servers
c173e565 188 function removeRemotesOfByMagnetUris (fromUrl, magnetUris, callback) {
45239549
C
189 if (callback === undefined) callback = function () {}
190
0b697522
C
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.')
8c308c2b
C
194 return callback(err)
195 }
196
0b697522
C
197 var to_remove = []
198 async.each(videos, function (video, callback_async) {
199 callback_async = dz(callback_async)
8c308c2b 200
0b697522
C
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)
8c308c2b
C
205 }
206
0b697522
C
207 callback_async()
208 }, function () {
209 VideosDB.remove({ _id: { $in: to_remove } }, function (err) {
210 if (err) {
211 logger.error('Cannot remove the remote videos.')
212 return callback(err)
213 }
214
45239549 215 logger.info('Removed remote videos from %s.', fromUrl)
0b697522
C
216 callback(null)
217 })
8c308c2b
C
218 })
219 })
220 }
221
c45f7f84 222 function search (name, callback) {
8c308c2b
C
223 VideosDB.find({ name: new RegExp(name) }, function (err, videos) {
224 if (err) {
8425cb89 225 logger.error('Cannot search the videos.')
8c308c2b
C
226 return callback(err)
227 }
228
229 return callback(null, videos)
230 })
231 }
232
c45f7f84
C
233 // ---------------------------------------------------------------------------
234
c173e565 235 module.exports = Videos
8c308c2b 236})()