]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - src/videos.js
Fix tests
[github/Chocobozzz/PeerTube.git] / src / videos.js
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 if (!err) err = new Error('Cannot find this video.')
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
117 logger.debug('Removing video %s', video.magnetUri)
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 = {
133 path: '/api/remotevideos/remove',
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) {
157 // TODO : check if the remote server has the rights to remove this video
158
159 VideosDB.findOne({ magnetUri: magnetUri }, function (err, video) {
160 if (err || !video) {
161 logger.error('Cannot find the torrent URI of this remote video.')
162 return callback(err)
163 }
164
165 if (video.podUrl !== fromUrl) {
166 logger.error('The pod has not rights on this video.')
167 return callback(err)
168 }
169
170 VideosDB.findByIdAndRemove(video._id, function (err) {
171 if (err) {
172 logger.error('Cannot remove the remote video.')
173 return callback(err)
174 }
175
176 callback(null)
177 })
178 })
179 }
180
181 // { name, magnetUri, podUrl }
182 videos.addRemote = function (data, callback) {
183 logger.debug('Add remote video from pod: %s', data.podUrl)
184
185 var params = {
186 name: data.name,
187 namePath: null,
188 description: data.description,
189 magnetUri: data.magnetUri,
190 podUrl: data.podUrl
191 }
192
193 VideosDB.create(params, function (err, video) {
194 if (err) {
195 logger.error('Cannot insert this remote video.', { error: err })
196 return callback(err)
197 }
198
199 return callback(null, video)
200 })
201 }
202
203 videos.get = function (id, callback) {
204 VideosDB.findById(id, function (err, video) {
205 if (err) {
206 logger.error('Cannot get this video.', { error: err })
207 return callback(err)
208 }
209
210 return callback(null, video)
211 })
212 }
213
214 videos.search = function (name, callback) {
215 VideosDB.find({ name: new RegExp(name) }, function (err, videos) {
216 if (err) {
217 logger.error('Cannot search the videos.', { error: err })
218 return callback(err)
219 }
220
221 return callback(null, videos)
222 })
223 }
224
225 videos.seedAll = function (final_callback) {
226 VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) {
227 if (err) {
228 logger.error('Cannot get list of the videos to seed.', { error: err })
229 return final_callback(err)
230 }
231
232 async.each(videos_list, function (video, callback) {
233 seedVideo(videos.uploadDir + video.namePath, function (err) {
234 if (err) {
235 logger.error('Cannot seed this video.', { error: err })
236 return callback(err)
237 }
238
239 callback(null)
240 })
241 }, final_callback)
242 })
243 }
244
245 module.exports = videos
246 })()