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 } 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 { TagInstance } from './tag-interface'
12 import { UserInstance } from '../user/user-interface'
20 isVideoDescriptionValid,
22 readFileBufferPromise,
27 } from '../../helpers'
36 VIDEO_FILE_RESOLUTIONS
37 } from '../../initializers'
38 import { removeVideoToFriends } from '../../lib'
39 import { VideoFileInstance } from './video-file-interface'
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 getTorrentFileName: VideoMethods.GetTorrentFileName
55 let isOwned: VideoMethods.IsOwned
56 let toFormattedJSON: VideoMethods.ToFormattedJSON
57 let toAddRemoteJSON: VideoMethods.ToAddRemoteJSON
58 let toUpdateRemoteJSON: VideoMethods.ToUpdateRemoteJSON
59 let transcodeVideofile: VideoMethods.TranscodeVideofile
60 let createPreview: VideoMethods.CreatePreview
61 let createThumbnail: VideoMethods.CreateThumbnail
62 let getVideoFilePath: VideoMethods.GetVideoFilePath
63 let createTorrentAndSetInfoHash: VideoMethods.CreateTorrentAndSetInfoHash
65 let generateThumbnailFromData: VideoMethods.GenerateThumbnailFromData
66 let getDurationFromFile: VideoMethods.GetDurationFromFile
67 let list: VideoMethods.List
68 let listForApi: VideoMethods.ListForApi
69 let loadByHostAndUUID: VideoMethods.LoadByHostAndUUID
70 let listOwnedAndPopulateAuthorAndTags: VideoMethods.ListOwnedAndPopulateAuthorAndTags
71 let listOwnedByAuthor: VideoMethods.ListOwnedByAuthor
72 let load: VideoMethods.Load
73 let loadByUUID: VideoMethods.LoadByUUID
74 let loadAndPopulateAuthor: VideoMethods.LoadAndPopulateAuthor
75 let loadAndPopulateAuthorAndPodAndTags: VideoMethods.LoadAndPopulateAuthorAndPodAndTags
76 let loadByUUIDAndPopulateAuthorAndPodAndTags: VideoMethods.LoadByUUIDAndPopulateAuthorAndPodAndTags
77 let searchAndPopulateAuthorAndPodAndTags: VideoMethods.SearchAndPopulateAuthorAndPodAndTags
78 let removeThumbnail: VideoMethods.RemoveThumbnail
79 let removePreview: VideoMethods.RemovePreview
80 let removeFile: VideoMethods.RemoveFile
81 let removeTorrent: VideoMethods.RemoveTorrent
83 export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
84 Video = sequelize.define<VideoInstance, VideoAttributes>('Video',
88 defaultValue: DataTypes.UUIDV4,
95 type: DataTypes.STRING,
99 const res = isVideoNameValid(value)
100 if (res === false) throw new Error('Video name is not valid.')
105 type: DataTypes.INTEGER,
108 categoryValid: value => {
109 const res = isVideoCategoryValid(value)
110 if (res === false) throw new Error('Video category is not valid.')
115 type: DataTypes.INTEGER,
119 licenceValid: value => {
120 const res = isVideoLicenceValid(value)
121 if (res === false) throw new Error('Video licence is not valid.')
126 type: DataTypes.INTEGER,
129 languageValid: value => {
130 const res = isVideoLanguageValid(value)
131 if (res === false) throw new Error('Video language is not valid.')
136 type: DataTypes.BOOLEAN,
139 nsfwValid: value => {
140 const res = isVideoNSFWValid(value)
141 if (res === false) throw new Error('Video nsfw attribute is not valid.')
146 type: DataTypes.STRING,
149 descriptionValid: value => {
150 const res = isVideoDescriptionValid(value)
151 if (res === false) throw new Error('Video description is not valid.')
156 type: DataTypes.INTEGER,
159 durationValid: value => {
160 const res = isVideoDurationValid(value)
161 if (res === false) throw new Error('Video duration is not valid.')
166 type: DataTypes.INTEGER,
175 type: DataTypes.INTEGER,
184 type: DataTypes.INTEGER,
193 type: DataTypes.BOOLEAN,
201 fields: [ 'authorId' ]
207 fields: [ 'createdAt' ]
210 fields: [ 'duration' ]
228 const classMethods = [
231 generateThumbnailFromData,
235 listOwnedAndPopulateAuthorAndTags,
238 loadAndPopulateAuthor,
239 loadAndPopulateAuthorAndPodAndTags,
242 loadByUUIDAndPopulateAuthorAndPodAndTags,
243 searchAndPopulateAuthorAndPodAndTags
245 const instanceMethods = [
248 createTorrentAndSetInfoHash,
265 addMethodsToModel(Video, classMethods, instanceMethods)
270 // ------------------------------ METHODS ------------------------------
272 function associate (models) {
273 Video.belongsTo(models.Author, {
281 Video.belongsToMany(models.Tag, {
282 foreignKey: 'videoId',
283 through: models.VideoTag,
287 Video.hasMany(models.VideoAbuse, {
295 Video.hasMany(models.VideoFile, {
304 function afterDestroy (video: VideoInstance) {
308 video.removeThumbnail()
311 if (video.isOwned()) {
312 const removeVideoToFriendsParams = {
317 video.removePreview(),
318 removeVideoToFriends(removeVideoToFriendsParams)
321 // TODO: check files is populated
322 video.VideoFiles.forEach(file => {
323 video.removeFile(file),
324 video.removeTorrent(file)
328 return Promise.all(tasks)
331 getVideoFilename = function (this: VideoInstance, videoFile: VideoFileInstance) {
332 // return this.uuid + '-' + VIDEO_FILE_RESOLUTIONS[videoFile.resolution] + videoFile.extname
333 return this.uuid + videoFile.extname
336 getThumbnailName = function (this: VideoInstance) {
337 // We always have a copy of the thumbnail
338 const extension = '.jpg'
339 return this.uuid + extension
342 getPreviewName = function (this: VideoInstance) {
343 const extension = '.jpg'
344 return this.uuid + extension
347 getTorrentFileName = function (this: VideoInstance, videoFile: VideoFileInstance) {
348 const extension = '.torrent'
349 // return this.uuid + '-' + VIDEO_FILE_RESOLUTIONS[videoFile.resolution] + extension
350 return this.uuid + extension
353 isOwned = function (this: VideoInstance) {
354 return this.remote === false
357 createPreview = function (this: VideoInstance, videoFile: VideoFileInstance) {
358 return generateImage(this, this.getVideoFilePath(videoFile), CONFIG.STORAGE.PREVIEWS_DIR, this.getPreviewName(), null)
361 createThumbnail = function (this: VideoInstance, videoFile: VideoFileInstance) {
362 return generateImage(this, this.getVideoFilePath(videoFile), CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName(), THUMBNAILS_SIZE)
365 getVideoFilePath = function (this: VideoInstance, videoFile: VideoFileInstance) {
366 return join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
369 createTorrentAndSetInfoHash = function (this: VideoInstance, videoFile: VideoFileInstance) {
372 [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ]
375 CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
379 return createTorrentPromise(this.getVideoFilePath(videoFile), options)
381 const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
382 return writeFilePromise(filePath, torrent).then(() => torrent)
385 const parsedTorrent = parseTorrent(torrent)
387 videoFile.infoHash = parsedTorrent.infoHash
391 generateMagnetUri = function (this: VideoInstance, videoFile: VideoFileInstance) {
395 if (this.isOwned()) {
396 baseUrlHttp = CONFIG.WEBSERVER.URL
397 baseUrlWs = CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
399 baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + this.Author.Pod.host
400 baseUrlWs = REMOTE_SCHEME.WS + '://' + this.Author.Pod.host
403 const xs = baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentFileName(videoFile)
404 const announce = [ baseUrlWs + '/tracker/socket' ]
405 const urlList = [ baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile) ]
411 infoHash: videoFile.infoHash,
415 return magnetUtil.encode(magnetHash)
418 toFormattedJSON = function (this: VideoInstance) {
421 if (this.Author.Pod) {
422 podHost = this.Author.Pod.host
424 // It means it's our video
425 podHost = CONFIG.WEBSERVER.HOST
428 // Maybe our pod is not up to date and there are new categories since our version
429 let categoryLabel = VIDEO_CATEGORIES[this.category]
430 if (!categoryLabel) categoryLabel = 'Misc'
432 // Maybe our pod is not up to date and there are new licences since our version
433 let licenceLabel = VIDEO_LICENCES[this.licence]
434 if (!licenceLabel) licenceLabel = 'Unknown'
436 // Language is an optional attribute
437 let languageLabel = VIDEO_LANGUAGES[this.language]
438 if (!languageLabel) languageLabel = 'Unknown'
444 category: this.category,
446 licence: this.licence,
448 language: this.language,
451 description: this.description,
453 isLocal: this.isOwned(),
454 author: this.Author.name,
455 duration: this.duration,
458 dislikes: this.dislikes,
459 tags: map<TagInstance, string>(this.Tags, 'name'),
460 thumbnailPath: join(STATIC_PATHS.THUMBNAILS, this.getThumbnailName()),
461 previewPath: join(STATIC_PATHS.PREVIEWS, this.getPreviewName()),
462 createdAt: this.createdAt,
463 updatedAt: this.updatedAt,
467 this.VideoFiles.forEach(videoFile => {
468 let resolutionLabel = VIDEO_FILE_RESOLUTIONS[videoFile.resolution]
469 if (!resolutionLabel) resolutionLabel = 'Unknown'
471 const videoFileJson = {
472 resolution: videoFile.resolution,
474 magnetUri: this.generateMagnetUri(videoFile),
478 json.files.push(videoFileJson)
484 toAddRemoteJSON = function (this: VideoInstance) {
485 // Get thumbnail data to send to the other pod
486 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
488 return readFileBufferPromise(thumbnailPath).then(thumbnailData => {
489 const remoteVideo = {
492 category: this.category,
493 licence: this.licence,
494 language: this.language,
496 description: this.description,
497 author: this.Author.name,
498 duration: this.duration,
499 thumbnailData: thumbnailData.toString('binary'),
500 tags: map<TagInstance, string>(this.Tags, 'name'),
501 createdAt: this.createdAt,
502 updatedAt: this.updatedAt,
505 dislikes: this.dislikes,
509 this.VideoFiles.forEach(videoFile => {
510 remoteVideo.files.push({
511 infoHash: videoFile.infoHash,
512 resolution: videoFile.resolution,
513 extname: videoFile.extname,
522 toUpdateRemoteJSON = function (this: VideoInstance) {
526 category: this.category,
527 licence: this.licence,
528 language: this.language,
530 description: this.description,
531 author: this.Author.name,
532 duration: this.duration,
533 tags: map<TagInstance, string>(this.Tags, 'name'),
534 createdAt: this.createdAt,
535 updatedAt: this.updatedAt,
538 dislikes: this.dislikes,
542 this.VideoFiles.forEach(videoFile => {
544 infoHash: videoFile.infoHash,
545 resolution: videoFile.resolution,
546 extname: videoFile.extname,
554 transcodeVideofile = function (this: VideoInstance, inputVideoFile: VideoFileInstance) {
555 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
556 const newExtname = '.mp4'
557 const videoInputPath = join(videosDirectory, this.getVideoFilename(inputVideoFile))
558 const videoOutputPath = join(videosDirectory, this.id + '-transcoded' + newExtname)
560 return new Promise<void>((res, rej) => {
561 ffmpeg(videoInputPath)
562 .output(videoOutputPath)
563 .videoCodec('libx264')
564 .outputOption('-threads ' + CONFIG.TRANSCODING.THREADS)
565 .outputOption('-movflags faststart')
569 return unlinkPromise(videoInputPath)
571 // Important to do this before getVideoFilename() to take in account the new file extension
572 inputVideoFile.set('extname', newExtname)
574 return renamePromise(videoOutputPath, this.getVideoFilePath(inputVideoFile))
577 return this.createTorrentAndSetInfoHash(inputVideoFile)
580 return inputVideoFile.save()
586 // Auto destruction...
587 this.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', err))
596 removeThumbnail = function (this: VideoInstance) {
597 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
598 return unlinkPromise(thumbnailPath)
601 removePreview = function (this: VideoInstance) {
602 // Same name than video thumbnail
603 return unlinkPromise(CONFIG.STORAGE.PREVIEWS_DIR + this.getPreviewName())
606 removeFile = function (this: VideoInstance, videoFile: VideoFileInstance) {
607 const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
608 return unlinkPromise(filePath)
611 removeTorrent = function (this: VideoInstance, videoFile: VideoFileInstance) {
612 const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
613 return unlinkPromise(torrentPath)
616 // ------------------------------ STATICS ------------------------------
618 generateThumbnailFromData = function (video: VideoInstance, thumbnailData: string) {
619 // Creating the thumbnail for a remote video
621 const thumbnailName = video.getThumbnailName()
622 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName)
623 return writeFilePromise(thumbnailPath, Buffer.from(thumbnailData, 'binary')).then(() => {
628 getDurationFromFile = function (videoPath: string) {
629 return new Promise<number>((res, rej) => {
630 ffmpeg.ffprobe(videoPath, (err, metadata) => {
631 if (err) return rej(err)
633 return res(Math.floor(metadata.format.duration))
640 include: [ Video['sequelize'].models.VideoFile ]
643 return Video.findAll(query)
646 listForApi = function (start: number, count: number, sort: string) {
647 // Exclude blacklisted videos from the list
652 order: [ getSort(sort), [ Video['sequelize'].models.Tag, 'name', 'ASC' ] ],
655 model: Video['sequelize'].models.Author,
656 include: [ { model: Video['sequelize'].models.Pod, required: false } ]
658 Video['sequelize'].models.Tag,
659 Video['sequelize'].models.VideoFile
661 where: createBaseVideosWhere()
664 return Video.findAndCountAll(query).then(({ rows, count }) => {
672 loadByHostAndUUID = function (fromHost: string, uuid: string) {
679 model: Video['sequelize'].models.VideoFile
682 model: Video['sequelize'].models.Author,
685 model: Video['sequelize'].models.Pod,
696 return Video.findOne(query)
699 listOwnedAndPopulateAuthorAndTags = function () {
705 Video['sequelize'].models.VideoFile,
706 Video['sequelize'].models.Author,
707 Video['sequelize'].models.Tag
711 return Video.findAll(query)
714 listOwnedByAuthor = function (author: string) {
721 model: Video['sequelize'].models.VideoFile
724 model: Video['sequelize'].models.Author,
732 return Video.findAll(query)
735 load = function (id: number) {
736 return Video.findById(id)
739 loadByUUID = function (uuid: string) {
744 include: [ Video['sequelize'].models.VideoFile ]
746 return Video.findOne(query)
749 loadAndPopulateAuthor = function (id: number) {
751 include: [ Video['sequelize'].models.VideoFile, Video['sequelize'].models.Author ]
754 return Video.findById(id, options)
757 loadAndPopulateAuthorAndPodAndTags = function (id: number) {
761 model: Video['sequelize'].models.Author,
762 include: [ { model: Video['sequelize'].models.Pod, required: false } ]
764 Video['sequelize'].models.Tag,
765 Video['sequelize'].models.VideoFile
769 return Video.findById(id, options)
772 loadByUUIDAndPopulateAuthorAndPodAndTags = function (uuid: string) {
779 model: Video['sequelize'].models.Author,
780 include: [ { model: Video['sequelize'].models.Pod, required: false } ]
782 Video['sequelize'].models.Tag,
783 Video['sequelize'].models.VideoFile
787 return Video.findOne(options)
790 searchAndPopulateAuthorAndPodAndTags = function (value: string, field: string, start: number, count: number, sort: string) {
791 const podInclude: Sequelize.IncludeOptions = {
792 model: Video['sequelize'].models.Pod,
796 const authorInclude: Sequelize.IncludeOptions = {
797 model: Video['sequelize'].models.Author,
803 const tagInclude: Sequelize.IncludeOptions = {
804 model: Video['sequelize'].models.Tag
807 const videoFileInclude: Sequelize.IncludeOptions = {
808 model: Video['sequelize'].models.VideoFile
811 const query: Sequelize.FindOptions<VideoAttributes> = {
813 where: createBaseVideosWhere(),
816 order: [ getSort(sort), [ Video['sequelize'].models.Tag, 'name', 'ASC' ] ]
819 // Make an exact search with the magnet
820 if (field === 'magnetUri') {
821 videoFileInclude.where = {
822 infoHash: magnetUtil.decode(value).infoHash
824 } else if (field === 'tags') {
825 const escapedValue = Video['sequelize'].escape('%' + value + '%')
826 query.where['id'].$in = Video['sequelize'].literal(
827 `(SELECT "VideoTags"."videoId"
829 INNER JOIN "VideoTags" ON "Tags"."id" = "VideoTags"."tagId"
830 WHERE name ILIKE ${escapedValue}
833 } else if (field === 'host') {
834 // FIXME: Include our pod? (not stored in the database)
837 $iLike: '%' + value + '%'
840 podInclude.required = true
841 } else if (field === 'author') {
842 authorInclude.where = {
844 $iLike: '%' + value + '%'
848 // authorInclude.or = true
850 query.where[field] = {
851 $iLike: '%' + value + '%'
856 authorInclude, tagInclude, videoFileInclude
859 return Video.findAndCountAll(query).then(({ rows, count }) => {
867 // ---------------------------------------------------------------------------
869 function createBaseVideosWhere () {
872 $notIn: Video['sequelize'].literal(
873 '(SELECT "BlacklistedVideos"."videoId" FROM "BlacklistedVideos")'
879 function generateImage (video: VideoInstance, videoPath: string, folder: string, imageName: string, size: string) {
887 options['size'] = size
890 return new Promise<string>((res, rej) => {
893 .on('end', () => res(imageName))