]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - src/videos.js
Update node modules
[github/Chocobozzz/PeerTube.git] / src / videos.js
CommitLineData
8c308c2b
C
1;(function () {
2 'use strict'
3
a1860380
C
4 var async = require('async')
5 var config = require('config')
0b697522 6 var dz = require('dezalgo')
8c308c2b
C
7 var fs = require('fs')
8 var webtorrent = require('./webTorrentNode')
8c308c2b
C
9
10 var logger = require('./logger')
8c308c2b 11 var pods = require('./pods')
a1860380 12 var VideosDB = require('./database').VideosDB
8c308c2b
C
13
14 var videos = {}
a1860380 15
8c308c2b
C
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) {
a1860380 22 logger.info('Seeding %s...', path)
8c308c2b
C
23
24 webtorrent.seed(path, function (torrent) {
a1860380 25 logger.info('%s seeded (%s).', path, torrent.magnetURI)
8c308c2b
C
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
a1860380 50 logger.info('Adding %s video.', video_file.path)
8c308c2b
C
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,
207fbab4 59 namePath: video_file.filename,
8c308c2b
C
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
0b697522 71 // Now we'll add the video's meta data to our friends
8c308c2b
C
72 params.namePath = null
73
0b697522 74 pods.addVideoToFriends(params)
3bcb78b3 75 callback(null)
8c308c2b
C
76 })
77 })
78 }
79
80 videos.remove = function (id, callback) {
a1860380 81 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
8c308c2b
C
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) {
77c2df95 93 if (!err) err = new Error('Cannot find this video.')
8c308c2b
C
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
a1860380 104 logger.info('Removing %s video', video.name)
8c308c2b
C
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
0b697522
C
119 var params = {
120 name: video.name,
121 magnetUri: video.magnetUri
8c308c2b
C
122 }
123
0b697522 124 pods.removeVideoToFriends(params)
3bcb78b3 125 callback(null)
8c308c2b
C
126 })
127 })
128 })
129 })
130 }
131
132 // Use the magnet Uri because the _id field is not the same on different servers
0b697522
C
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.')
8c308c2b
C
137 return callback(err)
138 }
139
0b697522
C
140 var to_remove = []
141 async.each(videos, function (video, callback_async) {
142 callback_async = dz(callback_async)
8c308c2b 143
0b697522
C
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)
8c308c2b
C
148 }
149
0b697522
C
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 })
8c308c2b
C
160 })
161 })
162 }
163
164 // { name, magnetUri, podUrl }
0b697522
C
165 videos.addRemotes = function (videos, callback) {
166 var to_add = []
8c308c2b 167
0b697522
C
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
8c308c2b
C
178 }
179
0b697522
C
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 })
8c308c2b
C
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
a1860380 217 videos.seedAll = function (callback) {
8c308c2b
C
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 })
a1860380 221 return callback(err)
8c308c2b
C
222 }
223
a1860380 224 async.each(videos_list, function (video, each_callback) {
8c308c2b
C
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
a1860380 231 each_callback(null)
8c308c2b 232 })
a1860380 233 }, callback)
8c308c2b
C
234 })
235 }
236
237 module.exports = videos
238})()