aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/videos.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/videos.js')
-rw-r--r--src/videos.js245
1 files changed, 245 insertions, 0 deletions
diff --git a/src/videos.js b/src/videos.js
new file mode 100644
index 000000000..605ce098f
--- /dev/null
+++ b/src/videos.js
@@ -0,0 +1,245 @@
1;(function () {
2 'use strict'
3
4 var fs = require('fs')
5 var webtorrent = require('./webTorrentNode')
6 var config = require('config')
7 var async = require('async')
8
9 var logger = require('./logger')
10 var VideosDB = require('./database').VideosDB
11 var pods = require('./pods')
12
13 var videos = {}
14 // Public url
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) {
21 logger.debug('Seeding : %s', path)
22
23 webtorrent.seed(path, function (torrent) {
24 logger.debug('Seeded : %s', torrent.magnetURI)
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
49 logger.debug('Path: %s', video_file.path)
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
73 logger.debug('Sending this video Uri to friends...')
74
75 var data = {
76 path: '/api/remotevideos/add',
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) {
94 // Maybe the torrent is not seeding, it doesn't have importance
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) {
106 logger.error('Cannot find this video.', { error: err })
107 return callback(err)
108 }
109
110 if (video.namePath === null) {
111 var error_string = 'Cannot remove the video of another pod.'
112 logger.error(error_string)
113 return callback(new Error(error_string))
114 }
115
116 logger.debug('Removing video %s', video.magnetUri)
117
118 removeTorrent(video.magnetUri, function () {
119 VideosDB.findByIdAndRemove(id, function (err) {
120 if (err) {
121 logger.error('Cannot remove the torrent.', { error: err })
122 return callback(err)
123 }
124
125 fs.unlink(videos.uploadDir + video.namePath, function (err) {
126 if (err) {
127 logger.error('Cannot remove this video file.', { error: err })
128 return callback(err)
129 }
130
131 var data = {
132 path: '/api/remotevideos/remove',
133 method: 'POST',
134 data: {
135 magnetUri: video.magnetUri
136 }
137 }
138
139 // Yes this is a POST request because we add some informations in the body (signature, encrypt etc)
140 pods.makeSecureRequest(data, function (err) {
141 if (err) {
142 logger.error('Somes issues when sending we want to remove the video to friends.', { error: err })
143 return callback(err)
144 }
145
146 callback(null)
147 })
148 })
149 })
150 })
151 })
152 }
153
154 // Use the magnet Uri because the _id field is not the same on different servers
155 videos.removeRemote = function (fromUrl, magnetUri, callback) {
156 // TODO : check if the remote server has the rights to remove this video
157
158 VideosDB.findOne({ magnetUri: magnetUri }, function (err, video) {
159 if (err || !video) {
160 logger.error('Cannot find the torrent URI of this remote video.')
161 return callback(err)
162 }
163
164 if (video.podUrl !== fromUrl) {
165 logger.error('The pod has not rights on this video.')
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
224 videos.seedAll = function (final_callback) {
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 })
228 return final_callback(err)
229 }
230
231 async.each(videos_list, function (video, callback) {
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
238 callback(null)
239 })
240 }, final_callback)
241 })
242 }
243
244 module.exports = videos
245})()