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 loadByHostAndUUID: VideoMethods.LoadByHostAndUUID
66 let listOwnedAndPopulateAuthorAndTags: VideoMethods.ListOwnedAndPopulateAuthorAndTags
67 let listOwnedByAuthor: VideoMethods.ListOwnedByAuthor
68 let load: VideoMethods.Load
69 let loadByUUID: VideoMethods.LoadByUUID
70 let loadAndPopulateAuthor: VideoMethods.LoadAndPopulateAuthor
71 let loadAndPopulateAuthorAndPodAndTags: VideoMethods.LoadAndPopulateAuthorAndPodAndTags
72 let loadByUUIDAndPopulateAuthorAndPodAndTags: VideoMethods.LoadByUUIDAndPopulateAuthorAndPodAndTags
73 let searchAndPopulateAuthorAndPodAndTags: VideoMethods.SearchAndPopulateAuthorAndPodAndTags
75 export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
76 Video = sequelize.define<VideoInstance, VideoAttributes>('Video',
80 defaultValue: DataTypes.UUIDV4,
87 type: DataTypes.STRING,
90 nameValid: function (value) {
91 const res = isVideoNameValid(value)
92 if (res === false) throw new Error('Video name is not valid.')
97 type: DataTypes.ENUM(values(CONSTRAINTS_FIELDS.VIDEOS.EXTNAME)),
101 type: DataTypes.INTEGER,
104 categoryValid: function (value) {
105 const res = isVideoCategoryValid(value)
106 if (res === false) throw new Error('Video category is not valid.')
111 type: DataTypes.INTEGER,
115 licenceValid: function (value) {
116 const res = isVideoLicenceValid(value)
117 if (res === false) throw new Error('Video licence is not valid.')
122 type: DataTypes.INTEGER,
125 languageValid: function (value) {
126 const res = isVideoLanguageValid(value)
127 if (res === false) throw new Error('Video language is not valid.')
132 type: DataTypes.BOOLEAN,
135 nsfwValid: function (value) {
136 const res = isVideoNSFWValid(value)
137 if (res === false) throw new Error('Video nsfw attribute is not valid.')
142 type: DataTypes.STRING,
145 descriptionValid: function (value) {
146 const res = isVideoDescriptionValid(value)
147 if (res === false) throw new Error('Video description is not valid.')
152 type: DataTypes.STRING,
155 infoHashValid: function (value) {
156 const res = isVideoInfoHashValid(value)
157 if (res === false) throw new Error('Video info hash is not valid.')
162 type: DataTypes.INTEGER,
165 durationValid: function (value) {
166 const res = isVideoDurationValid(value)
167 if (res === false) throw new Error('Video duration is not valid.')
172 type: DataTypes.INTEGER,
181 type: DataTypes.INTEGER,
190 type: DataTypes.INTEGER,
199 type: DataTypes.BOOLEAN,
207 fields: [ 'authorId' ]
213 fields: [ 'createdAt' ]
216 fields: [ 'duration' ]
219 fields: [ 'infoHash' ]
239 const classMethods = [
242 generateThumbnailFromData,
246 listOwnedAndPopulateAuthorAndTags,
251 loadAndPopulateAuthor,
252 loadAndPopulateAuthorAndPodAndTags,
253 loadByUUIDAndPopulateAuthorAndPodAndTags,
254 searchAndPopulateAuthorAndPodAndTags,
257 const instanceMethods = [
269 addMethodsToModel(Video, classMethods, instanceMethods)
274 function beforeValidate (video: VideoInstance) {
275 // Put a fake infoHash if it does not exists yet
276 if (video.isOwned() && !video.infoHash) {
278 video.infoHash = '0123456789abcdef0123456789abcdef01234567'
282 function beforeCreate (video: VideoInstance, options: { transaction: Sequelize.Transaction }) {
283 if (video.isOwned()) {
284 const videoPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename())
288 createTorrentFromVideo(video, videoPath),
289 createThumbnail(video, videoPath),
290 createPreview(video, videoPath)
293 if (CONFIG.TRANSCODING.ENABLED === true) {
294 // Put uuid because we don't have id auto incremented for now
296 videoUUID: video.uuid
300 JobScheduler.Instance.createJob(options.transaction, 'videoTranscoder', dataInput)
304 return Promise.all(tasks)
307 return Promise.resolve()
310 function afterDestroy (video: VideoInstance) {
314 removeThumbnail(video)
317 if (video.isOwned()) {
318 const removeVideoToFriendsParams = {
324 removeTorrent(video),
325 removePreview(video),
326 removeVideoToFriends(removeVideoToFriendsParams)
330 return Promise.all(tasks)
333 // ------------------------------ METHODS ------------------------------
335 function associate (models) {
336 Video.belongsTo(models.Author, {
344 Video.belongsToMany(models.Tag, {
345 foreignKey: 'videoId',
346 through: models.VideoTag,
350 Video.hasMany(models.VideoAbuse, {
359 generateMagnetUri = function (this: VideoInstance) {
363 if (this.isOwned()) {
364 baseUrlHttp = CONFIG.WEBSERVER.URL
365 baseUrlWs = CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
367 baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + this.Author.Pod.host
368 baseUrlWs = REMOTE_SCHEME.WS + '://' + this.Author.Pod.host
371 const xs = baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentName()
372 const announce = [ baseUrlWs + '/tracker/socket' ]
373 const urlList = [ baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename() ]
379 infoHash: this.infoHash,
383 return magnetUtil.encode(magnetHash)
386 getVideoFilename = function (this: VideoInstance) {
387 return this.uuid + this.extname
390 getThumbnailName = function (this: VideoInstance) {
391 // We always have a copy of the thumbnail
392 const extension = '.jpg'
393 return this.uuid + extension
396 getPreviewName = function (this: VideoInstance) {
397 const extension = '.jpg'
398 return this.uuid + extension
401 getTorrentName = function (this: VideoInstance) {
402 const extension = '.torrent'
403 return this.uuid + extension
406 isOwned = function (this: VideoInstance) {
407 return this.remote === false
410 toFormatedJSON = function (this: VideoInstance) {
413 if (this.Author.Pod) {
414 podHost = this.Author.Pod.host
416 // It means it's our video
417 podHost = CONFIG.WEBSERVER.HOST
420 // Maybe our pod is not up to date and there are new categories since our version
421 let categoryLabel = VIDEO_CATEGORIES[this.category]
422 if (!categoryLabel) categoryLabel = 'Misc'
424 // Maybe our pod is not up to date and there are new licences since our version
425 let licenceLabel = VIDEO_LICENCES[this.licence]
426 if (!licenceLabel) licenceLabel = 'Unknown'
428 // Language is an optional attribute
429 let languageLabel = VIDEO_LANGUAGES[this.language]
430 if (!languageLabel) languageLabel = 'Unknown'
436 category: this.category,
438 licence: this.licence,
440 language: this.language,
443 description: this.description,
445 isLocal: this.isOwned(),
446 magnetUri: this.generateMagnetUri(),
447 author: this.Author.name,
448 duration: this.duration,
451 dislikes: this.dislikes,
452 tags: map<TagInstance, string>(this.Tags, 'name'),
453 thumbnailPath: join(STATIC_PATHS.THUMBNAILS, this.getThumbnailName()),
454 createdAt: this.createdAt,
455 updatedAt: this.updatedAt
461 toAddRemoteJSON = function (this: VideoInstance) {
462 // Get thumbnail data to send to the other pod
463 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
465 return readFileBufferPromise(thumbnailPath).then(thumbnailData => {
466 const remoteVideo = {
469 category: this.category,
470 licence: this.licence,
471 language: this.language,
473 description: this.description,
474 infoHash: this.infoHash,
475 author: this.Author.name,
476 duration: this.duration,
477 thumbnailData: thumbnailData.toString('binary'),
478 tags: map<TagInstance, string>(this.Tags, 'name'),
479 createdAt: this.createdAt,
480 updatedAt: this.updatedAt,
481 extname: this.extname,
484 dislikes: this.dislikes
491 toUpdateRemoteJSON = function (this: VideoInstance) {
495 category: this.category,
496 licence: this.licence,
497 language: this.language,
499 description: this.description,
500 infoHash: this.infoHash,
501 author: this.Author.name,
502 duration: this.duration,
503 tags: map<TagInstance, string>(this.Tags, 'name'),
504 createdAt: this.createdAt,
505 updatedAt: this.updatedAt,
506 extname: this.extname,
509 dislikes: this.dislikes
515 transcodeVideofile = function (this: VideoInstance) {
518 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
519 const newExtname = '.mp4'
520 const videoInputPath = join(videosDirectory, video.getVideoFilename())
521 const videoOutputPath = join(videosDirectory, video.id + '-transcoded' + newExtname)
523 return new Promise<void>((res, rej) => {
524 ffmpeg(videoInputPath)
525 .output(videoOutputPath)
526 .videoCodec('libx264')
527 .outputOption('-threads ' + CONFIG.TRANSCODING.THREADS)
528 .outputOption('-movflags faststart')
532 return unlinkPromise(videoInputPath)
534 // Important to do this before getVideoFilename() to take in account the new file extension
535 video.set('extname', newExtname)
537 const newVideoPath = join(videosDirectory, video.getVideoFilename())
538 return renamePromise(videoOutputPath, newVideoPath)
541 const newVideoPath = join(videosDirectory, video.getVideoFilename())
542 return createTorrentFromVideo(video, newVideoPath)
551 // Autodesctruction...
552 video.destroy().asCallback(function (err) {
553 if (err) logger.error('Cannot destruct video after transcoding failure.', err)
563 // ------------------------------ STATICS ------------------------------
565 generateThumbnailFromData = function (video: VideoInstance, thumbnailData: string) {
566 // Creating the thumbnail for a remote video
568 const thumbnailName = video.getThumbnailName()
569 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName)
570 return writeFilePromise(thumbnailPath, Buffer.from(thumbnailData, 'binary')).then(() => {
575 getDurationFromFile = function (videoPath: string) {
576 return new Promise<number>((res, rej) => {
577 ffmpeg.ffprobe(videoPath, function (err, metadata) {
578 if (err) return rej(err)
580 return res(Math.floor(metadata.format.duration))
586 return Video.findAll()
589 listForApi = function (start: number, count: number, sort: string) {
590 // Exclude Blakclisted videos from the list
595 order: [ getSort(sort), [ Video['sequelize'].models.Tag, 'name', 'ASC' ] ],
598 model: Video['sequelize'].models.Author,
599 include: [ { model: Video['sequelize'].models.Pod, required: false } ]
602 Video['sequelize'].models.Tag
604 where: createBaseVideosWhere()
607 return Video.findAndCountAll(query).then(({ rows, count }) => {
615 loadByHostAndUUID = function (fromHost: string, uuid: string) {
622 model: Video['sequelize'].models.Author,
625 model: Video['sequelize'].models.Pod,
636 return Video.findOne(query)
639 listOwnedAndPopulateAuthorAndTags = function () {
644 include: [ Video['sequelize'].models.Author, Video['sequelize'].models.Tag ]
647 return Video.findAll(query)
650 listOwnedByAuthor = function (author: string) {
657 model: Video['sequelize'].models.Author,
665 return Video.findAll(query)
668 load = function (id: number) {
669 return Video.findById(id)
672 loadByUUID = function (uuid: string) {
678 return Video.findOne(query)
681 loadAndPopulateAuthor = function (id: number) {
683 include: [ Video['sequelize'].models.Author ]
686 return Video.findById(id, options)
689 loadAndPopulateAuthorAndPodAndTags = function (id: number) {
693 model: Video['sequelize'].models.Author,
694 include: [ { model: Video['sequelize'].models.Pod, required: false } ]
696 Video['sequelize'].models.Tag
700 return Video.findById(id, options)
703 loadByUUIDAndPopulateAuthorAndPodAndTags = function (uuid: string) {
710 model: Video['sequelize'].models.Author,
711 include: [ { model: Video['sequelize'].models.Pod, required: false } ]
713 Video['sequelize'].models.Tag
717 return Video.findOne(options)
720 searchAndPopulateAuthorAndPodAndTags = function (value: string, field: string, start: number, count: number, sort: string) {
721 const podInclude: Sequelize.IncludeOptions = {
722 model: Video['sequelize'].models.Pod,
726 const authorInclude: Sequelize.IncludeOptions = {
727 model: Video['sequelize'].models.Author,
733 const tagInclude: Sequelize.IncludeOptions = {
734 model: Video['sequelize'].models.Tag
737 const query: Sequelize.FindOptions = {
739 where: createBaseVideosWhere(),
742 order: [ getSort(sort), [ Video['sequelize'].models.Tag, 'name', 'ASC' ] ]
745 // Make an exact search with the magnet
746 if (field === 'magnetUri') {
747 const infoHash = magnetUtil.decode(value).infoHash
748 query.where['infoHash'] = infoHash
749 } else if (field === 'tags') {
750 const escapedValue = Video['sequelize'].escape('%' + value + '%')
751 query.where['id'].$in = Video['sequelize'].literal(
752 `(SELECT "VideoTags"."videoId"
754 INNER JOIN "VideoTags" ON "Tags"."id" = "VideoTags"."tagId"
755 WHERE name ILIKE ${escapedValue}
758 } else if (field === 'host') {
759 // FIXME: Include our pod? (not stored in the database)
762 $iLike: '%' + value + '%'
765 podInclude.required = true
766 } else if (field === 'author') {
767 authorInclude.where = {
769 $iLike: '%' + value + '%'
773 // authorInclude.or = true
775 query.where[field] = {
776 $iLike: '%' + value + '%'
781 authorInclude, tagInclude
784 return Video.findAndCountAll(query).then(({ rows, count }) => {
792 // ---------------------------------------------------------------------------
794 function createBaseVideosWhere () {
797 $notIn: Video['sequelize'].literal(
798 '(SELECT "BlacklistedVideos"."videoId" FROM "BlacklistedVideos")'
804 function removeThumbnail (video: VideoInstance) {
805 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName())
806 return unlinkPromise(thumbnailPath)
809 function removeFile (video: VideoInstance) {
810 const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename())
811 return unlinkPromise(filePath)
814 function removeTorrent (video: VideoInstance) {
815 const torrenPath = join(CONFIG.STORAGE.TORRENTS_DIR, video.getTorrentName())
816 return unlinkPromise(torrenPath)
819 function removePreview (video: VideoInstance) {
820 // Same name than video thumnail
821 return unlinkPromise(CONFIG.STORAGE.PREVIEWS_DIR + video.getPreviewName())
824 function createTorrentFromVideo (video: VideoInstance, videoPath: string) {
827 [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ]
830 CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + video.getVideoFilename()
834 return createTorrentPromise(videoPath, options)
836 const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, video.getTorrentName())
837 return writeFilePromise(filePath, torrent).then(() => torrent)
840 const parsedTorrent = parseTorrent(torrent)
841 video.set('infoHash', parsedTorrent.infoHash)
842 return video.validate()
846 function createPreview (video: VideoInstance, videoPath: string) {
847 return generateImage(video, videoPath, CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName(), null)
850 function createThumbnail (video: VideoInstance, videoPath: string) {
851 return generateImage(video, videoPath, CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName(), THUMBNAILS_SIZE)
854 function generateImage (video: VideoInstance, videoPath: string, folder: string, imageName: string, size: string) {
862 options['size'] = size
865 return new Promise<string>((res, rej) => {
868 .on('end', function () {
869 return res(imageName)
875 function removeFromBlacklist (video: VideoInstance) {
876 // Find the blacklisted video
877 return db.BlacklistedVideo.loadByVideoId(video.id).then(video => {
878 // Not found the video, skip
883 // If we found the video, remove it from the blacklist
884 return video.destroy()