]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/videos.js
Prepare folders structure for angular app
[github/Chocobozzz/PeerTube.git] / server / models / videos.js
CommitLineData
9f10b292
C
1'use strict'
2
3var async = require('async')
4var config = require('config')
5var dz = require('dezalgo')
6var fs = require('fs')
7var mongoose = require('mongoose')
8var path = require('path')
9
10var logger = require('../helpers/logger')
11
12var http = config.get('webserver.https') === true ? 'https' : 'http'
13var host = config.get('webserver.host')
14var port = config.get('webserver.port')
15var uploadDir = path.join(__dirname, '..', config.get('storage.uploads'))
16
17// ---------------------------------------------------------------------------
18
19var videosSchema = mongoose.Schema({
20 name: String,
21 namePath: String,
22 description: String,
23 magnetUri: String,
24 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)
c173e565 58 })
9f10b292 59}
8c308c2b 60
9f10b292
C
61// TODO: avoid doublons
62function addRemotes (videos, callback) {
63 if (!callback) callback = function () {}
c45f7f84 64
9f10b292 65 var to_add = []
c45f7f84 66
9f10b292
C
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)
c45f7f84 70
9f10b292
C
71 var params = {
72 name: video.name,
73 namePath: null,
74 description: video.description,
75 magnetUri: video.magnetUri,
76 podUrl: video.podUrl
77 }
c45f7f84 78
9f10b292 79 to_add.push(params)
c45f7f84 80
9f10b292
C
81 callback_each()
82 }, function () {
83 VideosDB.create(to_add, function (err, videos) {
c45f7f84 84 if (err) {
9f10b292 85 logger.error('Cannot insert this remote video.')
c45f7f84
C
86 return callback(err)
87 }
88
9f10b292 89 return callback(null, videos)
c45f7f84 90 })
9f10b292
C
91 })
92}
c45f7f84 93
9f10b292
C
94function get (id, callback) {
95 VideosDB.findById(id, function (err, video) {
96 if (err) {
97 logger.error('Cannot get this video.')
98 return callback(err)
99 }
c173e565 100
9f10b292
C
101 return callback(null, video)
102 })
103}
c173e565 104
9f10b292
C
105function getVideoState (id, callback) {
106 get(id, function (err, video) {
107 if (err) return callback(err)
c173e565 108
9f10b292
C
109 var exist = (video !== null)
110 var owned = false
111 if (exist === true) {
112 owned = (video.namePath !== null)
113 }
c173e565 114
9f10b292
C
115 return callback(null, { exist: exist, owned: owned })
116 })
117}
118
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}
c173e565 136
9f10b292
C
137function list (callback) {
138 VideosDB.find(function (err, videos_list) {
139 if (err) {
140 logger.error('Cannot get the list of the videos.')
141 return callback(err)
142 }
c173e565 143
9f10b292
C
144 return callback(null, videos_list)
145 })
146}
c45f7f84 147
9f10b292
C
148function 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) {
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 }
c45f7f84 166
9f10b292 167 fs.unlink(uploadDir + video.namePath, function (err) {
c45f7f84 168 if (err) {
9f10b292 169 logger.error('Cannot remove this video file.')
c45f7f84
C
170 return callback(err)
171 }
172
9f10b292 173 callback(null)
c45f7f84 174 })
9f10b292
C
175 })
176}
177
178function removeAllRemotes (callback) {
179 VideosDB.remove({ namePath: null }, callback)
180}
181
182function removeAllRemotesOf (fromUrl, callback) {
9f10b292
C
183 VideosDB.remove({ podUrl: fromUrl }, callback)
184}
185
186// Use the magnet Uri because the _id field is not the same on different servers
187function removeRemotesOfByMagnetUris (fromUrl, magnetUris, callback) {
188 if (callback === undefined) callback = function () {}
189
190 VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) {
191 if (err || !videos) {
192 logger.error('Cannot find the torrent URI of these remote videos.')
193 return callback(err)
194 }
195
196 var to_remove = []
197 async.each(videos, function (video, callback_async) {
198 callback_async = dz(callback_async)
199
200 if (video.podUrl !== fromUrl) {
201 logger.error('The pod %s has not the rights on the video of %s.', fromUrl, video.podUrl)
202 } else {
203 to_remove.push(video._id)
8c308c2b
C
204 }
205
9f10b292
C
206 callback_async()
207 }, function () {
208 VideosDB.remove({ _id: { $in: to_remove } }, function (err) {
c173e565 209 if (err) {
9f10b292 210 logger.error('Cannot remove the remote videos.')
c173e565
C
211 return callback(err)
212 }
8c308c2b 213
9f10b292 214 logger.info('Removed remote videos from %s.', fromUrl)
c173e565 215 callback(null)
8c308c2b
C
216 })
217 })
9f10b292
C
218 })
219}
8c308c2b 220
9f10b292
C
221function search (name, callback) {
222 VideosDB.find({ name: new RegExp(name) }, function (err, videos) {
223 if (err) {
224 logger.error('Cannot search the videos.')
225 return callback(err)
226 }
8c308c2b 227
9f10b292
C
228 return callback(null, videos)
229 })
230}
8c308c2b 231
9f10b292 232// ---------------------------------------------------------------------------
c45f7f84 233
9f10b292 234module.exports = Videos