]>
Commit | Line | Data |
---|---|---|
aaf61f38 C |
1 | 'use strict' |
2 | ||
052937db | 3 | const createTorrent = require('create-torrent') |
aaf61f38 C |
4 | const ffmpeg = require('fluent-ffmpeg') |
5 | const fs = require('fs') | |
f285faa0 | 6 | const magnetUtil = require('magnet-uri') |
1a42c9e2 | 7 | const parallel = require('async/parallel') |
052937db | 8 | const parseTorrent = require('parse-torrent') |
aaf61f38 C |
9 | const pathUtils = require('path') |
10 | const mongoose = require('mongoose') | |
11 | ||
12 | const constants = require('../initializers/constants') | |
e4c55619 | 13 | const customVideosValidators = require('../helpers/custom-validators').videos |
aaf61f38 | 14 | const logger = require('../helpers/logger') |
0ff21c1c | 15 | const modelUtils = require('./utils') |
aaf61f38 | 16 | |
aaf61f38 C |
17 | // --------------------------------------------------------------------------- |
18 | ||
19 | // TODO: add indexes on searchable columns | |
20 | const VideoSchema = mongoose.Schema({ | |
21 | name: String, | |
558d7c23 C |
22 | extname: { |
23 | type: String, | |
24 | enum: [ '.mp4', '.webm', '.ogv' ] | |
25 | }, | |
26 | remoteId: mongoose.Schema.Types.ObjectId, | |
aaf61f38 | 27 | description: String, |
f285faa0 C |
28 | magnet: { |
29 | infoHash: String | |
30 | }, | |
49abbbbe | 31 | podHost: String, |
aaf61f38 C |
32 | author: String, |
33 | duration: Number, | |
aaf61f38 C |
34 | tags: [ String ], |
35 | createdDate: { | |
36 | type: Date, | |
37 | default: Date.now | |
38 | } | |
39 | }) | |
40 | ||
e4c55619 C |
41 | VideoSchema.path('name').validate(customVideosValidators.isVideoNameValid) |
42 | VideoSchema.path('description').validate(customVideosValidators.isVideoDescriptionValid) | |
49abbbbe | 43 | VideoSchema.path('podHost').validate(customVideosValidators.isVideoPodHostValid) |
e4c55619 C |
44 | VideoSchema.path('author').validate(customVideosValidators.isVideoAuthorValid) |
45 | VideoSchema.path('duration').validate(customVideosValidators.isVideoDurationValid) | |
e4c55619 | 46 | VideoSchema.path('tags').validate(customVideosValidators.isVideoTagsValid) |
aaf61f38 C |
47 | |
48 | VideoSchema.methods = { | |
f285faa0 C |
49 | generateMagnetUri, |
50 | getVideoFilename, | |
51 | getThumbnailName, | |
52 | getPreviewName, | |
558d7c23 | 53 | getTorrentName, |
c4403b29 C |
54 | isOwned, |
55 | toFormatedJSON, | |
56 | toRemoteJSON | |
aaf61f38 C |
57 | } |
58 | ||
59 | VideoSchema.statics = { | |
c4403b29 C |
60 | getDurationFromFile, |
61 | listForApi, | |
49abbbbe C |
62 | listByHostAndRemoteId, |
63 | listByHost, | |
c4403b29 C |
64 | listOwned, |
65 | listOwnedByAuthor, | |
66 | listRemotes, | |
67 | load, | |
a6375e69 | 68 | search |
aaf61f38 C |
69 | } |
70 | ||
71 | VideoSchema.pre('remove', function (next) { | |
72 | const video = this | |
73 | const tasks = [] | |
74 | ||
75 | tasks.push( | |
76 | function (callback) { | |
77 | removeThumbnail(video, callback) | |
78 | } | |
79 | ) | |
80 | ||
81 | if (video.isOwned()) { | |
82 | tasks.push( | |
83 | function (callback) { | |
84 | removeFile(video, callback) | |
85 | }, | |
86 | function (callback) { | |
87 | removeTorrent(video, callback) | |
6a94a109 C |
88 | }, |
89 | function (callback) { | |
90 | removePreview(video, callback) | |
aaf61f38 C |
91 | } |
92 | ) | |
93 | } | |
94 | ||
1a42c9e2 | 95 | parallel(tasks, next) |
aaf61f38 C |
96 | }) |
97 | ||
98 | VideoSchema.pre('save', function (next) { | |
99 | const video = this | |
100 | const tasks = [] | |
101 | ||
102 | if (video.isOwned()) { | |
f285faa0 | 103 | const videoPath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename()) |
49abbbbe | 104 | this.podHost = constants.CONFIG.WEBSERVER.HOST |
aaf61f38 C |
105 | |
106 | tasks.push( | |
052937db | 107 | // TODO: refractoring |
aaf61f38 | 108 | function (callback) { |
25cad919 C |
109 | const options = { |
110 | announceList: [ | |
3737bbaf | 111 | [ constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT + '/tracker/socket' ] |
25cad919 C |
112 | ], |
113 | urlList: [ | |
f285faa0 | 114 | constants.CONFIG.WEBSERVER.URL + constants.STATIC_PATHS.WEBSEED + video.getVideoFilename() |
25cad919 C |
115 | ] |
116 | } | |
117 | ||
118 | createTorrent(videoPath, options, function (err, torrent) { | |
052937db C |
119 | if (err) return callback(err) |
120 | ||
558d7c23 | 121 | fs.writeFile(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), torrent, function (err) { |
052937db C |
122 | if (err) return callback(err) |
123 | ||
124 | const parsedTorrent = parseTorrent(torrent) | |
f285faa0 | 125 | video.magnet.infoHash = parsedTorrent.infoHash |
052937db | 126 | |
f285faa0 | 127 | console.log(parsedTorrent) |
052937db C |
128 | callback(null) |
129 | }) | |
130 | }) | |
aaf61f38 C |
131 | }, |
132 | function (callback) { | |
558d7c23 | 133 | createThumbnail(video, videoPath, callback) |
6a94a109 C |
134 | }, |
135 | function (callback) { | |
558d7c23 | 136 | createPreview(video, videoPath, callback) |
aaf61f38 C |
137 | } |
138 | ) | |
139 | ||
558d7c23 | 140 | parallel(tasks, next) |
aaf61f38 | 141 | } else { |
558d7c23 | 142 | generateThumbnailFromBase64(video, video.thumbnail, next) |
aaf61f38 C |
143 | } |
144 | }) | |
145 | ||
146 | mongoose.model('Video', VideoSchema) | |
147 | ||
148 | // ------------------------------ METHODS ------------------------------ | |
149 | ||
f285faa0 C |
150 | function generateMagnetUri () { |
151 | let baseUrlHttp, baseUrlWs | |
152 | ||
153 | if (this.isOwned()) { | |
154 | baseUrlHttp = constants.CONFIG.WEBSERVER.URL | |
155 | baseUrlWs = constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT | |
156 | } else { | |
49abbbbe C |
157 | baseUrlHttp = constants.REMOTE_SCHEME.HTTP + '://' + this.podHost |
158 | baseUrlWs = constants.REMOTE_SCHEME.WS + this.podHost | |
f285faa0 C |
159 | } |
160 | ||
161 | const xs = baseUrlHttp + constants.STATIC_PATHS.TORRENTS + this.getTorrentName() | |
162 | const announce = baseUrlWs + '/tracker/socket' | |
163 | const urlList = [ baseUrlHttp + constants.STATIC_PATHS.WEBSEED + this.getVideoFilename() ] | |
164 | ||
165 | const magnetHash = { | |
166 | xs, | |
167 | announce, | |
168 | urlList, | |
169 | infoHash: this.magnet.infoHash, | |
170 | name: this.name | |
171 | } | |
172 | ||
173 | return magnetUtil.encode(magnetHash) | |
558d7c23 C |
174 | } |
175 | ||
f285faa0 C |
176 | function getVideoFilename () { |
177 | if (this.isOwned()) return this._id + this.extname | |
178 | ||
179 | return this.remoteId + this.extname | |
180 | } | |
181 | ||
182 | function getThumbnailName () { | |
183 | // We always have a copy of the thumbnail | |
558d7c23 C |
184 | return this._id + '.jpg' |
185 | } | |
186 | ||
f285faa0 C |
187 | function getPreviewName () { |
188 | const extension = '.jpg' | |
189 | ||
190 | if (this.isOwned()) return this._id + extension | |
191 | ||
192 | return this.remoteId + extension | |
193 | } | |
194 | ||
558d7c23 | 195 | function getTorrentName () { |
f285faa0 C |
196 | const extension = '.torrent' |
197 | ||
198 | if (this.isOwned()) return this._id + extension | |
199 | ||
200 | return this.remoteId + extension | |
558d7c23 C |
201 | } |
202 | ||
aaf61f38 | 203 | function isOwned () { |
558d7c23 | 204 | return this.remoteId === null |
aaf61f38 C |
205 | } |
206 | ||
207 | function toFormatedJSON () { | |
208 | const json = { | |
209 | id: this._id, | |
210 | name: this.name, | |
211 | description: this.description, | |
49abbbbe | 212 | podHost: this.podHost, |
aaf61f38 | 213 | isLocal: this.isOwned(), |
f285faa0 | 214 | magnetUri: this.generateMagnetUri(), |
aaf61f38 C |
215 | author: this.author, |
216 | duration: this.duration, | |
217 | tags: this.tags, | |
f285faa0 | 218 | thumbnailPath: constants.STATIC_PATHS.THUMBNAILS + '/' + this.getThumbnailName(), |
aaf61f38 C |
219 | createdDate: this.createdDate |
220 | } | |
221 | ||
222 | return json | |
223 | } | |
224 | ||
225 | function toRemoteJSON (callback) { | |
226 | const self = this | |
227 | ||
228 | // Convert thumbnail to base64 | |
f285faa0 | 229 | const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName()) |
558d7c23 | 230 | fs.readFile(thumbnailPath, function (err, thumbnailData) { |
aaf61f38 C |
231 | if (err) { |
232 | logger.error('Cannot read the thumbnail of the video') | |
233 | return callback(err) | |
234 | } | |
235 | ||
236 | const remoteVideo = { | |
237 | name: self.name, | |
238 | description: self.description, | |
f285faa0 | 239 | magnet: self.magnet, |
558d7c23 | 240 | remoteId: self._id, |
aaf61f38 C |
241 | author: self.author, |
242 | duration: self.duration, | |
243 | thumbnailBase64: new Buffer(thumbnailData).toString('base64'), | |
244 | tags: self.tags, | |
245 | createdDate: self.createdDate, | |
49abbbbe | 246 | podHost: self.podHost |
aaf61f38 C |
247 | } |
248 | ||
249 | return callback(null, remoteVideo) | |
250 | }) | |
251 | } | |
252 | ||
253 | // ------------------------------ STATICS ------------------------------ | |
254 | ||
255 | function getDurationFromFile (videoPath, callback) { | |
256 | ffmpeg.ffprobe(videoPath, function (err, metadata) { | |
257 | if (err) return callback(err) | |
258 | ||
259 | return callback(null, Math.floor(metadata.format.duration)) | |
260 | }) | |
261 | } | |
262 | ||
0ff21c1c | 263 | function listForApi (start, count, sort, callback) { |
aaf61f38 | 264 | const query = {} |
5c39adb7 | 265 | return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback) |
aaf61f38 C |
266 | } |
267 | ||
49abbbbe C |
268 | function listByHostAndRemoteId (fromHost, remoteId, callback) { |
269 | this.find({ podHost: fromHost, remoteId: remoteId }, callback) | |
aaf61f38 C |
270 | } |
271 | ||
49abbbbe C |
272 | function listByHost (fromHost, callback) { |
273 | this.find({ podHost: fromHost }, callback) | |
aaf61f38 C |
274 | } |
275 | ||
276 | function listOwned (callback) { | |
558d7c23 C |
277 | // If remoteId is null this is *our* video |
278 | this.find({ remoteId: null }, callback) | |
aaf61f38 C |
279 | } |
280 | ||
9bd26629 | 281 | function listOwnedByAuthor (author, callback) { |
558d7c23 | 282 | this.find({ remoteId: null, author: author }, callback) |
9bd26629 C |
283 | } |
284 | ||
aaf61f38 | 285 | function listRemotes (callback) { |
558d7c23 | 286 | this.find({ remoteId: { $ne: null } }, callback) |
aaf61f38 C |
287 | } |
288 | ||
289 | function load (id, callback) { | |
290 | this.findById(id, callback) | |
291 | } | |
292 | ||
293 | function search (value, field, start, count, sort, callback) { | |
294 | const query = {} | |
295 | // Make an exact search with the magnet | |
55723d16 C |
296 | if (field === 'magnetUri') { |
297 | const infoHash = magnetUtil.decode(value).infoHash | |
298 | query.magnet = { | |
299 | infoHash | |
300 | } | |
301 | } else if (field === 'tags') { | |
aaf61f38 C |
302 | query[field] = value |
303 | } else { | |
cf6412e8 | 304 | query[field] = new RegExp(value, 'i') |
aaf61f38 C |
305 | } |
306 | ||
5c39adb7 | 307 | modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback) |
aaf61f38 C |
308 | } |
309 | ||
aaf61f38 C |
310 | // --------------------------------------------------------------------------- |
311 | ||
aaf61f38 | 312 | function removeThumbnail (video, callback) { |
f285faa0 | 313 | fs.unlink(constants.CONFIG.STORAGE.THUMBNAILS_DIR + video.getThumbnailName(), callback) |
aaf61f38 C |
314 | } |
315 | ||
316 | function removeFile (video, callback) { | |
f285faa0 | 317 | fs.unlink(constants.CONFIG.STORAGE.VIDEOS_DIR + video.getVideoFilename(), callback) |
aaf61f38 C |
318 | } |
319 | ||
aaf61f38 | 320 | function removeTorrent (video, callback) { |
558d7c23 | 321 | fs.unlink(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), callback) |
aaf61f38 C |
322 | } |
323 | ||
6a94a109 C |
324 | function removePreview (video, callback) { |
325 | // Same name than video thumnail | |
f285faa0 | 326 | fs.unlink(constants.CONFIG.STORAGE.PREVIEWS_DIR + video.getPreviewName(), callback) |
6a94a109 C |
327 | } |
328 | ||
558d7c23 | 329 | function createPreview (video, videoPath, callback) { |
f285faa0 | 330 | generateImage(video, videoPath, constants.CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName(), callback) |
6a94a109 C |
331 | } |
332 | ||
558d7c23 | 333 | function createThumbnail (video, videoPath, callback) { |
f285faa0 | 334 | generateImage(video, videoPath, constants.CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName(), constants.THUMBNAILS_SIZE, callback) |
aaf61f38 C |
335 | } |
336 | ||
558d7c23 C |
337 | function generateThumbnailFromBase64 (video, thumbnailData, callback) { |
338 | // Creating the thumbnail for this remote video) | |
339 | ||
f285faa0 | 340 | const thumbnailName = video.getThumbnailName() |
558d7c23 C |
341 | const thumbnailPath = constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName |
342 | fs.writeFile(thumbnailPath, thumbnailData, { encoding: 'base64' }, function (err) { | |
aaf61f38 C |
343 | if (err) return callback(err) |
344 | ||
558d7c23 C |
345 | return callback(null, thumbnailName) |
346 | }) | |
347 | } | |
348 | ||
f285faa0 | 349 | function generateImage (video, videoPath, folder, imageName, size, callback) { |
558d7c23 | 350 | const options = { |
f285faa0 | 351 | filename: imageName, |
558d7c23 C |
352 | count: 1, |
353 | folder | |
354 | } | |
aaf61f38 | 355 | |
558d7c23 C |
356 | if (!callback) { |
357 | callback = size | |
358 | } else { | |
359 | options.size = size | |
360 | } | |
361 | ||
362 | ffmpeg(videoPath) | |
363 | .on('error', callback) | |
364 | .on('end', function () { | |
f285faa0 | 365 | callback(null, imageName) |
aaf61f38 | 366 | }) |
558d7c23 | 367 | .thumbnail(options) |
aaf61f38 | 368 | } |