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 // TODO: add indexes on searchable columns
24 const Video
= sequelize
.define('Video',
28 defaultValue: DataTypes
.UUIDV4
,
35 type: DataTypes
.STRING
,
38 nameValid: function (value
) {
39 const res
= customVideosValidators
.isVideoNameValid(value
)
40 if (res
=== false) throw new Error('Video name is not valid.')
45 type: DataTypes
.ENUM(values(constants
.CONSTRAINTS_FIELDS
.VIDEOS
.EXTNAME
)),
56 type: DataTypes
.STRING
,
59 descriptionValid: function (value
) {
60 const res
= customVideosValidators
.isVideoDescriptionValid(value
)
61 if (res
=== false) throw new Error('Video description is not valid.')
66 type: DataTypes
.STRING
,
69 infoHashValid: function (value
) {
70 const res
= customVideosValidators
.isVideoInfoHashValid(value
)
71 if (res
=== false) throw new Error('Video info hash is not valid.')
76 type: DataTypes
.INTEGER
,
79 durationValid: function (value
) {
80 const res
= customVideosValidators
.isVideoDurationValid(value
)
81 if (res
=== false) throw new Error('Video duration is not valid.')
89 fields: [ 'authorId' ]
92 fields: [ 'remoteId' ]
98 fields: [ 'createdAt' ]
101 fields: [ 'duration' ]
104 fields: [ 'infoHash' ]
110 generateThumbnailFromData
,
114 listByHostAndRemoteId
,
115 listOwnedAndPopulateAuthorAndTags
,
118 loadAndPopulateAuthor
,
119 loadAndPopulateAuthorAndPodAndTags
,
120 searchAndPopulateAuthorAndPodAndTags
143 function beforeValidate (video
, options
, next
) {
144 if (video
.isOwned()) {
146 video
.infoHash
= '0123456789abcdef0123456789abcdef01234567'
152 function beforeCreate (video
, options
, next
) {
155 if (video
.isOwned()) {
156 const videoPath
= pathUtils
.join(constants
.CONFIG
.STORAGE
.VIDEOS_DIR
, video
.getVideoFilename())
159 // TODO: refractoring
160 function (callback
) {
163 [ constants
.CONFIG
.WEBSERVER
.WS
+ '://' + constants
.CONFIG
.WEBSERVER
.HOSTNAME
+ ':' + constants
.CONFIG
.WEBSERVER
.PORT
+ '/tracker/socket' ]
166 constants
.CONFIG
.WEBSERVER
.URL
+ constants
.STATIC_PATHS
.WEBSEED
+ video
.getVideoFilename()
170 createTorrent(videoPath
, options
, function (err
, torrent
) {
171 if (err
) return callback(err
)
173 fs
.writeFile(constants
.CONFIG
.STORAGE
.TORRENTS_DIR
+ video
.getTorrentName(), torrent
, function (err
) {
174 if (err
) return callback(err
)
176 const parsedTorrent
= parseTorrent(torrent
)
177 video
.set('infoHash', parsedTorrent
.infoHash
)
178 video
.validate().asCallback(callback
)
182 function (callback
) {
183 createThumbnail(video
, videoPath
, callback
)
185 function (callback
) {
186 createPreview(video
, videoPath
, callback
)
190 return parallel(tasks
, next
)
196 function afterDestroy (video
, options
, next
) {
200 function (callback
) {
201 removeThumbnail(video
, callback
)
205 if (video
.isOwned()) {
207 function (callback
) {
208 removeFile(video
, callback
)
211 function (callback
) {
212 removeTorrent(video
, callback
)
215 function (callback
) {
216 removePreview(video
, callback
)
219 function (callback
) {
225 friends
.removeVideoToFriends(params
)
232 parallel(tasks
, next
)
235 // ------------------------------ METHODS ------------------------------
237 function associate (models
) {
238 this.belongsTo(models
.Author
, {
246 this.belongsToMany(models
.Tag
, {
247 foreignKey: 'videoId',
248 through: models
.VideoTag
,
253 function generateMagnetUri () {
254 let baseUrlHttp
, baseUrlWs
256 if (this.isOwned()) {
257 baseUrlHttp
= constants
.CONFIG
.WEBSERVER
.URL
258 baseUrlWs
= constants
.CONFIG
.WEBSERVER
.WS
+ '://' + constants
.CONFIG
.WEBSERVER
.HOSTNAME
+ ':' + constants
.CONFIG
.WEBSERVER
.PORT
260 baseUrlHttp
= constants
.REMOTE_SCHEME
.HTTP
+ '://' + this.Author
.Pod
.host
261 baseUrlWs
= constants
.REMOTE_SCHEME
.WS
+ '://' + this.Author
.Pod
.host
264 const xs
= baseUrlHttp
+ constants
.STATIC_PATHS
.TORRENTS
+ this.getTorrentName()
265 const announce
= baseUrlWs
+ '/tracker/socket'
266 const urlList
= [ baseUrlHttp
+ constants
.STATIC_PATHS
.WEBSEED
+ this.getVideoFilename() ]
272 infoHash: this.infoHash
,
276 return magnetUtil
.encode(magnetHash
)
279 function getVideoFilename () {
280 if (this.isOwned()) return this.id
+ this.extname
282 return this.remoteId
+ this.extname
285 function getThumbnailName () {
286 // We always have a copy of the thumbnail
287 return this.id
+ '.jpg'
290 function getPreviewName () {
291 const extension
= '.jpg'
293 if (this.isOwned()) return this.id
+ extension
295 return this.remoteId
+ extension
298 function getTorrentName () {
299 const extension
= '.torrent'
301 if (this.isOwned()) return this.id
+ extension
303 return this.remoteId
+ extension
306 function isOwned () {
307 return this.remoteId
=== null
310 function toFormatedJSON () {
313 if (this.Author
.Pod
) {
314 podHost
= this.Author
.Pod
.host
316 // It means it's our video
317 podHost
= constants
.CONFIG
.WEBSERVER
.HOST
323 description: this.description
,
325 isLocal: this.isOwned(),
326 magnetUri: this.generateMagnetUri(),
327 author: this.Author
.name
,
328 duration: this.duration
,
329 tags: map(this.Tags
, 'name'),
330 thumbnailPath: constants
.STATIC_PATHS
.THUMBNAILS
+ '/' + this.getThumbnailName(),
331 createdAt: this.createdAt
337 function toRemoteJSON (callback
) {
340 // Get thumbnail data to send to the other pod
341 const thumbnailPath
= pathUtils
.join(constants
.CONFIG
.STORAGE
.THUMBNAILS_DIR
, this.getThumbnailName())
342 fs
.readFile(thumbnailPath
, function (err
, thumbnailData
) {
344 logger
.error('Cannot read the thumbnail of the video')
348 const remoteVideo
= {
350 description: self
.description
,
351 infoHash: self
.infoHash
,
353 author: self
.Author
.name
,
354 duration: self
.duration
,
355 thumbnailData: thumbnailData
.toString('binary'),
356 tags: map(self
.Tags
, 'name'),
357 createdAt: self
.createdAt
,
358 extname: self
.extname
361 return callback(null, remoteVideo
)
365 // ------------------------------ STATICS ------------------------------
367 function generateThumbnailFromData (video
, thumbnailData
, callback
) {
368 // Creating the thumbnail for a remote video
370 const thumbnailName
= video
.getThumbnailName()
371 const thumbnailPath
= constants
.CONFIG
.STORAGE
.THUMBNAILS_DIR
+ thumbnailName
372 fs
.writeFile(thumbnailPath
, Buffer
.from(thumbnailData
, 'binary'), function (err
) {
373 if (err
) return callback(err
)
375 return callback(null, thumbnailName
)
379 function getDurationFromFile (videoPath
, callback
) {
380 ffmpeg
.ffprobe(videoPath
, function (err
, metadata
) {
381 if (err
) return callback(err
)
383 return callback(null, Math
.floor(metadata
.format
.duration
))
387 function list (callback
) {
388 return this.find().asCallback()
391 function listForApi (start
, count
, sort
, callback
) {
395 distinct: true, // For the count, a video can have many tags
396 order: [ modelUtils
.getSort(sort
), [ this.sequelize
.models
.Tag
, 'name', 'ASC' ] ],
399 model: this.sequelize
.models
.Author
,
400 include: [ { model: this.sequelize
.models
.Pod
, required: false } ]
403 this.sequelize
.models
.Tag
407 return this.findAndCountAll(query
).asCallback(function (err
, result
) {
408 if (err
) return callback(err
)
410 return callback(null, result
.rows
, result
.count
)
414 function listByHostAndRemoteId (fromHost
, remoteId
, callback
) {
421 model: this.sequelize
.models
.Author
,
424 model: this.sequelize
.models
.Pod
,
435 return this.findAll(query
).asCallback(callback
)
438 function listOwnedAndPopulateAuthorAndTags (callback
) {
439 // If remoteId is null this is *our* video
444 include: [ this.sequelize
.models
.Author
, this.sequelize
.models
.Tag
]
447 return this.findAll(query
).asCallback(callback
)
450 function listOwnedByAuthor (author
, callback
) {
457 model: this.sequelize
.models
.Author
,
465 return this.findAll(query
).asCallback(callback
)
468 function load (id
, callback
) {
469 return this.findById(id
).asCallback(callback
)
472 function loadAndPopulateAuthor (id
, callback
) {
474 include: [ this.sequelize
.models
.Author
]
477 return this.findById(id
, options
).asCallback(callback
)
480 function loadAndPopulateAuthorAndPodAndTags (id
, callback
) {
484 model: this.sequelize
.models
.Author
,
485 include: [ { model: this.sequelize
.models
.Pod
, required: false } ]
487 this.sequelize
.models
.Tag
491 return this.findById(id
, options
).asCallback(callback
)
494 function searchAndPopulateAuthorAndPodAndTags (value
, field
, start
, count
, sort
, callback
) {
496 model: this.sequelize
.models
.Pod
,
500 const authorInclude
= {
501 model: this.sequelize
.models
.Author
,
508 model: this.sequelize
.models
.Tag
515 distinct: true, // For the count, a video can have many tags
516 order: [ modelUtils
.getSort(sort
), [ this.sequelize
.models
.Tag
, 'name', 'ASC' ] ]
519 // Make an exact search with the magnet
520 if (field
=== 'magnetUri') {
521 const infoHash
= magnetUtil
.decode(value
).infoHash
522 query
.where
.infoHash
= infoHash
523 } else if (field
=== 'tags') {
524 const escapedValue
= this.sequelize
.escape('%' + value
+ '%')
527 $in: this.sequelize
.literal(
528 '(SELECT "VideoTags"."videoId" FROM "Tags" INNER JOIN "VideoTags" ON "Tags"."id" = "VideoTags"."tagId" WHERE name LIKE ' + escapedValue
+ ')'
532 } else if (field
=== 'host') {
533 // FIXME: Include our pod? (not stored in the database)
536 $like: '%' + value
+ '%'
539 podInclude
.required
= true
540 } else if (field
=== 'author') {
541 authorInclude
.where
= {
543 $like: '%' + value
+ '%'
547 // authorInclude.or = true
549 query
.where
[field
] = {
550 $like: '%' + value
+ '%'
555 authorInclude
, tagInclude
558 if (tagInclude
.where
) {
559 // query.include.push([ this.sequelize.models.Tag ])
562 return this.findAndCountAll(query
).asCallback(function (err
, result
) {
563 if (err
) return callback(err
)
565 return callback(null, result
.rows
, result
.count
)
569 // ---------------------------------------------------------------------------
571 function removeThumbnail (video
, callback
) {
572 fs
.unlink(constants
.CONFIG
.STORAGE
.THUMBNAILS_DIR
+ video
.getThumbnailName(), callback
)
575 function removeFile (video
, callback
) {
576 fs
.unlink(constants
.CONFIG
.STORAGE
.VIDEOS_DIR
+ video
.getVideoFilename(), callback
)
579 function removeTorrent (video
, callback
) {
580 fs
.unlink(constants
.CONFIG
.STORAGE
.TORRENTS_DIR
+ video
.getTorrentName(), callback
)
583 function removePreview (video
, callback
) {
584 // Same name than video thumnail
585 fs
.unlink(constants
.CONFIG
.STORAGE
.PREVIEWS_DIR
+ video
.getPreviewName(), callback
)
588 function createPreview (video
, videoPath
, callback
) {
589 generateImage(video
, videoPath
, constants
.CONFIG
.STORAGE
.PREVIEWS_DIR
, video
.getPreviewName(), callback
)
592 function createThumbnail (video
, videoPath
, callback
) {
593 generateImage(video
, videoPath
, constants
.CONFIG
.STORAGE
.THUMBNAILS_DIR
, video
.getThumbnailName(), constants
.THUMBNAILS_SIZE
, callback
)
596 function generateImage (video
, videoPath
, folder
, imageName
, size
, callback
) {
610 .on('error', callback
)
611 .on('end', function () {
612 callback(null, imageName
)