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