aboutsummaryrefslogtreecommitdiffhomepage
path: root/models/videos.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-02-04 21:10:33 +0100
committerChocobozzz <florian.bigard@gmail.com>2016-02-04 21:10:33 +0100
commitc173e56520b0fe4206b9ea8049b6add40bfeabcd (patch)
tree264c6cbf1bf81a6522685b4be5771bbeef4cd5dc /models/videos.js
parentc45f7f84001c2731909db04dd82e1c1f290386eb (diff)
downloadPeerTube-c173e56520b0fe4206b9ea8049b6add40bfeabcd.tar.gz
PeerTube-c173e56520b0fe4206b9ea8049b6add40bfeabcd.tar.zst
PeerTube-c173e56520b0fe4206b9ea8049b6add40bfeabcd.zip
Split models
Diffstat (limited to 'models/videos.js')
-rw-r--r--models/videos.js202
1 files changed, 76 insertions, 126 deletions
diff --git a/models/videos.js b/models/videos.js
index 5711c5657..10abee6e7 100644
--- a/models/videos.js
+++ b/models/videos.js
@@ -5,71 +5,62 @@
5 var config = require('config') 5 var config = require('config')
6 var dz = require('dezalgo') 6 var dz = require('dezalgo')
7 var fs = require('fs') 7 var fs = require('fs')
8 var webtorrent = require('../lib/webTorrentNode') 8 var mongoose = require('mongoose')
9 9
10 var logger = require('../helpers/logger') 10 var logger = require('../helpers/logger')
11 var pods = require('./pods')
12 var VideosDB = require('../initializers/database').VideosDB
13 11
14 var http = config.get('webserver.https') === true ? 'https' : 'http' 12 var http = config.get('webserver.https') === true ? 'https' : 'http'
15 var host = config.get('webserver.host') 13 var host = config.get('webserver.host')
16 var port = config.get('webserver.port') 14 var port = config.get('webserver.port')
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 // ---------------------------------------------------------------------------
17 29
18 var videos = { 30 var Videos = {
19 add: add, 31 add: add,
20 addRemotes: addRemotes, 32 addRemotes: addRemotes,
21 get: get, 33 get: get,
34 getVideoState: getVideoState,
35 isOwned: isOwned,
22 list: list, 36 list: list,
23 listOwned: listOwned, 37 listOwned: listOwned,
24 remove: remove, 38 removeOwned: removeOwned,
25 removeAllRemotes: removeAllRemotes, 39 removeAllRemotes: removeAllRemotes,
26 removeAllRemotesOf: removeAllRemotesOf, 40 removeAllRemotesOf: removeAllRemotesOf,
27 removeRemotes: removeRemotes, 41 removeRemotesOfByMagnetUris: removeRemotesOfByMagnetUris,
28 search: search, 42 search: search
29 seedAll: seedAll,
30 uploadDir: uploadDir
31 } 43 }
32 44
33 // ----------- Public attributes ---------- 45 function add (video, callback) {
34 var uploadDir = __dirname + '/../' + config.get('storage.uploads') 46 logger.info('Adding %s video to database.', video.name)
35 47
36 function add (data, callback) { 48 var params = video
37 var video_file = data.video 49 params.podUrl = http + '://' + host + ':' + port
38 var video_data = data.data
39 50
40 logger.info('Adding %s video.', video_file.path) 51 VideosDB.create(params, function (err, video) {
41 seedVideo(video_file.path, function (err, torrent) {
42 if (err) { 52 if (err) {
43 logger.error('Cannot seed this video.', { error: err }) 53 logger.error('Cannot insert this video into database.', { error: err })
44 return callback(err) 54 return callback(err)
45 } 55 }
46 56
47 var params = { 57 callback(null)
48 name: video_data.name,
49 namePath: video_file.filename,
50 description: video_data.description,
51 magnetUri: torrent.magnetURI,
52 podUrl: http + '://' + host + ':' + port
53 }
54
55 VideosDB.create(params, function (err, video) {
56 if (err) {
57 logger.error('Cannot insert this video.', { error: err })
58 return callback(err)
59 }
60
61 // Now we'll add the video's meta data to our friends
62 params.namePath = null
63
64 pods.addVideoToFriends(params)
65 callback(null)
66 })
67 }) 58 })
68 } 59 }
69 60
70 // TODO: avoid doublons 61 // TODO: avoid doublons
71 function addRemotes (videos, callback) { 62 function addRemotes (videos, callback) {
72 if (callback === undefined) callback = function () {} 63 if (!callback) callback = function () {}
73 64
74 var to_add = [] 65 var to_add = []
75 66
@@ -111,6 +102,38 @@
111 }) 102 })
112 } 103 }
113 104
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.')
123 logger.error('Cannot find this video.', { error: err })
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(null, false, video)
131 }
132
133 callback(null, true, video)
134 })
135 }
136
114 function list (callback) { 137 function list (callback) {
115 VideosDB.find(function (err, videos_list) { 138 VideosDB.find(function (err, videos_list) {
116 if (err) { 139 if (err) {
@@ -134,76 +157,35 @@
134 }) 157 })
135 } 158 }
136 159
137 function remove (id, callback) { 160 function removeOwned (id, callback) {
138 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process 161 VideosDB.findByIdAndRemove(id, function (err, video) {
139 function removeTorrent (magnetUri, callback) { 162 if (err) {
140 try { 163 logger.error('Cannot remove the torrent.', { error: err })
141 webtorrent.remove(magnetUri, callback)
142 } catch (err) {
143 logger.warn('Cannot remove the torrent from WebTorrent', { err: err })
144 return callback(null)
145 }
146 }
147
148 VideosDB.findById(id, function (err, video) {
149 if (err || !video) {
150 if (!err) err = new Error('Cannot find this video.')
151 logger.error('Cannot find this video.', { error: err })
152 return callback(err) 164 return callback(err)
153 } 165 }
154 166
155 if (video.namePath === null) { 167 fs.unlink(uploadDir + video.namePath, function (err) {
156 var error_string = 'Cannot remove the video of another pod.' 168 if (err) {
157 logger.error(error_string) 169 logger.error('Cannot remove this video file.', { error: err })
158 return callback(new Error(error_string)) 170 return callback(err)
159 } 171 }
160
161 logger.info('Removing %s video', video.name)
162
163 removeTorrent(video.magnetUri, function () {
164 VideosDB.findByIdAndRemove(id, function (err) {
165 if (err) {
166 logger.error('Cannot remove the torrent.', { error: err })
167 return callback(err)
168 }
169
170 fs.unlink(uploadDir + video.namePath, function (err) {
171 if (err) {
172 logger.error('Cannot remove this video file.', { error: err })
173 return callback(err)
174 }
175
176 var params = {
177 name: video.name,
178 magnetUri: video.magnetUri
179 }
180 172
181 pods.removeVideoToFriends(params) 173 callback(null)
182 callback(null)
183 })
184 })
185 }) 174 })
186 }) 175 })
187 } 176 }
188 177
189 function removeAllRemotes (callback) { 178 function removeAllRemotes (callback) {
190 VideosDB.remove({ namePath: null }, function (err) { 179 VideosDB.remove({ namePath: null }, callback)
191 if (err) return callback(err)
192
193 callback(null)
194 })
195 } 180 }
196 181
197 function removeAllRemotesOf (fromUrl, callback) { 182 function removeAllRemotesOf (fromUrl, callback) {
198 VideosDB.remove({ podUrl: fromUrl }, function (err) { 183 // TODO { podUrl: { $in: urls } }
199 if (err) return callback(err) 184 VideosDB.remove({ podUrl: fromUrl }, callback)
200
201 callback(null)
202 })
203 } 185 }
204 186
205 // Use the magnet Uri because the _id field is not the same on different servers 187 // Use the magnet Uri because the _id field is not the same on different servers
206 function removeRemotes (fromUrl, magnetUris, callback) { 188 function removeRemotesOfByMagnetUris (fromUrl, magnetUris, callback) {
207 if (callback === undefined) callback = function () {} 189 if (callback === undefined) callback = function () {}
208 190
209 VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) { 191 VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) {
@@ -248,39 +230,7 @@
248 }) 230 })
249 } 231 }
250 232
251 function seedAll (callback) {
252 VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) {
253 if (err) {
254 logger.error('Cannot get list of the videos to seed.', { error: err })
255 return callback(err)
256 }
257
258 async.each(videos_list, function (video, each_callback) {
259 seedVideo(uploadDir + video.namePath, function (err) {
260 if (err) {
261 logger.error('Cannot seed this video.', { error: err })
262 return callback(err)
263 }
264
265 each_callback(null)
266 })
267 }, callback)
268 })
269 }
270
271 // --------------------------------------------------------------------------- 233 // ---------------------------------------------------------------------------
272 234
273 module.exports = videos 235 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 }
286})() 236})()