]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video.js
Server: add views attribute when sending videos to friends
[github/Chocobozzz/PeerTube.git] / server / models / video.js
1 'use strict'
2
3 const Buffer = require('safe-buffer').Buffer
4 const createTorrent = require('create-torrent')
5 const ffmpeg = require('fluent-ffmpeg')
6 const fs = require('fs')
7 const magnetUtil = require('magnet-uri')
8 const map = require('lodash/map')
9 const parallel = require('async/parallel')
10 const parseTorrent = require('parse-torrent')
11 const pathUtils = require('path')
12 const values = require('lodash/values')
13
14 const constants = require('../initializers/constants')
15 const logger = require('../helpers/logger')
16 const friends = require('../lib/friends')
17 const modelUtils = require('./utils')
18 const customVideosValidators = require('../helpers/custom-validators').videos
19
20 // ---------------------------------------------------------------------------
21
22 module.exports = function (sequelize, DataTypes) {
23 const Video = sequelize.define('Video',
24 {
25 id: {
26 type: DataTypes.UUID,
27 defaultValue: DataTypes.UUIDV4,
28 primaryKey: true,
29 validate: {
30 isUUID: 4
31 }
32 },
33 name: {
34 type: DataTypes.STRING,
35 allowNull: false,
36 validate: {
37 nameValid: function (value) {
38 const res = customVideosValidators.isVideoNameValid(value)
39 if (res === false) throw new Error('Video name is not valid.')
40 }
41 }
42 },
43 extname: {
44 type: DataTypes.ENUM(values(constants.CONSTRAINTS_FIELDS.VIDEOS.EXTNAME)),
45 allowNull: false
46 },
47 remoteId: {
48 type: DataTypes.UUID,
49 allowNull: true,
50 validate: {
51 isUUID: 4
52 }
53 },
54 description: {
55 type: DataTypes.STRING,
56 allowNull: false,
57 validate: {
58 descriptionValid: function (value) {
59 const res = customVideosValidators.isVideoDescriptionValid(value)
60 if (res === false) throw new Error('Video description is not valid.')
61 }
62 }
63 },
64 infoHash: {
65 type: DataTypes.STRING,
66 allowNull: false,
67 validate: {
68 infoHashValid: function (value) {
69 const res = customVideosValidators.isVideoInfoHashValid(value)
70 if (res === false) throw new Error('Video info hash is not valid.')
71 }
72 }
73 },
74 duration: {
75 type: DataTypes.INTEGER,
76 allowNull: false,
77 validate: {
78 durationValid: function (value) {
79 const res = customVideosValidators.isVideoDurationValid(value)
80 if (res === false) throw new Error('Video duration is not valid.')
81 }
82 }
83 },
84 views: {
85 type: DataTypes.INTEGER,
86 allowNull: false,
87 defaultValue: 0,
88 validate: {
89 min: 0,
90 isInt: true
91 }
92 }
93 },
94 {
95 indexes: [
96 {
97 fields: [ 'authorId' ]
98 },
99 {
100 fields: [ 'remoteId' ]
101 },
102 {
103 fields: [ 'name' ]
104 },
105 {
106 fields: [ 'createdAt' ]
107 },
108 {
109 fields: [ 'duration' ]
110 },
111 {
112 fields: [ 'infoHash' ]
113 },
114 {
115 fields: [ 'views' ]
116 }
117 ],
118 classMethods: {
119 associate,
120
121 generateThumbnailFromData,
122 getDurationFromFile,
123 list,
124 listForApi,
125 listOwnedAndPopulateAuthorAndTags,
126 listOwnedByAuthor,
127 load,
128 loadByHostAndRemoteId,
129 loadAndPopulateAuthor,
130 loadAndPopulateAuthorAndPodAndTags,
131 searchAndPopulateAuthorAndPodAndTags
132 },
133 instanceMethods: {
134 generateMagnetUri,
135 getVideoFilename,
136 getThumbnailName,
137 getPreviewName,
138 getTorrentName,
139 isOwned,
140 toFormatedJSON,
141 toAddRemoteJSON,
142 toUpdateRemoteJSON
143 },
144 hooks: {
145 beforeValidate,
146 beforeCreate,
147 afterDestroy
148 }
149 }
150 )
151
152 return Video
153 }
154
155 function beforeValidate (video, options, next) {
156 // Put a fake infoHash if it does not exists yet
157 if (video.isOwned() && !video.infoHash) {
158 // 40 hexa length
159 video.infoHash = '0123456789abcdef0123456789abcdef01234567'
160 }
161
162 return next(null)
163 }
164
165 function beforeCreate (video, options, next) {
166 const tasks = []
167
168 if (video.isOwned()) {
169 const videoPath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename())
170
171 tasks.push(
172 function createVideoTorrent (callback) {
173 const options = {
174 announceList: [
175 [ constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT + '/tracker/socket' ]
176 ],
177 urlList: [
178 constants.CONFIG.WEBSERVER.URL + constants.STATIC_PATHS.WEBSEED + video.getVideoFilename()
179 ]
180 }
181
182 createTorrent(videoPath, options, function (err, torrent) {
183 if (err) return callback(err)
184
185 const filePath = pathUtils.join(constants.CONFIG.STORAGE.TORRENTS_DIR, video.getTorrentName())
186 fs.writeFile(filePath, torrent, function (err) {
187 if (err) return callback(err)
188
189 const parsedTorrent = parseTorrent(torrent)
190 video.set('infoHash', parsedTorrent.infoHash)
191 video.validate().asCallback(callback)
192 })
193 })
194 },
195
196 function createVideoThumbnail (callback) {
197 createThumbnail(video, videoPath, callback)
198 },
199
200 function createVIdeoPreview (callback) {
201 createPreview(video, videoPath, callback)
202 }
203 )
204
205 return parallel(tasks, next)
206 }
207
208 return next()
209 }
210
211 function afterDestroy (video, options, next) {
212 const tasks = []
213
214 tasks.push(
215 function (callback) {
216 removeThumbnail(video, callback)
217 }
218 )
219
220 if (video.isOwned()) {
221 tasks.push(
222 function removeVideoFile (callback) {
223 removeFile(video, callback)
224 },
225
226 function removeVideoTorrent (callback) {
227 removeTorrent(video, callback)
228 },
229
230 function removeVideoPreview (callback) {
231 removePreview(video, callback)
232 },
233
234 function removeVideoToFriends (callback) {
235 const params = {
236 remoteId: video.id
237 }
238
239 friends.removeVideoToFriends(params)
240
241 return callback()
242 }
243 )
244 }
245
246 parallel(tasks, next)
247 }
248
249 // ------------------------------ METHODS ------------------------------
250
251 function associate (models) {
252 this.belongsTo(models.Author, {
253 foreignKey: {
254 name: 'authorId',
255 allowNull: false
256 },
257 onDelete: 'cascade'
258 })
259
260 this.belongsToMany(models.Tag, {
261 foreignKey: 'videoId',
262 through: models.VideoTag,
263 onDelete: 'cascade'
264 })
265
266 this.hasMany(models.VideoAbuse, {
267 foreignKey: {
268 name: 'videoId',
269 allowNull: false
270 },
271 onDelete: 'cascade'
272 })
273 }
274
275 function generateMagnetUri () {
276 let baseUrlHttp, baseUrlWs
277
278 if (this.isOwned()) {
279 baseUrlHttp = constants.CONFIG.WEBSERVER.URL
280 baseUrlWs = constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT
281 } else {
282 baseUrlHttp = constants.REMOTE_SCHEME.HTTP + '://' + this.Author.Pod.host
283 baseUrlWs = constants.REMOTE_SCHEME.WS + '://' + this.Author.Pod.host
284 }
285
286 const xs = baseUrlHttp + constants.STATIC_PATHS.TORRENTS + this.getTorrentName()
287 const announce = baseUrlWs + '/tracker/socket'
288 const urlList = [ baseUrlHttp + constants.STATIC_PATHS.WEBSEED + this.getVideoFilename() ]
289
290 const magnetHash = {
291 xs,
292 announce,
293 urlList,
294 infoHash: this.infoHash,
295 name: this.name
296 }
297
298 return magnetUtil.encode(magnetHash)
299 }
300
301 function getVideoFilename () {
302 if (this.isOwned()) return this.id + this.extname
303
304 return this.remoteId + this.extname
305 }
306
307 function getThumbnailName () {
308 // We always have a copy of the thumbnail
309 return this.id + '.jpg'
310 }
311
312 function getPreviewName () {
313 const extension = '.jpg'
314
315 if (this.isOwned()) return this.id + extension
316
317 return this.remoteId + extension
318 }
319
320 function getTorrentName () {
321 const extension = '.torrent'
322
323 if (this.isOwned()) return this.id + extension
324
325 return this.remoteId + extension
326 }
327
328 function isOwned () {
329 return this.remoteId === null
330 }
331
332 function toFormatedJSON () {
333 let podHost
334
335 if (this.Author.Pod) {
336 podHost = this.Author.Pod.host
337 } else {
338 // It means it's our video
339 podHost = constants.CONFIG.WEBSERVER.HOST
340 }
341
342 const json = {
343 id: this.id,
344 name: this.name,
345 description: this.description,
346 podHost,
347 isLocal: this.isOwned(),
348 magnetUri: this.generateMagnetUri(),
349 author: this.Author.name,
350 duration: this.duration,
351 views: this.views,
352 tags: map(this.Tags, 'name'),
353 thumbnailPath: pathUtils.join(constants.STATIC_PATHS.THUMBNAILS, this.getThumbnailName()),
354 createdAt: this.createdAt,
355 updatedAt: this.updatedAt
356 }
357
358 return json
359 }
360
361 function toAddRemoteJSON (callback) {
362 const self = this
363
364 // Get thumbnail data to send to the other pod
365 const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
366 fs.readFile(thumbnailPath, function (err, thumbnailData) {
367 if (err) {
368 logger.error('Cannot read the thumbnail of the video')
369 return callback(err)
370 }
371
372 const remoteVideo = {
373 name: self.name,
374 description: self.description,
375 infoHash: self.infoHash,
376 remoteId: self.id,
377 author: self.Author.name,
378 duration: self.duration,
379 thumbnailData: thumbnailData.toString('binary'),
380 tags: map(self.Tags, 'name'),
381 createdAt: self.createdAt,
382 updatedAt: self.updatedAt,
383 extname: self.extname,
384 views: self.views
385 }
386
387 return callback(null, remoteVideo)
388 })
389 }
390
391 function toUpdateRemoteJSON (callback) {
392 const json = {
393 name: this.name,
394 description: this.description,
395 infoHash: this.infoHash,
396 remoteId: this.id,
397 author: this.Author.name,
398 duration: this.duration,
399 tags: map(this.Tags, 'name'),
400 createdAt: this.createdAt,
401 updatedAt: this.updatedAt,
402 extname: this.extname,
403 views: this.views
404 }
405
406 return json
407 }
408
409 // ------------------------------ STATICS ------------------------------
410
411 function generateThumbnailFromData (video, thumbnailData, callback) {
412 // Creating the thumbnail for a remote video
413
414 const thumbnailName = video.getThumbnailName()
415 const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName)
416 fs.writeFile(thumbnailPath, Buffer.from(thumbnailData, 'binary'), function (err) {
417 if (err) return callback(err)
418
419 return callback(null, thumbnailName)
420 })
421 }
422
423 function getDurationFromFile (videoPath, callback) {
424 ffmpeg.ffprobe(videoPath, function (err, metadata) {
425 if (err) return callback(err)
426
427 return callback(null, Math.floor(metadata.format.duration))
428 })
429 }
430
431 function list (callback) {
432 return this.findAll().asCallback(callback)
433 }
434
435 function listForApi (start, count, sort, callback) {
436 const query = {
437 offset: start,
438 limit: count,
439 distinct: true, // For the count, a video can have many tags
440 order: [ modelUtils.getSort(sort), [ this.sequelize.models.Tag, 'name', 'ASC' ] ],
441 include: [
442 {
443 model: this.sequelize.models.Author,
444 include: [ { model: this.sequelize.models.Pod, required: false } ]
445 },
446
447 this.sequelize.models.Tag
448 ]
449 }
450
451 return this.findAndCountAll(query).asCallback(function (err, result) {
452 if (err) return callback(err)
453
454 return callback(null, result.rows, result.count)
455 })
456 }
457
458 function loadByHostAndRemoteId (fromHost, remoteId, callback) {
459 const query = {
460 where: {
461 remoteId: remoteId
462 },
463 include: [
464 {
465 model: this.sequelize.models.Author,
466 include: [
467 {
468 model: this.sequelize.models.Pod,
469 required: true,
470 where: {
471 host: fromHost
472 }
473 }
474 ]
475 }
476 ]
477 }
478
479 return this.findOne(query).asCallback(callback)
480 }
481
482 function listOwnedAndPopulateAuthorAndTags (callback) {
483 // If remoteId is null this is *our* video
484 const query = {
485 where: {
486 remoteId: null
487 },
488 include: [ this.sequelize.models.Author, this.sequelize.models.Tag ]
489 }
490
491 return this.findAll(query).asCallback(callback)
492 }
493
494 function listOwnedByAuthor (author, callback) {
495 const query = {
496 where: {
497 remoteId: null
498 },
499 include: [
500 {
501 model: this.sequelize.models.Author,
502 where: {
503 name: author
504 }
505 }
506 ]
507 }
508
509 return this.findAll(query).asCallback(callback)
510 }
511
512 function load (id, callback) {
513 return this.findById(id).asCallback(callback)
514 }
515
516 function loadAndPopulateAuthor (id, callback) {
517 const options = {
518 include: [ this.sequelize.models.Author ]
519 }
520
521 return this.findById(id, options).asCallback(callback)
522 }
523
524 function loadAndPopulateAuthorAndPodAndTags (id, callback) {
525 const options = {
526 include: [
527 {
528 model: this.sequelize.models.Author,
529 include: [ { model: this.sequelize.models.Pod, required: false } ]
530 },
531 this.sequelize.models.Tag
532 ]
533 }
534
535 return this.findById(id, options).asCallback(callback)
536 }
537
538 function searchAndPopulateAuthorAndPodAndTags (value, field, start, count, sort, callback) {
539 const podInclude = {
540 model: this.sequelize.models.Pod,
541 required: false
542 }
543
544 const authorInclude = {
545 model: this.sequelize.models.Author,
546 include: [
547 podInclude
548 ]
549 }
550
551 const tagInclude = {
552 model: this.sequelize.models.Tag
553 }
554
555 const query = {
556 where: {},
557 offset: start,
558 limit: count,
559 distinct: true, // For the count, a video can have many tags
560 order: [ modelUtils.getSort(sort), [ this.sequelize.models.Tag, 'name', 'ASC' ] ]
561 }
562
563 // Make an exact search with the magnet
564 if (field === 'magnetUri') {
565 const infoHash = magnetUtil.decode(value).infoHash
566 query.where.infoHash = infoHash
567 } else if (field === 'tags') {
568 const escapedValue = this.sequelize.escape('%' + value + '%')
569 query.where = {
570 id: {
571 $in: this.sequelize.literal(
572 '(SELECT "VideoTags"."videoId" FROM "Tags" INNER JOIN "VideoTags" ON "Tags"."id" = "VideoTags"."tagId" WHERE name LIKE ' + escapedValue + ')'
573 )
574 }
575 }
576 } else if (field === 'host') {
577 // FIXME: Include our pod? (not stored in the database)
578 podInclude.where = {
579 host: {
580 $like: '%' + value + '%'
581 }
582 }
583 podInclude.required = true
584 } else if (field === 'author') {
585 authorInclude.where = {
586 name: {
587 $like: '%' + value + '%'
588 }
589 }
590
591 // authorInclude.or = true
592 } else {
593 query.where[field] = {
594 $like: '%' + value + '%'
595 }
596 }
597
598 query.include = [
599 authorInclude, tagInclude
600 ]
601
602 if (tagInclude.where) {
603 // query.include.push([ this.sequelize.models.Tag ])
604 }
605
606 return this.findAndCountAll(query).asCallback(function (err, result) {
607 if (err) return callback(err)
608
609 return callback(null, result.rows, result.count)
610 })
611 }
612
613 // ---------------------------------------------------------------------------
614
615 function removeThumbnail (video, callback) {
616 const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName())
617 fs.unlink(thumbnailPath, callback)
618 }
619
620 function removeFile (video, callback) {
621 const filePath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename())
622 fs.unlink(filePath, callback)
623 }
624
625 function removeTorrent (video, callback) {
626 const torrenPath = pathUtils.join(constants.CONFIG.STORAGE.TORRENTS_DIR, video.getTorrentName())
627 fs.unlink(torrenPath, callback)
628 }
629
630 function removePreview (video, callback) {
631 // Same name than video thumnail
632 fs.unlink(constants.CONFIG.STORAGE.PREVIEWS_DIR + video.getPreviewName(), callback)
633 }
634
635 function createPreview (video, videoPath, callback) {
636 generateImage(video, videoPath, constants.CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName(), callback)
637 }
638
639 function createThumbnail (video, videoPath, callback) {
640 generateImage(video, videoPath, constants.CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName(), constants.THUMBNAILS_SIZE, callback)
641 }
642
643 function generateImage (video, videoPath, folder, imageName, size, callback) {
644 const options = {
645 filename: imageName,
646 count: 1,
647 folder
648 }
649
650 if (!callback) {
651 callback = size
652 } else {
653 options.size = size
654 }
655
656 ffmpeg(videoPath)
657 .on('error', callback)
658 .on('end', function () {
659 callback(null, imageName)
660 })
661 .thumbnail(options)
662 }