1 import * as safeBuffer from 'safe-buffer'
2 const Buffer = safeBuffer.Buffer
3 import * as ffmpeg from 'fluent-ffmpeg'
4 import * as magnetUtil from 'magnet-uri'
5 import { map, values } from 'lodash'
6 import * as parseTorrent from 'parse-torrent'
7 import { join } from 'path'
8 import * as Sequelize from 'sequelize'
9 import * as Promise from 'bluebird'
11 import { database as db } from '../../initializers/database'
12 import { TagInstance } from './tag-interface'
20 isVideoDescriptionValid,
23 readFileBufferPromise,
28 } from '../../helpers'
38 } from '../../initializers'
39 import { JobScheduler, removeVideoToFriends } from '../../lib'
41 import { addMethodsToModel, getSort } from '../utils'
47 } from './video-interface'
49 let Video: Sequelize.Model<VideoInstance, VideoAttributes>
50 let generateMagnetUri: VideoMethods.GenerateMagnetUri
51 let getVideoFilename: VideoMethods.GetVideoFilename
52 let getThumbnailName: VideoMethods.GetThumbnailName
53 let getPreviewName: VideoMethods.GetPreviewName
54 let getTorrentName: VideoMethods.GetTorrentName
55 let isOwned: VideoMethods.IsOwned
56 let toFormatedJSON: VideoMethods.ToFormatedJSON
57 let toAddRemoteJSON: VideoMethods.ToAddRemoteJSON
58 let toUpdateRemoteJSON: VideoMethods.ToUpdateRemoteJSON
59 let transcodeVideofile: VideoMethods.TranscodeVideofile
61 let generateThumbnailFromData: VideoMethods.GenerateThumbnailFromData
62 let getDurationFromFile: VideoMethods.GetDurationFromFile
63 let list: VideoMethods.List
64 let listForApi: VideoMethods.ListForApi
65 let loadByHostAndRemoteId: VideoMethods.LoadByHostAndRemoteId
66 let listOwnedAndPopulateAuthorAndTags: VideoMethods.ListOwnedAndPopulateAuthorAndTags
67 let listOwnedByAuthor: VideoMethods.ListOwnedByAuthor
68 let load: VideoMethods.Load
69 let loadAndPopulateAuthor: VideoMethods.LoadAndPopulateAuthor
70 let loadAndPopulateAuthorAndPodAndTags: VideoMethods.LoadAndPopulateAuthorAndPodAndTags
71 let searchAndPopulateAuthorAndPodAndTags: VideoMethods.SearchAndPopulateAuthorAndPodAndTags
73 export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
74 Video = sequelize.define<VideoInstance, VideoAttributes>('Video',
78 defaultValue: DataTypes.UUIDV4,
85 type: DataTypes.STRING,
88 nameValid: function (value) {
89 const res = isVideoNameValid(value)
90 if (res === false) throw new Error('Video name is not valid.')
95 type: DataTypes.ENUM(values(CONSTRAINTS_FIELDS.VIDEOS.EXTNAME)),
106 type: DataTypes.INTEGER,
109 categoryValid: function (value) {
110 const res = isVideoCategoryValid(value)
111 if (res === false) throw new Error('Video category is not valid.')
116 type: DataTypes.INTEGER,
120 licenceValid: function (value) {
121 const res = isVideoLicenceValid(value)
122 if (res === false) throw new Error('Video licence is not valid.')
127 type: DataTypes.INTEGER,
130 languageValid: function (value) {
131 const res = isVideoLanguageValid(value)
132 if (res === false) throw new Error('Video language is not valid.')
137 type: DataTypes.BOOLEAN,
140 nsfwValid: function (value) {
141 const res = isVideoNSFWValid(value)
142 if (res === false) throw new Error('Video nsfw attribute is not valid.')
147 type: DataTypes.STRING,
150 descriptionValid: function (value) {
151 const res = isVideoDescriptionValid(value)
152 if (res === false) throw new Error('Video description is not valid.')
157 type: DataTypes.STRING,
160 infoHashValid: function (value) {
161 const res = isVideoInfoHashValid(value)
162 if (res === false) throw new Error('Video info hash is not valid.')
167 type: DataTypes.INTEGER,
170 durationValid: function (value) {
171 const res = isVideoDurationValid(value)
172 if (res === false) throw new Error('Video duration is not valid.')
177 type: DataTypes.INTEGER,
186 type: DataTypes.INTEGER,
195 type: DataTypes.INTEGER,
207 fields: [ 'authorId' ]
210 fields: [ 'remoteId' ]
216 fields: [ 'createdAt' ]
219 fields: [ 'duration' ]
222 fields: [ 'infoHash' ]
239 const classMethods = [
242 generateThumbnailFromData,
246 listOwnedAndPopulateAuthorAndTags,
249 loadByHostAndRemoteId,
250 loadAndPopulateAuthor,
251 loadAndPopulateAuthorAndPodAndTags,
252 searchAndPopulateAuthorAndPodAndTags,
255 const instanceMethods = [
267 addMethodsToModel(Video, classMethods, instanceMethods)
272 function beforeValidate (video: VideoInstance) {
273 // Put a fake infoHash if it does not exists yet
274 if (video.isOwned() && !video.infoHash) {
276 video.infoHash = '0123456789abcdef0123456789abcdef01234567'
280 function beforeCreate (video: VideoInstance, options: { transaction: Sequelize.Transaction }) {
281 if (video.isOwned()) {
282 const videoPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename())
286 createTorrentFromVideo(video, videoPath),
287 createThumbnail(video, videoPath),
288 createPreview(video, videoPath)
291 if (CONFIG.TRANSCODING.ENABLED === true) {
297 JobScheduler.Instance.createJob(options.transaction, 'videoTranscoder', dataInput)
301 return Promise.all(tasks)
304 return Promise.resolve()
307 function afterDestroy (video: VideoInstance) {
311 removeThumbnail(video)
314 if (video.isOwned()) {
315 const removeVideoToFriendsParams = {
321 removeTorrent(video),
322 removePreview(video),
323 removeVideoToFriends(removeVideoToFriendsParams)
327 return Promise.all(tasks)
330 // ------------------------------ METHODS ------------------------------
332 function associate (models) {
333 Video.belongsTo(models.Author, {
341 Video.belongsToMany(models.Tag, {
342 foreignKey: 'videoId',
343 through: models.VideoTag,
347 Video.hasMany(models.VideoAbuse, {
356 generateMagnetUri = function (this: VideoInstance) {
360 if (this.isOwned()) {
361 baseUrlHttp = CONFIG.WEBSERVER.URL
362 baseUrlWs = CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
364 baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + this.Author.Pod.host
365 baseUrlWs = REMOTE_SCHEME.WS + '://' + this.Author.Pod.host
368 const xs = baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentName()
369 const announce = [ baseUrlWs + '/tracker/socket' ]
370 const urlList = [ baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename() ]
376 infoHash: this.infoHash,
380 return magnetUtil.encode(magnetHash)
383 getVideoFilename = function (this: VideoInstance) {
384 if (this.isOwned()) return this.id + this.extname
386 return this.remoteId + this.extname
389 getThumbnailName = function (this: VideoInstance) {
390 // We always have a copy of the thumbnail
391 return this.id + '.jpg'
394 getPreviewName = function (this: VideoInstance) {
395 const extension = '.jpg'
397 if (this.isOwned()) return this.id + extension
399 return this.remoteId + extension
402 getTorrentName = function (this: VideoInstance) {
403 const extension = '.torrent'
405 if (this.isOwned()) return this.id + extension
407 return this.remoteId + extension
410 isOwned = function (this: VideoInstance) {
411 return this.remoteId === null
414 toFormatedJSON = function (this: VideoInstance) {
417 if (this.Author.Pod) {
418 podHost = this.Author.Pod.host
420 // It means it's our video
421 podHost = CONFIG.WEBSERVER.HOST
424 // Maybe our pod is not up to date and there are new categories since our version
425 let categoryLabel = VIDEO_CATEGORIES[this.category]
426 if (!categoryLabel) categoryLabel = 'Misc'
428 // Maybe our pod is not up to date and there are new licences since our version
429 let licenceLabel = VIDEO_LICENCES[this.licence]
430 if (!licenceLabel) licenceLabel = 'Unknown'
432 // Language is an optional attribute
433 let languageLabel = VIDEO_LANGUAGES[this.language]
434 if (!languageLabel) languageLabel = 'Unknown'
439 category: this.category,
441 licence: this.licence,
443 language: this.language,
446 description: this.description,
448 isLocal: this.isOwned(),
449 magnetUri: this.generateMagnetUri(),
450 author: this.Author.name,
451 duration: this.duration,
454 dislikes: this.dislikes,
455 tags: map<TagInstance, string>(this.Tags, 'name'),
456 thumbnailPath: join(STATIC_PATHS.THUMBNAILS, this.getThumbnailName()),
457 createdAt: this.createdAt,
458 updatedAt: this.updatedAt
464 toAddRemoteJSON = function (this: VideoInstance) {
465 // Get thumbnail data to send to the other pod
466 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
468 return readFileBufferPromise(thumbnailPath).then(thumbnailData => {
469 const remoteVideo = {
471 category: this.category,
472 licence: this.licence,
473 language: this.language,
475 description: this.description,
476 infoHash: this.infoHash,
478 author: this.Author.name,
479 duration: this.duration,
480 thumbnailData: thumbnailData.toString('binary'),
481 tags: map<TagInstance, string>(this.Tags, 'name'),
482 createdAt: this.createdAt,
483 updatedAt: this.updatedAt,
484 extname: this.extname,
487 dislikes: this.dislikes
494 toUpdateRemoteJSON = function (this: VideoInstance) {
497 category: this.category,
498 licence: this.licence,
499 language: this.language,
501 description: this.description,
502 infoHash: this.infoHash,
504 author: this.Author.name,
505 duration: this.duration,
506 tags: map<TagInstance, string>(this.Tags, 'name'),
507 createdAt: this.createdAt,
508 updatedAt: this.updatedAt,
509 extname: this.extname,
512 dislikes: this.dislikes
518 transcodeVideofile = function (this: VideoInstance) {
521 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
522 const newExtname = '.mp4'
523 const videoInputPath = join(videosDirectory, video.getVideoFilename())
524 const videoOutputPath = join(videosDirectory, video.id + '-transcoded' + newExtname)
526 return new Promise<void>((res, rej) => {
527 ffmpeg(videoInputPath)
528 .output(videoOutputPath)
529 .videoCodec('libx264')
530 .outputOption('-threads ' + CONFIG.TRANSCODING.THREADS)
531 .outputOption('-movflags faststart')
535 return unlinkPromise(videoInputPath)
537 // Important to do this before getVideoFilename() to take in account the new file extension
538 video.set('extname', newExtname)
540 const newVideoPath = join(videosDirectory, video.getVideoFilename())
541 return renamePromise(videoOutputPath, newVideoPath)
544 const newVideoPath = join(videosDirectory, video.getVideoFilename())
545 return createTorrentFromVideo(video, newVideoPath)
554 // Autodesctruction...
555 video.destroy().asCallback(function (err) {
556 if (err) logger.error('Cannot destruct video after transcoding failure.', { error: err })
566 // ------------------------------ STATICS ------------------------------
568 generateThumbnailFromData = function (video: VideoInstance, thumbnailData: string) {
569 // Creating the thumbnail for a remote video
571 const thumbnailName = video.getThumbnailName()
572 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName)
573 return writeFilePromise(thumbnailPath, Buffer.from(thumbnailData, 'binary')).then(() => {
578 getDurationFromFile = function (videoPath: string) {
579 return new Promise<number>((res, rej) => {
580 ffmpeg.ffprobe(videoPath, function (err, metadata) {
581 if (err) return rej(err)
583 return res(Math.floor(metadata.format.duration))
589 return Video.findAll()
592 listForApi = function (start: number, count: number, sort: string) {
593 // Exclude Blakclisted videos from the list
598 order: [ getSort(sort), [ Video['sequelize'].models.Tag, 'name', 'ASC' ] ],
601 model: Video['sequelize'].models.Author,
602 include: [ { model: Video['sequelize'].models.Pod, required: false } ]
605 Video['sequelize'].models.Tag
607 where: createBaseVideosWhere()
610 return Video.findAndCountAll(query).then(({ rows, count }) => {
618 loadByHostAndRemoteId = function (fromHost: string, remoteId: string) {
625 model: Video['sequelize'].models.Author,
628 model: Video['sequelize'].models.Pod,
639 return Video.findOne(query)
642 listOwnedAndPopulateAuthorAndTags = function () {
643 // If remoteId is null this is *our* video
648 include: [ Video['sequelize'].models.Author, Video['sequelize'].models.Tag ]
651 return Video.findAll(query)
654 listOwnedByAuthor = function (author: string) {
661 model: Video['sequelize'].models.Author,
669 return Video.findAll(query)
672 load = function (id: string) {
673 return Video.findById(id)
676 loadAndPopulateAuthor = function (id: string) {
678 include: [ Video['sequelize'].models.Author ]
681 return Video.findById(id, options)
684 loadAndPopulateAuthorAndPodAndTags = function (id: string) {
688 model: Video['sequelize'].models.Author,
689 include: [ { model: Video['sequelize'].models.Pod, required: false } ]
691 Video['sequelize'].models.Tag
695 return Video.findById(id, options)
698 searchAndPopulateAuthorAndPodAndTags = function (value: string, field: string, start: number, count: number, sort: string) {
699 const podInclude: any = {
700 model: Video['sequelize'].models.Pod,
704 const authorInclude: any = {
705 model: Video['sequelize'].models.Author,
711 const tagInclude: any = {
712 model: Video['sequelize'].models.Tag
717 where: createBaseVideosWhere(),
720 order: [ getSort(sort), [ Video['sequelize'].models.Tag, 'name', 'ASC' ] ]
723 // Make an exact search with the magnet
724 if (field === 'magnetUri') {
725 const infoHash = magnetUtil.decode(value).infoHash
726 query.where.infoHash = infoHash
727 } else if (field === 'tags') {
728 const escapedValue = Video['sequelize'].escape('%' + value + '%')
729 query.where.id.$in = Video['sequelize'].literal(
730 `(SELECT "VideoTags"."videoId"
732 INNER JOIN "VideoTags" ON "Tags"."id" = "VideoTags"."tagId"
733 WHERE name LIKE ${escapedValue}
736 } else if (field === 'host') {
737 // FIXME: Include our pod? (not stored in the database)
740 $like: '%' + value + '%'
743 podInclude.required = true
744 } else if (field === 'author') {
745 authorInclude.where = {
747 $like: '%' + value + '%'
751 // authorInclude.or = true
753 query.where[field] = {
754 $like: '%' + value + '%'
759 authorInclude, tagInclude
762 if (tagInclude.where) {
763 // query.include.push([ Video['sequelize'].models.Tag ])
766 return Video.findAndCountAll(query).then(({ rows, count }) => {
774 // ---------------------------------------------------------------------------
776 function createBaseVideosWhere () {
779 $notIn: Video['sequelize'].literal(
780 '(SELECT "BlacklistedVideos"."videoId" FROM "BlacklistedVideos")'
786 function removeThumbnail (video: VideoInstance) {
787 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName())
788 return unlinkPromise(thumbnailPath)
791 function removeFile (video: VideoInstance) {
792 const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename())
793 return unlinkPromise(filePath)
796 function removeTorrent (video: VideoInstance) {
797 const torrenPath = join(CONFIG.STORAGE.TORRENTS_DIR, video.getTorrentName())
798 return unlinkPromise(torrenPath)
801 function removePreview (video: VideoInstance) {
802 // Same name than video thumnail
803 return unlinkPromise(CONFIG.STORAGE.PREVIEWS_DIR + video.getPreviewName())
806 function createTorrentFromVideo (video: VideoInstance, videoPath: string) {
809 [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ]
812 CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + video.getVideoFilename()
816 return createTorrentPromise(videoPath, options)
818 const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, video.getTorrentName())
819 return writeFilePromise(filePath, torrent).then(() => torrent)
822 const parsedTorrent = parseTorrent(torrent)
823 video.set('infoHash', parsedTorrent.infoHash)
824 return video.validate()
828 function createPreview (video: VideoInstance, videoPath: string) {
829 return generateImage(video, videoPath, CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName(), null)
832 function createThumbnail (video: VideoInstance, videoPath: string) {
833 return generateImage(video, videoPath, CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName(), THUMBNAILS_SIZE)
836 function generateImage (video: VideoInstance, videoPath: string, folder: string, imageName: string, size: string) {
837 const options: any = {
847 return new Promise<string>((res, rej) => {
850 .on('end', function () {
851 return res(imageName)
857 function removeFromBlacklist (video: VideoInstance) {
858 // Find the blacklisted video
859 return db.BlacklistedVideo.loadByVideoId(video.id).then(video => {
860 // Not found the video, skip
865 // If we found the video, remove it from the blacklist
866 return video.destroy()