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,
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: value => {
105 const res = isVideoCategoryValid(value)
106 if (res === false) throw new Error('Video category is not valid.')
111 type: DataTypes.INTEGER,
115 licenceValid: value => {
116 const res = isVideoLicenceValid(value)
117 if (res === false) throw new Error('Video licence is not valid.')
122 type: DataTypes.INTEGER,
125 languageValid: value => {
126 const res = isVideoLanguageValid(value)
127 if (res === false) throw new Error('Video language is not valid.')
132 type: DataTypes.BOOLEAN,
135 nsfwValid: 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: value => {
146 const res = isVideoDescriptionValid(value)
147 if (res === false) throw new Error('Video description is not valid.')
152 type: DataTypes.STRING,
155 infoHashValid: 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: 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 previewPath: join(STATIC_PATHS.PREVIEWS, this.getPreviewName()),
455 createdAt: this.createdAt,
456 updatedAt: this.updatedAt
462 toAddRemoteJSON = function (this: VideoInstance) {
463 // Get thumbnail data to send to the other pod
464 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
466 return readFileBufferPromise(thumbnailPath).then(thumbnailData => {
467 const remoteVideo = {
470 category: this.category,
471 licence: this.licence,
472 language: this.language,
474 description: this.description,
475 infoHash: this.infoHash,
476 author: this.Author.name,
477 duration: this.duration,
478 thumbnailData: thumbnailData.toString('binary'),
479 tags: map<TagInstance, string>(this.Tags, 'name'),
480 createdAt: this.createdAt,
481 updatedAt: this.updatedAt,
482 extname: this.extname,
485 dislikes: this.dislikes
492 toUpdateRemoteJSON = function (this: VideoInstance) {
496 category: this.category,
497 licence: this.licence,
498 language: this.language,
500 description: this.description,
501 infoHash: this.infoHash,
502 author: this.Author.name,
503 duration: this.duration,
504 tags: map<TagInstance, string>(this.Tags, 'name'),
505 createdAt: this.createdAt,
506 updatedAt: this.updatedAt,
507 extname: this.extname,
510 dislikes: this.dislikes
516 transcodeVideofile = function (this: VideoInstance) {
519 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
520 const newExtname = '.mp4'
521 const videoInputPath = join(videosDirectory, video.getVideoFilename())
522 const videoOutputPath = join(videosDirectory, video.id + '-transcoded' + newExtname)
524 return new Promise<void>((res, rej) => {
525 ffmpeg(videoInputPath)
526 .output(videoOutputPath)
527 .videoCodec('libx264')
528 .outputOption('-threads ' + CONFIG.TRANSCODING.THREADS)
529 .outputOption('-movflags faststart')
533 return unlinkPromise(videoInputPath)
535 // Important to do this before getVideoFilename() to take in account the new file extension
536 video.set('extname', newExtname)
538 const newVideoPath = join(videosDirectory, video.getVideoFilename())
539 return renamePromise(videoOutputPath, newVideoPath)
542 const newVideoPath = join(videosDirectory, video.getVideoFilename())
543 return createTorrentFromVideo(video, newVideoPath)
552 // Autodesctruction...
553 video.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', err))
562 // ------------------------------ STATICS ------------------------------
564 generateThumbnailFromData = function (video: VideoInstance, thumbnailData: string) {
565 // Creating the thumbnail for a remote video
567 const thumbnailName = video.getThumbnailName()
568 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName)
569 return writeFilePromise(thumbnailPath, Buffer.from(thumbnailData, 'binary')).then(() => {
574 getDurationFromFile = function (videoPath: string) {
575 return new Promise<number>((res, rej) => {
576 ffmpeg.ffprobe(videoPath, (err, metadata) => {
577 if (err) return rej(err)
579 return res(Math.floor(metadata.format.duration))
585 return Video.findAll()
588 listForApi = function (start: number, count: number, sort: string) {
589 // Exclude Blakclisted videos from the list
594 order: [ getSort(sort), [ Video['sequelize'].models.Tag, 'name', 'ASC' ] ],
597 model: Video['sequelize'].models.Author,
598 include: [ { model: Video['sequelize'].models.Pod, required: false } ]
601 Video['sequelize'].models.Tag
603 where: createBaseVideosWhere()
606 return Video.findAndCountAll(query).then(({ rows, count }) => {
614 loadByHostAndUUID = function (fromHost: string, uuid: string) {
621 model: Video['sequelize'].models.Author,
624 model: Video['sequelize'].models.Pod,
635 return Video.findOne(query)
638 listOwnedAndPopulateAuthorAndTags = function () {
643 include: [ Video['sequelize'].models.Author, Video['sequelize'].models.Tag ]
646 return Video.findAll(query)
649 listOwnedByAuthor = function (author: string) {
656 model: Video['sequelize'].models.Author,
664 return Video.findAll(query)
667 load = function (id: number) {
668 return Video.findById(id)
671 loadByUUID = function (uuid: string) {
677 return Video.findOne(query)
680 loadAndPopulateAuthor = function (id: number) {
682 include: [ Video['sequelize'].models.Author ]
685 return Video.findById(id, options)
688 loadAndPopulateAuthorAndPodAndTags = function (id: number) {
692 model: Video['sequelize'].models.Author,
693 include: [ { model: Video['sequelize'].models.Pod, required: false } ]
695 Video['sequelize'].models.Tag
699 return Video.findById(id, options)
702 loadByUUIDAndPopulateAuthorAndPodAndTags = function (uuid: string) {
709 model: Video['sequelize'].models.Author,
710 include: [ { model: Video['sequelize'].models.Pod, required: false } ]
712 Video['sequelize'].models.Tag
716 return Video.findOne(options)
719 searchAndPopulateAuthorAndPodAndTags = function (value: string, field: string, start: number, count: number, sort: string) {
720 const podInclude: Sequelize.IncludeOptions = {
721 model: Video['sequelize'].models.Pod,
725 const authorInclude: Sequelize.IncludeOptions = {
726 model: Video['sequelize'].models.Author,
732 const tagInclude: Sequelize.IncludeOptions = {
733 model: Video['sequelize'].models.Tag
736 const query: Sequelize.FindOptions = {
738 where: createBaseVideosWhere(),
741 order: [ getSort(sort), [ Video['sequelize'].models.Tag, 'name', 'ASC' ] ]
744 // Make an exact search with the magnet
745 if (field === 'magnetUri') {
746 const infoHash = magnetUtil.decode(value).infoHash
747 query.where['infoHash'] = infoHash
748 } else if (field === 'tags') {
749 const escapedValue = Video['sequelize'].escape('%' + value + '%')
750 query.where['id'].$in = Video['sequelize'].literal(
751 `(SELECT "VideoTags"."videoId"
753 INNER JOIN "VideoTags" ON "Tags"."id" = "VideoTags"."tagId"
754 WHERE name ILIKE ${escapedValue}
757 } else if (field === 'host') {
758 // FIXME: Include our pod? (not stored in the database)
761 $iLike: '%' + value + '%'
764 podInclude.required = true
765 } else if (field === 'author') {
766 authorInclude.where = {
768 $iLike: '%' + value + '%'
772 // authorInclude.or = true
774 query.where[field] = {
775 $iLike: '%' + value + '%'
780 authorInclude, tagInclude
783 return Video.findAndCountAll(query).then(({ rows, count }) => {
791 // ---------------------------------------------------------------------------
793 function createBaseVideosWhere () {
796 $notIn: Video['sequelize'].literal(
797 '(SELECT "BlacklistedVideos"."videoId" FROM "BlacklistedVideos")'
803 function removeThumbnail (video: VideoInstance) {
804 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName())
805 return unlinkPromise(thumbnailPath)
808 function removeFile (video: VideoInstance) {
809 const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename())
810 return unlinkPromise(filePath)
813 function removeTorrent (video: VideoInstance) {
814 const torrenPath = join(CONFIG.STORAGE.TORRENTS_DIR, video.getTorrentName())
815 return unlinkPromise(torrenPath)
818 function removePreview (video: VideoInstance) {
819 // Same name than video thumnail
820 return unlinkPromise(CONFIG.STORAGE.PREVIEWS_DIR + video.getPreviewName())
823 function createTorrentFromVideo (video: VideoInstance, videoPath: string) {
826 [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ]
829 CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + video.getVideoFilename()
833 return createTorrentPromise(videoPath, options)
835 const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, video.getTorrentName())
836 return writeFilePromise(filePath, torrent).then(() => torrent)
839 const parsedTorrent = parseTorrent(torrent)
840 video.set('infoHash', parsedTorrent.infoHash)
841 return video.validate()
845 function createPreview (video: VideoInstance, videoPath: string) {
846 return generateImage(video, videoPath, CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName(), null)
849 function createThumbnail (video: VideoInstance, videoPath: string) {
850 return generateImage(video, videoPath, CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName(), THUMBNAILS_SIZE)
853 function generateImage (video: VideoInstance, videoPath: string, folder: string, imageName: string, size: string) {
861 options['size'] = size
864 return new Promise<string>((res, rej) => {
867 .on('end', () => res(imageName))
872 function removeFromBlacklist (video: VideoInstance) {
873 // Find the blacklisted video
874 return db.BlacklistedVideo.loadByVideoId(video.id).then(video => {
875 // Not found the video, skip
880 // If we found the video, remove it from the blacklist
881 return video.destroy()