]>
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') |
7920c273 | 7 | const map = require('lodash/map') |
1a42c9e2 | 8 | const parallel = require('async/parallel') |
052937db | 9 | const parseTorrent = require('parse-torrent') |
aaf61f38 | 10 | const pathUtils = require('path') |
aaf61f38 C |
11 | |
12 | const constants = require('../initializers/constants') | |
aaf61f38 | 13 | const logger = require('../helpers/logger') |
0ff21c1c | 14 | const modelUtils = require('./utils') |
aaf61f38 | 15 | |
aaf61f38 C |
16 | // --------------------------------------------------------------------------- |
17 | ||
feb4bdfd | 18 | module.exports = function (sequelize, DataTypes) { |
b769007f | 19 | // TODO: add indexes on searchable columns |
feb4bdfd C |
20 | const Video = sequelize.define('Video', |
21 | { | |
22 | id: { | |
23 | type: DataTypes.UUID, | |
24 | defaultValue: DataTypes.UUIDV4, | |
25 | primaryKey: true | |
aaf61f38 | 26 | }, |
feb4bdfd C |
27 | name: { |
28 | type: DataTypes.STRING | |
6a94a109 | 29 | }, |
feb4bdfd C |
30 | extname: { |
31 | // TODO: enum? | |
32 | type: DataTypes.STRING | |
33 | }, | |
34 | remoteId: { | |
35 | type: DataTypes.UUID | |
36 | }, | |
37 | description: { | |
38 | type: DataTypes.STRING | |
39 | }, | |
40 | infoHash: { | |
41 | type: DataTypes.STRING | |
42 | }, | |
43 | duration: { | |
44 | type: DataTypes.INTEGER | |
aaf61f38 | 45 | } |
feb4bdfd C |
46 | }, |
47 | { | |
48 | classMethods: { | |
49 | associate, | |
50 | ||
51 | generateThumbnailFromBase64, | |
52 | getDurationFromFile, | |
b769007f | 53 | list, |
feb4bdfd C |
54 | listForApi, |
55 | listByHostAndRemoteId, | |
7920c273 | 56 | listOwnedAndPopulateAuthorAndTags, |
feb4bdfd C |
57 | listOwnedByAuthor, |
58 | load, | |
59 | loadAndPopulateAuthor, | |
7920c273 C |
60 | loadAndPopulateAuthorAndPodAndTags, |
61 | searchAndPopulateAuthorAndPodAndTags | |
feb4bdfd C |
62 | }, |
63 | instanceMethods: { | |
64 | generateMagnetUri, | |
65 | getVideoFilename, | |
66 | getThumbnailName, | |
67 | getPreviewName, | |
68 | getTorrentName, | |
69 | isOwned, | |
70 | toFormatedJSON, | |
71 | toRemoteJSON | |
72 | }, | |
73 | hooks: { | |
74 | beforeCreate, | |
75 | afterDestroy | |
76 | } | |
77 | } | |
78 | ) | |
aaf61f38 | 79 | |
feb4bdfd C |
80 | return Video |
81 | } | |
aaf61f38 | 82 | |
feb4bdfd C |
83 | // TODO: Validation |
84 | // VideoSchema.path('name').validate(customVideosValidators.isVideoNameValid) | |
85 | // VideoSchema.path('description').validate(customVideosValidators.isVideoDescriptionValid) | |
86 | // VideoSchema.path('podHost').validate(customVideosValidators.isVideoPodHostValid) | |
87 | // VideoSchema.path('author').validate(customVideosValidators.isVideoAuthorValid) | |
88 | // VideoSchema.path('duration').validate(customVideosValidators.isVideoDurationValid) | |
89 | // VideoSchema.path('tags').validate(customVideosValidators.isVideoTagsValid) | |
90 | ||
91 | function beforeCreate (video, options, next) { | |
aaf61f38 C |
92 | const tasks = [] |
93 | ||
94 | if (video.isOwned()) { | |
f285faa0 | 95 | const videoPath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename()) |
aaf61f38 C |
96 | |
97 | tasks.push( | |
052937db | 98 | // TODO: refractoring |
aaf61f38 | 99 | function (callback) { |
25cad919 C |
100 | const options = { |
101 | announceList: [ | |
3737bbaf | 102 | [ constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT + '/tracker/socket' ] |
25cad919 C |
103 | ], |
104 | urlList: [ | |
f285faa0 | 105 | constants.CONFIG.WEBSERVER.URL + constants.STATIC_PATHS.WEBSEED + video.getVideoFilename() |
25cad919 C |
106 | ] |
107 | } | |
108 | ||
109 | createTorrent(videoPath, options, function (err, torrent) { | |
052937db C |
110 | if (err) return callback(err) |
111 | ||
558d7c23 | 112 | fs.writeFile(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), torrent, function (err) { |
052937db C |
113 | if (err) return callback(err) |
114 | ||
115 | const parsedTorrent = parseTorrent(torrent) | |
feb4bdfd | 116 | video.infoHash = parsedTorrent.infoHash |
052937db C |
117 | |
118 | callback(null) | |
119 | }) | |
120 | }) | |
aaf61f38 C |
121 | }, |
122 | function (callback) { | |
558d7c23 | 123 | createThumbnail(video, videoPath, callback) |
6a94a109 C |
124 | }, |
125 | function (callback) { | |
558d7c23 | 126 | createPreview(video, videoPath, callback) |
aaf61f38 C |
127 | } |
128 | ) | |
129 | ||
c77fa067 | 130 | return parallel(tasks, next) |
aaf61f38 | 131 | } |
c77fa067 C |
132 | |
133 | return next() | |
feb4bdfd | 134 | } |
aaf61f38 | 135 | |
feb4bdfd C |
136 | function afterDestroy (video, options, next) { |
137 | const tasks = [] | |
138 | ||
139 | tasks.push( | |
140 | function (callback) { | |
141 | removeThumbnail(video, callback) | |
142 | } | |
143 | ) | |
144 | ||
145 | if (video.isOwned()) { | |
146 | tasks.push( | |
147 | function (callback) { | |
148 | removeFile(video, callback) | |
149 | }, | |
150 | function (callback) { | |
151 | removeTorrent(video, callback) | |
152 | }, | |
153 | function (callback) { | |
154 | removePreview(video, callback) | |
155 | } | |
156 | ) | |
157 | } | |
158 | ||
159 | parallel(tasks, next) | |
160 | } | |
aaf61f38 C |
161 | |
162 | // ------------------------------ METHODS ------------------------------ | |
163 | ||
feb4bdfd C |
164 | function associate (models) { |
165 | this.belongsTo(models.Author, { | |
166 | foreignKey: { | |
167 | name: 'authorId', | |
168 | allowNull: false | |
169 | }, | |
170 | onDelete: 'cascade' | |
171 | }) | |
7920c273 C |
172 | |
173 | this.belongsToMany(models.Tag, { | |
174 | foreignKey: 'videoId', | |
175 | through: models.VideoTag, | |
176 | onDelete: 'cascade' | |
177 | }) | |
feb4bdfd C |
178 | } |
179 | ||
f285faa0 C |
180 | function generateMagnetUri () { |
181 | let baseUrlHttp, baseUrlWs | |
182 | ||
183 | if (this.isOwned()) { | |
184 | baseUrlHttp = constants.CONFIG.WEBSERVER.URL | |
185 | baseUrlWs = constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT | |
186 | } else { | |
feb4bdfd C |
187 | baseUrlHttp = constants.REMOTE_SCHEME.HTTP + '://' + this.Author.Pod.host |
188 | baseUrlWs = constants.REMOTE_SCHEME.WS + '://' + this.Author.Pod.host | |
f285faa0 C |
189 | } |
190 | ||
191 | const xs = baseUrlHttp + constants.STATIC_PATHS.TORRENTS + this.getTorrentName() | |
192 | const announce = baseUrlWs + '/tracker/socket' | |
193 | const urlList = [ baseUrlHttp + constants.STATIC_PATHS.WEBSEED + this.getVideoFilename() ] | |
194 | ||
195 | const magnetHash = { | |
196 | xs, | |
197 | announce, | |
198 | urlList, | |
feb4bdfd | 199 | infoHash: this.infoHash, |
f285faa0 C |
200 | name: this.name |
201 | } | |
202 | ||
203 | return magnetUtil.encode(magnetHash) | |
558d7c23 C |
204 | } |
205 | ||
f285faa0 | 206 | function getVideoFilename () { |
feb4bdfd | 207 | if (this.isOwned()) return this.id + this.extname |
f285faa0 C |
208 | |
209 | return this.remoteId + this.extname | |
210 | } | |
211 | ||
212 | function getThumbnailName () { | |
213 | // We always have a copy of the thumbnail | |
feb4bdfd | 214 | return this.id + '.jpg' |
558d7c23 C |
215 | } |
216 | ||
f285faa0 C |
217 | function getPreviewName () { |
218 | const extension = '.jpg' | |
219 | ||
feb4bdfd | 220 | if (this.isOwned()) return this.id + extension |
f285faa0 C |
221 | |
222 | return this.remoteId + extension | |
223 | } | |
224 | ||
558d7c23 | 225 | function getTorrentName () { |
f285faa0 C |
226 | const extension = '.torrent' |
227 | ||
feb4bdfd | 228 | if (this.isOwned()) return this.id + extension |
f285faa0 C |
229 | |
230 | return this.remoteId + extension | |
558d7c23 C |
231 | } |
232 | ||
aaf61f38 | 233 | function isOwned () { |
558d7c23 | 234 | return this.remoteId === null |
aaf61f38 C |
235 | } |
236 | ||
237 | function toFormatedJSON () { | |
feb4bdfd C |
238 | let podHost |
239 | ||
240 | if (this.Author.Pod) { | |
241 | podHost = this.Author.Pod.host | |
242 | } else { | |
243 | // It means it's our video | |
244 | podHost = constants.CONFIG.WEBSERVER.HOST | |
245 | } | |
246 | ||
aaf61f38 | 247 | const json = { |
feb4bdfd | 248 | id: this.id, |
aaf61f38 C |
249 | name: this.name, |
250 | description: this.description, | |
feb4bdfd | 251 | podHost, |
aaf61f38 | 252 | isLocal: this.isOwned(), |
f285faa0 | 253 | magnetUri: this.generateMagnetUri(), |
feb4bdfd | 254 | author: this.Author.name, |
aaf61f38 | 255 | duration: this.duration, |
7920c273 | 256 | tags: map(this.Tags, 'name'), |
f285faa0 | 257 | thumbnailPath: constants.STATIC_PATHS.THUMBNAILS + '/' + this.getThumbnailName(), |
feb4bdfd | 258 | createdAt: this.createdAt |
aaf61f38 C |
259 | } |
260 | ||
261 | return json | |
262 | } | |
263 | ||
264 | function toRemoteJSON (callback) { | |
265 | const self = this | |
266 | ||
267 | // Convert thumbnail to base64 | |
f285faa0 | 268 | const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName()) |
558d7c23 | 269 | fs.readFile(thumbnailPath, function (err, thumbnailData) { |
aaf61f38 C |
270 | if (err) { |
271 | logger.error('Cannot read the thumbnail of the video') | |
272 | return callback(err) | |
273 | } | |
274 | ||
275 | const remoteVideo = { | |
276 | name: self.name, | |
277 | description: self.description, | |
feb4bdfd C |
278 | infoHash: self.infoHash, |
279 | remoteId: self.id, | |
280 | author: self.Author.name, | |
aaf61f38 C |
281 | duration: self.duration, |
282 | thumbnailBase64: new Buffer(thumbnailData).toString('base64'), | |
7920c273 | 283 | tags: map(self.Tags, 'name'), |
feb4bdfd | 284 | createdAt: self.createdAt, |
8f217302 | 285 | extname: self.extname |
aaf61f38 C |
286 | } |
287 | ||
288 | return callback(null, remoteVideo) | |
289 | }) | |
290 | } | |
291 | ||
292 | // ------------------------------ STATICS ------------------------------ | |
293 | ||
c77fa067 C |
294 | function generateThumbnailFromBase64 (video, thumbnailData, callback) { |
295 | // Creating the thumbnail for a remote video | |
296 | ||
297 | const thumbnailName = video.getThumbnailName() | |
298 | const thumbnailPath = constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName | |
299 | fs.writeFile(thumbnailPath, thumbnailData, { encoding: 'base64' }, function (err) { | |
300 | if (err) return callback(err) | |
301 | ||
302 | return callback(null, thumbnailName) | |
303 | }) | |
304 | } | |
305 | ||
aaf61f38 C |
306 | function getDurationFromFile (videoPath, callback) { |
307 | ffmpeg.ffprobe(videoPath, function (err, metadata) { | |
308 | if (err) return callback(err) | |
309 | ||
310 | return callback(null, Math.floor(metadata.format.duration)) | |
311 | }) | |
312 | } | |
313 | ||
b769007f C |
314 | function list (callback) { |
315 | return this.find().asCallback() | |
316 | } | |
317 | ||
0ff21c1c | 318 | function listForApi (start, count, sort, callback) { |
feb4bdfd C |
319 | const query = { |
320 | offset: start, | |
321 | limit: count, | |
7920c273 | 322 | distinct: true, // For the count, a video can have many tags |
feb4bdfd C |
323 | order: [ modelUtils.getSort(sort) ], |
324 | include: [ | |
325 | { | |
326 | model: this.sequelize.models.Author, | |
7920c273 C |
327 | include: [ { model: this.sequelize.models.Pod, required: false } ] |
328 | }, | |
329 | ||
330 | this.sequelize.models.Tag | |
feb4bdfd C |
331 | ] |
332 | } | |
333 | ||
334 | return this.findAndCountAll(query).asCallback(function (err, result) { | |
335 | if (err) return callback(err) | |
336 | ||
337 | return callback(null, result.rows, result.count) | |
338 | }) | |
aaf61f38 C |
339 | } |
340 | ||
49abbbbe | 341 | function listByHostAndRemoteId (fromHost, remoteId, callback) { |
feb4bdfd C |
342 | const query = { |
343 | where: { | |
344 | remoteId: remoteId | |
345 | }, | |
346 | include: [ | |
347 | { | |
348 | model: this.sequelize.models.Author, | |
349 | include: [ | |
350 | { | |
351 | model: this.sequelize.models.Pod, | |
7920c273 | 352 | required: true, |
feb4bdfd C |
353 | where: { |
354 | host: fromHost | |
355 | } | |
356 | } | |
357 | ] | |
358 | } | |
359 | ] | |
360 | } | |
aaf61f38 | 361 | |
feb4bdfd | 362 | return this.findAll(query).asCallback(callback) |
aaf61f38 C |
363 | } |
364 | ||
7920c273 | 365 | function listOwnedAndPopulateAuthorAndTags (callback) { |
558d7c23 | 366 | // If remoteId is null this is *our* video |
feb4bdfd C |
367 | const query = { |
368 | where: { | |
369 | remoteId: null | |
370 | }, | |
7920c273 | 371 | include: [ this.sequelize.models.Author, this.sequelize.models.Tag ] |
feb4bdfd C |
372 | } |
373 | ||
374 | return this.findAll(query).asCallback(callback) | |
aaf61f38 C |
375 | } |
376 | ||
9bd26629 | 377 | function listOwnedByAuthor (author, callback) { |
feb4bdfd C |
378 | const query = { |
379 | where: { | |
380 | remoteId: null | |
381 | }, | |
382 | include: [ | |
383 | { | |
384 | model: this.sequelize.models.Author, | |
385 | where: { | |
386 | name: author | |
387 | } | |
388 | } | |
389 | ] | |
390 | } | |
9bd26629 | 391 | |
feb4bdfd | 392 | return this.findAll(query).asCallback(callback) |
aaf61f38 C |
393 | } |
394 | ||
395 | function load (id, callback) { | |
feb4bdfd C |
396 | return this.findById(id).asCallback(callback) |
397 | } | |
398 | ||
399 | function loadAndPopulateAuthor (id, callback) { | |
400 | const options = { | |
401 | include: [ this.sequelize.models.Author ] | |
402 | } | |
403 | ||
404 | return this.findById(id, options).asCallback(callback) | |
405 | } | |
406 | ||
7920c273 | 407 | function loadAndPopulateAuthorAndPodAndTags (id, callback) { |
feb4bdfd C |
408 | const options = { |
409 | include: [ | |
410 | { | |
411 | model: this.sequelize.models.Author, | |
7920c273 C |
412 | include: [ { model: this.sequelize.models.Pod, required: false } ] |
413 | }, | |
414 | this.sequelize.models.Tag | |
feb4bdfd C |
415 | ] |
416 | } | |
417 | ||
418 | return this.findById(id, options).asCallback(callback) | |
aaf61f38 C |
419 | } |
420 | ||
7920c273 | 421 | function searchAndPopulateAuthorAndPodAndTags (value, field, start, count, sort, callback) { |
feb4bdfd | 422 | const podInclude = { |
7920c273 C |
423 | model: this.sequelize.models.Pod, |
424 | required: false | |
feb4bdfd | 425 | } |
7920c273 | 426 | |
feb4bdfd C |
427 | const authorInclude = { |
428 | model: this.sequelize.models.Author, | |
429 | include: [ | |
430 | podInclude | |
431 | ] | |
432 | } | |
433 | ||
7920c273 C |
434 | const tagInclude = { |
435 | model: this.sequelize.models.Tag | |
436 | } | |
437 | ||
feb4bdfd C |
438 | const query = { |
439 | where: {}, | |
feb4bdfd C |
440 | offset: start, |
441 | limit: count, | |
7920c273 | 442 | distinct: true, // For the count, a video can have many tags |
feb4bdfd C |
443 | order: [ modelUtils.getSort(sort) ] |
444 | } | |
445 | ||
aaf61f38 | 446 | // Make an exact search with the magnet |
55723d16 C |
447 | if (field === 'magnetUri') { |
448 | const infoHash = magnetUtil.decode(value).infoHash | |
feb4bdfd | 449 | query.where.infoHash = infoHash |
55723d16 | 450 | } else if (field === 'tags') { |
7920c273 C |
451 | const escapedValue = this.sequelize.escape('%' + value + '%') |
452 | query.where = { | |
453 | id: { | |
454 | $in: this.sequelize.literal( | |
455 | '(SELECT "VideoTags"."videoId" FROM "Tags" INNER JOIN "VideoTags" ON "Tags"."id" = "VideoTags"."tagId" WHERE name LIKE ' + escapedValue + ')' | |
456 | ) | |
feb4bdfd C |
457 | } |
458 | } | |
7920c273 C |
459 | } else if (field === 'host') { |
460 | // FIXME: Include our pod? (not stored in the database) | |
461 | podInclude.where = { | |
462 | host: { | |
463 | $like: '%' + value + '%' | |
feb4bdfd | 464 | } |
feb4bdfd | 465 | } |
7920c273 | 466 | podInclude.required = true |
feb4bdfd | 467 | } else if (field === 'author') { |
7920c273 C |
468 | authorInclude.where = { |
469 | name: { | |
feb4bdfd C |
470 | $like: '%' + value + '%' |
471 | } | |
472 | } | |
7920c273 C |
473 | |
474 | // authorInclude.or = true | |
aaf61f38 | 475 | } else { |
feb4bdfd C |
476 | query.where[field] = { |
477 | $like: '%' + value + '%' | |
478 | } | |
aaf61f38 C |
479 | } |
480 | ||
7920c273 C |
481 | query.include = [ |
482 | authorInclude, tagInclude | |
483 | ] | |
484 | ||
485 | if (tagInclude.where) { | |
486 | // query.include.push([ this.sequelize.models.Tag ]) | |
487 | } | |
488 | ||
feb4bdfd C |
489 | return this.findAndCountAll(query).asCallback(function (err, result) { |
490 | if (err) return callback(err) | |
491 | ||
492 | return callback(null, result.rows, result.count) | |
493 | }) | |
aaf61f38 C |
494 | } |
495 | ||
aaf61f38 C |
496 | // --------------------------------------------------------------------------- |
497 | ||
aaf61f38 | 498 | function removeThumbnail (video, callback) { |
f285faa0 | 499 | fs.unlink(constants.CONFIG.STORAGE.THUMBNAILS_DIR + video.getThumbnailName(), callback) |
aaf61f38 C |
500 | } |
501 | ||
502 | function removeFile (video, callback) { | |
f285faa0 | 503 | fs.unlink(constants.CONFIG.STORAGE.VIDEOS_DIR + video.getVideoFilename(), callback) |
aaf61f38 C |
504 | } |
505 | ||
aaf61f38 | 506 | function removeTorrent (video, callback) { |
558d7c23 | 507 | fs.unlink(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), callback) |
aaf61f38 C |
508 | } |
509 | ||
6a94a109 C |
510 | function removePreview (video, callback) { |
511 | // Same name than video thumnail | |
f285faa0 | 512 | fs.unlink(constants.CONFIG.STORAGE.PREVIEWS_DIR + video.getPreviewName(), callback) |
6a94a109 C |
513 | } |
514 | ||
558d7c23 | 515 | function createPreview (video, videoPath, callback) { |
f285faa0 | 516 | generateImage(video, videoPath, constants.CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName(), callback) |
6a94a109 C |
517 | } |
518 | ||
558d7c23 | 519 | function createThumbnail (video, videoPath, callback) { |
f285faa0 | 520 | generateImage(video, videoPath, constants.CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName(), constants.THUMBNAILS_SIZE, callback) |
aaf61f38 C |
521 | } |
522 | ||
f285faa0 | 523 | function generateImage (video, videoPath, folder, imageName, size, callback) { |
558d7c23 | 524 | const options = { |
f285faa0 | 525 | filename: imageName, |
558d7c23 C |
526 | count: 1, |
527 | folder | |
528 | } | |
aaf61f38 | 529 | |
558d7c23 C |
530 | if (!callback) { |
531 | callback = size | |
532 | } else { | |
533 | options.size = size | |
534 | } | |
535 | ||
536 | ffmpeg(videoPath) | |
537 | .on('error', callback) | |
538 | .on('end', function () { | |
f285faa0 | 539 | callback(null, imageName) |
aaf61f38 | 540 | }) |
558d7c23 | 541 | .thumbnail(options) |
aaf61f38 | 542 | } |