]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - src/videos.js
Update node modules
[github/Chocobozzz/PeerTube.git] / src / 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 webtorrent = require('./webTorrentNode')
9
10 var logger = require('./logger')
11 var pods = require('./pods')
12 var VideosDB = require('./database').VideosDB
13
14 var videos = {}
15
16 var http = config.get('webserver.https') === true ? 'https' : 'http'
17 var host = config.get('webserver.host')
18 var port = config.get('webserver.port')
19
20 // ----------- Private functions -----------
21 function seedVideo (path, callback) {
22 logger.info('Seeding %s...', path)
23
24 webtorrent.seed(path, function (torrent) {
25 logger.info('%s seeded (%s).', path, torrent.magnetURI)
26
27 return callback(null, torrent)
28 })
29 }
30
31 // ----------- Public attributes ----------
32 videos.uploadDir = __dirname + '/../' + config.get('storage.uploads')
33
34 // ----------- Public functions -----------
35 videos.list = function (callback) {
36 VideosDB.find(function (err, videos_list) {
37 if (err) {
38 logger.error('Cannot get list of the videos.', { error: err })
39 return callback(err)
40 }
41
42 return callback(null, videos_list)
43 })
44 }
45
46 videos.add = function (data, callback) {
47 var video_file = data.video
48 var video_data = data.data
49
50 logger.info('Adding %s video.', video_file.path)
51 seedVideo(video_file.path, function (err, torrent) {
52 if (err) {
53 logger.error('Cannot seed this video.', { error: err })
54 return callback(err)
55 }
56
57 var params = {
58 name: video_data.name,
59 namePath: video_file.filename,
60 description: video_data.description,
61 magnetUri: torrent.magnetURI,
62 podUrl: http + '://' + host + ':' + port
63 }
64
65 VideosDB.create(params, function (err, video) {
66 if (err) {
67 logger.error('Cannot insert this video.', { error: err })
68 return callback(err)
69 }
70
71 // Now we'll add the video's meta data to our friends
72 params.namePath = null
73
74 pods.addVideoToFriends(params)
75 callback(null)
76 })
77 })
78 }
79
80 videos.remove = function (id, callback) {
81 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
82 function removeTorrent (magnetUri, callback) {
83 try {
84 webtorrent.remove(magnetUri, callback)
85 } catch (err) {
86 logger.warn('Cannot remove the torrent from WebTorrent', { err: err })
87 return callback(null)
88 }
89 }
90
91 VideosDB.findById(id, function (err, video) {
92 if (err || !video) {
93 if (!err) err = new Error('Cannot find this video.')
94 logger.error('Cannot find this video.', { error: err })
95 return callback(err)
96 }
97
98 if (video.namePath === null) {
99 var error_string = 'Cannot remove the video of another pod.'
100 logger.error(error_string)
101 return callback(new Error(error_string))
102 }
103
104 logger.info('Removing %s video', video.name)
105
106 removeTorrent(video.magnetUri, function () {
107 VideosDB.findByIdAndRemove(id, function (err) {
108 if (err) {
109 logger.error('Cannot remove the torrent.', { error: err })
110 return callback(err)
111 }
112
113 fs.unlink(videos.uploadDir + video.namePath, function (err) {
114 if (err) {
115 logger.error('Cannot remove this video file.', { error: err })
116 return callback(err)
117 }
118
119 var params = {
120 name: video.name,
121 magnetUri: video.magnetUri
122 }
123
124 pods.removeVideoToFriends(params)
125 callback(null)
126 })
127 })
128 })
129 })
130 }
131
132 // Use the magnet Uri because the _id field is not the same on different servers
133 videos.removeRemotes = function (fromUrl, magnetUris, callback) {
134 VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) {
135 if (err || !videos) {
136 logger.error('Cannot find the torrent URI of these remote videos.')
137 return callback(err)
138 }
139
140 var to_remove = []
141 async.each(videos, function (video, callback_async) {
142 callback_async = dz(callback_async)
143
144 if (video.podUrl !== fromUrl) {
145 logger.error('The pod %s has not the rights on the video of %s.', fromUrl, video.podUrl)
146 } else {
147 to_remove.push(video._id)
148 }
149
150 callback_async()
151 }, function () {
152 VideosDB.remove({ _id: { $in: to_remove } }, function (err) {
153 if (err) {
154 logger.error('Cannot remove the remote videos.')
155 return callback(err)
156 }
157
158 callback(null)
159 })
160 })
161 })
162 }
163
164 // { name, magnetUri, podUrl }
165 videos.addRemotes = function (videos, callback) {
166 var to_add = []
167
168 async.each(videos, function (video, callback_each) {
169 callback_each = dz(callback_each)
170 logger.debug('Add remote video from pod: %s', video.podUrl)
171
172 var params = {
173 name: video.name,
174 namePath: null,
175 description: video.description,
176 magnetUri: video.magnetUri,
177 podUrl: video.podUrl
178 }
179
180 to_add.push(params)
181
182 callback_each()
183 }, function () {
184 VideosDB.create(to_add, function (err, videos) {
185 if (err) {
186 logger.error('Cannot insert this remote video.', { error: err })
187 return callback(err)
188 }
189
190 return callback(null, videos)
191 })
192 })
193 }
194
195 videos.get = function (id, callback) {
196 VideosDB.findById(id, function (err, video) {
197 if (err) {
198 logger.error('Cannot get this video.', { error: err })
199 return callback(err)
200 }
201
202 return callback(null, video)
203 })
204 }
205
206 videos.search = function (name, callback) {
207 VideosDB.find({ name: new RegExp(name) }, function (err, videos) {
208 if (err) {
209 logger.error('Cannot search the videos.', { error: err })
210 return callback(err)
211 }
212
213 return callback(null, videos)
214 })
215 }
216
217 videos.seedAll = function (callback) {
218 VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) {
219 if (err) {
220 logger.error('Cannot get list of the videos to seed.', { error: err })
221 return callback(err)
222 }
223
224 async.each(videos_list, function (video, each_callback) {
225 seedVideo(videos.uploadDir + video.namePath, function (err) {
226 if (err) {
227 logger.error('Cannot seed this video.', { error: err })
228 return callback(err)
229 }
230
231 each_callback(null)
232 })
233 }, callback)
234 })
235 }
236
237 module.exports = videos
238 })()