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')
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
20 // ---------------------------------------------------------------------------
22 module
.exports = function (sequelize
, DataTypes
) {
23 const Video
= sequelize
.define('Video',
27 defaultValue: DataTypes
.UUIDV4
,
34 type: DataTypes
.STRING
,
37 nameValid: function (value
) {
38 const res
= customVideosValidators
.isVideoNameValid(value
)
39 if (res
=== false) throw new Error('Video name is not valid.')
44 type: DataTypes
.ENUM(values(constants
.CONSTRAINTS_FIELDS
.VIDEOS
.EXTNAME
)),
55 type: DataTypes
.INTEGER
,
58 categoryValid: function (value
) {
59 const res
= customVideosValidators
.isVideoCategoryValid(value
)
60 if (res
=== false) throw new Error('Video category is not valid.')
65 type: DataTypes
.STRING
,
68 descriptionValid: function (value
) {
69 const res
= customVideosValidators
.isVideoDescriptionValid(value
)
70 if (res
=== false) throw new Error('Video description is not valid.')
75 type: DataTypes
.STRING
,
78 infoHashValid: function (value
) {
79 const res
= customVideosValidators
.isVideoInfoHashValid(value
)
80 if (res
=== false) throw new Error('Video info hash is not valid.')
85 type: DataTypes
.INTEGER
,
88 durationValid: function (value
) {
89 const res
= customVideosValidators
.isVideoDurationValid(value
)
90 if (res
=== false) throw new Error('Video duration is not valid.')
95 type: DataTypes
.INTEGER
,
104 type: DataTypes
.INTEGER
,
113 type: DataTypes
.INTEGER
,
125 fields: [ 'authorId' ]
128 fields: [ 'remoteId' ]
134 fields: [ 'createdAt' ]
137 fields: [ 'duration' ]
140 fields: [ 'infoHash' ]
152 generateThumbnailFromData
,
156 listOwnedAndPopulateAuthorAndTags
,
159 loadByHostAndRemoteId
,
160 loadAndPopulateAuthor
,
161 loadAndPopulateAuthorAndPodAndTags
,
162 searchAndPopulateAuthorAndPodAndTags
186 function beforeValidate (video
, options
, next
) {
187 // Put a fake infoHash if it does not exists yet
188 if (video
.isOwned() && !video
.infoHash
) {
190 video
.infoHash
= '0123456789abcdef0123456789abcdef01234567'
196 function beforeCreate (video
, options
, next
) {
199 if (video
.isOwned()) {
200 const videoPath
= pathUtils
.join(constants
.CONFIG
.STORAGE
.VIDEOS_DIR
, video
.getVideoFilename())
203 function createVideoTorrent (callback
) {
206 [ constants
.CONFIG
.WEBSERVER
.WS
+ '://' + constants
.CONFIG
.WEBSERVER
.HOSTNAME
+ ':' + constants
.CONFIG
.WEBSERVER
.PORT
+ '/tracker/socket' ]
209 constants
.CONFIG
.WEBSERVER
.URL
+ constants
.STATIC_PATHS
.WEBSEED
+ video
.getVideoFilename()
213 createTorrent(videoPath
, options
, function (err
, torrent
) {
214 if (err
) return callback(err
)
216 const filePath
= pathUtils
.join(constants
.CONFIG
.STORAGE
.TORRENTS_DIR
, video
.getTorrentName())
217 fs
.writeFile(filePath
, torrent
, function (err
) {
218 if (err
) return callback(err
)
220 const parsedTorrent
= parseTorrent(torrent
)
221 video
.set('infoHash', parsedTorrent
.infoHash
)
222 video
.validate().asCallback(callback
)
227 function createVideoThumbnail (callback
) {
228 createThumbnail(video
, videoPath
, callback
)
231 function createVIdeoPreview (callback
) {
232 createPreview(video
, videoPath
, callback
)
236 return parallel(tasks
, next
)
242 function afterDestroy (video
, options
, next
) {
246 function (callback
) {
247 removeThumbnail(video
, callback
)
251 if (video
.isOwned()) {
253 function removeVideoFile (callback
) {
254 removeFile(video
, callback
)
257 function removeVideoTorrent (callback
) {
258 removeTorrent(video
, callback
)
261 function removeVideoPreview (callback
) {
262 removePreview(video
, callback
)
265 function removeVideoToFriends (callback
) {
270 friends
.removeVideoToFriends(params
)
277 parallel(tasks
, next
)
280 // ------------------------------ METHODS ------------------------------
282 function associate (models
) {
283 this.belongsTo(models
.Author
, {
291 this.belongsToMany(models
.Tag
, {
292 foreignKey: 'videoId',
293 through: models
.VideoTag
,
297 this.hasMany(models
.VideoAbuse
, {
306 function generateMagnetUri () {
307 let baseUrlHttp
, baseUrlWs
309 if (this.isOwned()) {
310 baseUrlHttp
= constants
.CONFIG
.WEBSERVER
.URL
311 baseUrlWs
= constants
.CONFIG
.WEBSERVER
.WS
+ '://' + constants
.CONFIG
.WEBSERVER
.HOSTNAME
+ ':' + constants
.CONFIG
.WEBSERVER
.PORT
313 baseUrlHttp
= constants
.REMOTE_SCHEME
.HTTP
+ '://' + this.Author
.Pod
.host
314 baseUrlWs
= constants
.REMOTE_SCHEME
.WS
+ '://' + this.Author
.Pod
.host
317 const xs
= baseUrlHttp
+ constants
.STATIC_PATHS
.TORRENTS
+ this.getTorrentName()
318 const announce
= baseUrlWs
+ '/tracker/socket'
319 const urlList
= [ baseUrlHttp
+ constants
.STATIC_PATHS
.WEBSEED
+ this.getVideoFilename() ]
325 infoHash: this.infoHash
,
329 return magnetUtil
.encode(magnetHash
)
332 function getVideoFilename () {
333 if (this.isOwned()) return this.id
+ this.extname
335 return this.remoteId
+ this.extname
338 function getThumbnailName () {
339 // We always have a copy of the thumbnail
340 return this.id
+ '.jpg'
343 function getPreviewName () {
344 const extension
= '.jpg'
346 if (this.isOwned()) return this.id
+ extension
348 return this.remoteId
+ extension
351 function getTorrentName () {
352 const extension
= '.torrent'
354 if (this.isOwned()) return this.id
+ extension
356 return this.remoteId
+ extension
359 function isOwned () {
360 return this.remoteId
=== null
363 function toFormatedJSON () {
366 if (this.Author
.Pod
) {
367 podHost
= this.Author
.Pod
.host
369 // It means it's our video
370 podHost
= constants
.CONFIG
.WEBSERVER
.HOST
373 // Maybe our pod is not up to date and there are new categories since our version
374 let categoryLabel
= constants
.VIDEO_CATEGORIES
[this.category
]
375 if (!categoryLabel
) categoryLabel
= 'Misc'
380 category: this.category
,
382 description: this.description
,
384 isLocal: this.isOwned(),
385 magnetUri: this.generateMagnetUri(),
386 author: this.Author
.name
,
387 duration: this.duration
,
390 dislikes: this.dislikes
,
391 tags: map(this.Tags
, 'name'),
392 thumbnailPath: pathUtils
.join(constants
.STATIC_PATHS
.THUMBNAILS
, this.getThumbnailName()),
393 createdAt: this.createdAt
,
394 updatedAt: this.updatedAt
400 function toAddRemoteJSON (callback
) {
403 // Get thumbnail data to send to the other pod
404 const thumbnailPath
= pathUtils
.join(constants
.CONFIG
.STORAGE
.THUMBNAILS_DIR
, this.getThumbnailName())
405 fs
.readFile(thumbnailPath
, function (err
, thumbnailData
) {
407 logger
.error('Cannot read the thumbnail of the video')
411 const remoteVideo
= {
413 category: self
.category
,
414 description: self
.description
,
415 infoHash: self
.infoHash
,
417 author: self
.Author
.name
,
418 duration: self
.duration
,
419 thumbnailData: thumbnailData
.toString('binary'),
420 tags: map(self
.Tags
, 'name'),
421 createdAt: self
.createdAt
,
422 updatedAt: self
.updatedAt
,
423 extname: self
.extname
,
426 dislikes: self
.dislikes
429 return callback(null, remoteVideo
)
433 function toUpdateRemoteJSON (callback
) {
436 category: this.category
,
437 description: this.description
,
438 infoHash: this.infoHash
,
440 author: this.Author
.name
,
441 duration: this.duration
,
442 tags: map(this.Tags
, 'name'),
443 createdAt: this.createdAt
,
444 updatedAt: this.updatedAt
,
445 extname: this.extname
,
448 dislikes: this.dislikes
454 // ------------------------------ STATICS ------------------------------
456 function generateThumbnailFromData (video
, thumbnailData
, callback
) {
457 // Creating the thumbnail for a remote video
459 const thumbnailName
= video
.getThumbnailName()
460 const thumbnailPath
= pathUtils
.join(constants
.CONFIG
.STORAGE
.THUMBNAILS_DIR
, thumbnailName
)
461 fs
.writeFile(thumbnailPath
, Buffer
.from(thumbnailData
, 'binary'), function (err
) {
462 if (err
) return callback(err
)
464 return callback(null, thumbnailName
)
468 function getDurationFromFile (videoPath
, callback
) {
469 ffmpeg
.ffprobe(videoPath
, function (err
, metadata
) {
470 if (err
) return callback(err
)
472 return callback(null, Math
.floor(metadata
.format
.duration
))
476 function list (callback
) {
477 return this.findAll().asCallback(callback
)
480 function listForApi (start
, count
, sort
, callback
) {
484 distinct: true, // For the count, a video can have many tags
485 order: [ modelUtils
.getSort(sort
), [ this.sequelize
.models
.Tag
, 'name', 'ASC' ] ],
488 model: this.sequelize
.models
.Author
,
489 include: [ { model: this.sequelize
.models
.Pod
, required: false } ]
492 this.sequelize
.models
.Tag
496 return this.findAndCountAll(query
).asCallback(function (err
, result
) {
497 if (err
) return callback(err
)
499 return callback(null, result
.rows
, result
.count
)
503 function loadByHostAndRemoteId (fromHost
, remoteId
, callback
) {
510 model: this.sequelize
.models
.Author
,
513 model: this.sequelize
.models
.Pod
,
524 return this.findOne(query
).asCallback(callback
)
527 function listOwnedAndPopulateAuthorAndTags (callback
) {
528 // If remoteId is null this is *our* video
533 include: [ this.sequelize
.models
.Author
, this.sequelize
.models
.Tag
]
536 return this.findAll(query
).asCallback(callback
)
539 function listOwnedByAuthor (author
, callback
) {
546 model: this.sequelize
.models
.Author
,
554 return this.findAll(query
).asCallback(callback
)
557 function load (id
, callback
) {
558 return this.findById(id
).asCallback(callback
)
561 function loadAndPopulateAuthor (id
, callback
) {
563 include: [ this.sequelize
.models
.Author
]
566 return this.findById(id
, options
).asCallback(callback
)
569 function loadAndPopulateAuthorAndPodAndTags (id
, callback
) {
573 model: this.sequelize
.models
.Author
,
574 include: [ { model: this.sequelize
.models
.Pod
, required: false } ]
576 this.sequelize
.models
.Tag
580 return this.findById(id
, options
).asCallback(callback
)
583 function searchAndPopulateAuthorAndPodAndTags (value
, field
, start
, count
, sort
, callback
) {
585 model: this.sequelize
.models
.Pod
,
589 const authorInclude
= {
590 model: this.sequelize
.models
.Author
,
597 model: this.sequelize
.models
.Tag
604 distinct: true, // For the count, a video can have many tags
605 order: [ modelUtils
.getSort(sort
), [ this.sequelize
.models
.Tag
, 'name', 'ASC' ] ]
608 // Make an exact search with the magnet
609 if (field
=== 'magnetUri') {
610 const infoHash
= magnetUtil
.decode(value
).infoHash
611 query
.where
.infoHash
= infoHash
612 } else if (field
=== 'tags') {
613 const escapedValue
= this.sequelize
.escape('%' + value
+ '%')
616 $in: this.sequelize
.literal(
617 '(SELECT "VideoTags"."videoId" FROM "Tags" INNER JOIN "VideoTags" ON "Tags"."id" = "VideoTags"."tagId" WHERE name LIKE ' + escapedValue
+ ')'
621 } else if (field
=== 'host') {
622 // FIXME: Include our pod? (not stored in the database)
625 $like: '%' + value
+ '%'
628 podInclude
.required
= true
629 } else if (field
=== 'author') {
630 authorInclude
.where
= {
632 $like: '%' + value
+ '%'
636 // authorInclude.or = true
638 query
.where
[field
] = {
639 $like: '%' + value
+ '%'
644 authorInclude
, tagInclude
647 if (tagInclude
.where
) {
648 // query.include.push([ this.sequelize.models.Tag ])
651 return this.findAndCountAll(query
).asCallback(function (err
, result
) {
652 if (err
) return callback(err
)
654 return callback(null, result
.rows
, result
.count
)
658 // ---------------------------------------------------------------------------
660 function removeThumbnail (video
, callback
) {
661 const thumbnailPath
= pathUtils
.join(constants
.CONFIG
.STORAGE
.THUMBNAILS_DIR
, video
.getThumbnailName())
662 fs
.unlink(thumbnailPath
, callback
)
665 function removeFile (video
, callback
) {
666 const filePath
= pathUtils
.join(constants
.CONFIG
.STORAGE
.VIDEOS_DIR
, video
.getVideoFilename())
667 fs
.unlink(filePath
, callback
)
670 function removeTorrent (video
, callback
) {
671 const torrenPath
= pathUtils
.join(constants
.CONFIG
.STORAGE
.TORRENTS_DIR
, video
.getTorrentName())
672 fs
.unlink(torrenPath
, callback
)
675 function removePreview (video
, callback
) {
676 // Same name than video thumnail
677 fs
.unlink(constants
.CONFIG
.STORAGE
.PREVIEWS_DIR
+ video
.getPreviewName(), callback
)
680 function createPreview (video
, videoPath
, callback
) {
681 generateImage(video
, videoPath
, constants
.CONFIG
.STORAGE
.PREVIEWS_DIR
, video
.getPreviewName(), callback
)
684 function createThumbnail (video
, videoPath
, callback
) {
685 generateImage(video
, videoPath
, constants
.CONFIG
.STORAGE
.THUMBNAILS_DIR
, video
.getThumbnailName(), constants
.THUMBNAILS_SIZE
, callback
)
688 function generateImage (video
, videoPath
, folder
, imageName
, size
, callback
) {
702 .on('error', callback
)
703 .on('end', function () {
704 callback(null, imageName
)