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