]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - models/videos.js
Standard v6
[github/Chocobozzz/PeerTube.git] / models / videos.js
1 ;(function () {
2 'use strict'
3
4 var async = require('async')
5 var config = require('config')
6 var dz = require('dezalgo')
7 var fs = require('fs')
8 var mongoose = require('mongoose')
9 var path = require('path')
10
11 var logger = require('../helpers/logger')
12
13 var http = config.get('webserver.https') === true ? 'https' : 'http'
14 var host = config.get('webserver.host')
15 var port = config.get('webserver.port')
16 var uploadDir = path.join(__dirname, '..', config.get('storage.uploads'))
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 // ---------------------------------------------------------------------------
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
62 // TODO: avoid doublons
63 function addRemotes (videos, callback) {
64 if (!callback) callback = function () {}
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) {
86 logger.error('Cannot insert this remote video.')
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) {
98 logger.error('Cannot get this video.')
99 return callback(err)
100 }
101
102 return callback(null, video)
103 })
104 }
105
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.')
124 logger.error('Cannot find this video.')
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)
131 return callback(new Error(error_string), false, video)
132 }
133
134 callback(null, true, video)
135 })
136 }
137
138 function list (callback) {
139 VideosDB.find(function (err, videos_list) {
140 if (err) {
141 logger.error('Cannot get the list of the videos.')
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) {
153 logger.error('Cannot get the list of owned videos.')
154 return callback(err)
155 }
156
157 return callback(null, videos_list)
158 })
159 }
160
161 function removeOwned (id, callback) {
162 VideosDB.findByIdAndRemove(id, function (err, video) {
163 if (err) {
164 logger.error('Cannot remove the torrent.')
165 return callback(err)
166 }
167
168 fs.unlink(uploadDir + video.namePath, function (err) {
169 if (err) {
170 logger.error('Cannot remove this video file.')
171 return callback(err)
172 }
173
174 callback(null)
175 })
176 })
177 }
178
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
223 function search (name, callback) {
224 VideosDB.find({ name: new RegExp(name) }, function (err, videos) {
225 if (err) {
226 logger.error('Cannot search the videos.')
227 return callback(err)
228 }
229
230 return callback(null, videos)
231 })
232 }
233
234 // ---------------------------------------------------------------------------
235
236 module.exports = Videos
237 })()