1 import * as Bluebird from 'bluebird'
2 import { map, maxBy, truncate } from 'lodash'
3 import * as magnetUtil from 'magnet-uri'
4 import * as parseTorrent from 'parse-torrent'
5 import { join } from 'path'
6 import * as Sequelize from 'sequelize'
27 } from 'sequelize-typescript'
28 import { IIncludeOptions } from 'sequelize-typescript/lib/interfaces/IIncludeOptions'
29 import { VideoPrivacy, VideoResolution } from '../../../shared'
30 import { VideoTorrentObject } from '../../../shared/models/activitypub/objects'
31 import { Video, VideoDetails } from '../../../shared/models/videos'
33 activityPubCollection,
35 generateImageFromVideoFile,
43 } from '../../helpers'
44 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub'
47 isVideoDescriptionValid,
54 } from '../../helpers/custom-validators/videos'
67 } from '../../initializers'
68 import { getAnnounceActivityPubUrl } from '../../lib/activitypub'
69 import { sendDeleteVideo } from '../../lib/activitypub/send'
70 import { AccountModel } from '../account/account'
71 import { AccountVideoRateModel } from '../account/account-video-rate'
72 import { ActorModel } from '../activitypub/actor'
73 import { ServerModel } from '../server/server'
74 import { getSort, throwIfNotValid } from '../utils'
75 import { TagModel } from './tag'
76 import { VideoAbuseModel } from './video-abuse'
77 import { VideoChannelModel } from './video-channel'
78 import { VideoFileModel } from './video-file'
79 import { VideoShareModel } from './video-share'
80 import { VideoTagModel } from './video-tag'
83 AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST',
84 WITH_ACCOUNT = 'WITH_ACCOUNT',
85 WITH_TAGS = 'WITH_TAGS',
86 WITH_FILES = 'WITH_FILES',
87 WITH_SHARES = 'WITH_SHARES',
88 WITH_RATES = 'WITH_RATES'
92 [ScopeNames.AVAILABLE_FOR_LIST]: {
95 [Sequelize.Op.notIn]: Sequelize.literal(
96 '(SELECT "videoBlacklist"."videoId" FROM "videoBlacklist")'
99 privacy: VideoPrivacy.PUBLIC
102 [ScopeNames.WITH_ACCOUNT]: {
105 model: () => VideoChannelModel,
109 model: () => AccountModel,
113 model: () => ActorModel,
117 model: () => ServerModel,
128 [ScopeNames.WITH_TAGS]: {
129 include: [ () => TagModel ]
131 [ScopeNames.WITH_FILES]: {
134 model: () => VideoFileModel,
139 [ScopeNames.WITH_SHARES]: {
142 model: () => VideoShareModel,
143 include: [ () => ActorModel ]
147 [ScopeNames.WITH_RATES]: {
150 model: () => AccountVideoRateModel,
151 include: [ () => AccountModel ]
163 fields: [ 'createdAt' ]
166 fields: [ 'duration' ]
178 fields: [ 'channelId' ]
182 export class VideoModel extends Model<VideoModel> {
185 @Default(DataType.UUIDV4)
187 @Column(DataType.UUID)
191 @Is('VideoName', value => throwIfNotValid(value, isVideoNameValid, 'name'))
197 @Is('VideoCategory', value => throwIfNotValid(value, isVideoCategoryValid, 'category'))
203 @Is('VideoLicence', value => throwIfNotValid(value, isVideoLicenceValid, 'licence'))
209 @Is('VideoLanguage', value => throwIfNotValid(value, isVideoLanguageValid, 'language'))
214 @Is('VideoPrivacy', value => throwIfNotValid(value, isVideoPrivacyValid, 'privacy'))
219 @Is('VideoNSFW', value => throwIfNotValid(value, isVideoNSFWValid, 'NSFW boolean'))
225 @Is('VideoDescription', value => throwIfNotValid(value, isVideoDescriptionValid, 'description'))
226 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max))
230 @Is('VideoDuration', value => throwIfNotValid(value, isVideoDurationValid, 'duration'))
260 @Is('VideoUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
261 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
270 @ForeignKey(() => VideoChannelModel)
274 @BelongsTo(() => VideoChannelModel, {
280 VideoChannel: VideoChannelModel
282 @BelongsToMany(() => TagModel, {
283 foreignKey: 'videoId',
284 through: () => VideoTagModel,
289 @HasMany(() => VideoAbuseModel, {
296 VideoAbuses: VideoAbuseModel[]
298 @HasMany(() => VideoFileModel, {
305 VideoFiles: VideoFileModel[]
307 @HasMany(() => VideoShareModel, {
314 VideoShares: VideoShareModel[]
316 @HasMany(() => AccountVideoRateModel, {
323 AccountVideoRates: AccountVideoRateModel[]
326 static removeFilesAndSendDelete (instance: VideoModel) {
330 instance.removeThumbnail()
333 if (instance.isOwned()) {
335 instance.removePreview(),
336 sendDeleteVideo(instance, undefined)
339 // Remove physical files and torrents
340 instance.VideoFiles.forEach(file => {
341 tasks.push(instance.removeFile(file))
342 tasks.push(instance.removeTorrent(file))
346 return Promise.all(tasks)
348 logger.error('Some errors when removing files of video %s in after destroy hook.', instance.uuid, err)
353 return VideoModel.scope(ScopeNames.WITH_FILES).findAll()
356 static listAllAndSharedByActorForOutbox (actorId: number, start: number, count: number) {
357 function getRawQuery (select: string) {
358 const queryVideo = 'SELECT ' + select + ' FROM "video" AS "Video" ' +
359 'INNER JOIN "videoChannel" AS "VideoChannel" ON "VideoChannel"."id" = "Video"."channelId" ' +
360 'INNER JOIN "account" AS "Account" ON "Account"."id" = "VideoChannel"."accountId" ' +
361 'WHERE "Account"."actorId" = ' + actorId
362 const queryVideoShare = 'SELECT ' + select + ' FROM "videoShare" AS "VideoShare" ' +
363 'INNER JOIN "video" AS "Video" ON "Video"."id" = "VideoShare"."videoId" ' +
364 'WHERE "VideoShare"."actorId" = ' + actorId
366 return `(${queryVideo}) UNION (${queryVideoShare})`
369 const rawQuery = getRawQuery('"Video"."id"')
370 const rawCountQuery = getRawQuery('COUNT("Video"."id") as "total"')
376 order: [ getSort('createdAt'), [ 'Tags', 'name', 'ASC' ] ],
379 [Sequelize.Op.in]: Sequelize.literal('(' + rawQuery + ')')
384 model: VideoShareModel,
387 [Sequelize.Op.and]: [
390 [Sequelize.Op.not]: null
406 model: VideoChannelModel,
416 model: AccountVideoRateModel,
417 include: [ AccountModel ]
424 return Bluebird.all([
425 // FIXME: typing issue
426 VideoModel.findAll(query as any),
427 VideoModel.sequelize.query(rawCountQuery, { type: Sequelize.QueryTypes.SELECT })
428 ]).then(([ rows, totals ]) => {
429 // totals: totalVideos + totalVideoShares
431 let totalVideoShares = 0
432 if (totals[0]) totalVideos = parseInt(totals[0].total, 10)
433 if (totals[1]) totalVideoShares = parseInt(totals[1].total, 10)
435 const total = totalVideos + totalVideoShares
443 static listUserVideosForApi (userId: number, start: number, count: number, sort: string) {
447 order: [ getSort(sort) ],
450 model: VideoChannelModel,
465 return VideoModel.findAndCountAll(query).then(({ rows, count }) => {
473 static listForApi (start: number, count: number, sort: string) {
477 order: [ getSort(sort) ]
480 return VideoModel.scope([ ScopeNames.AVAILABLE_FOR_LIST, ScopeNames.WITH_ACCOUNT ])
481 .findAndCountAll(query)
482 .then(({ rows, count }) => {
490 static load (id: number) {
491 return VideoModel.findById(id)
494 static loadByUrl (url: string, t?: Sequelize.Transaction) {
495 const query: IFindOptions<VideoModel> = {
501 if (t !== undefined) query.transaction = t
503 return VideoModel.findOne(query)
506 static loadByUrlAndPopulateAccount (url: string, t?: Sequelize.Transaction) {
507 const query: IFindOptions<VideoModel> = {
513 if (t !== undefined) query.transaction = t
515 return VideoModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_FILES ]).findOne(query)
518 static loadByUUIDOrURL (uuid: string, url: string, t?: Sequelize.Transaction) {
519 const query: IFindOptions<VideoModel> = {
528 if (t !== undefined) query.transaction = t
530 return VideoModel.scope(ScopeNames.WITH_FILES).findOne(query)
533 static loadAndPopulateAccountAndServerAndTags (id: number) {
535 order: [ [ 'Tags', 'name', 'ASC' ] ]
539 .scope([ ScopeNames.WITH_RATES, ScopeNames.WITH_SHARES, ScopeNames.WITH_TAGS, ScopeNames.WITH_FILES, ScopeNames.WITH_ACCOUNT ])
540 .findById(id, options)
543 static loadByUUID (uuid: string) {
551 .scope([ ScopeNames.WITH_FILES ])
555 static loadByUUIDAndPopulateAccountAndServerAndTags (uuid: string) {
557 order: [ [ 'Tags', 'name', 'ASC' ] ],
564 .scope([ ScopeNames.WITH_RATES, ScopeNames.WITH_SHARES, ScopeNames.WITH_TAGS, ScopeNames.WITH_FILES, ScopeNames.WITH_ACCOUNT ])
568 static searchAndPopulateAccountAndServerAndTags (value: string, start: number, count: number, sort: string) {
569 const serverInclude: IIncludeOptions = {
574 const accountInclude: IIncludeOptions = {
580 include: [ serverInclude ]
585 const videoChannelInclude: IIncludeOptions = {
586 model: VideoChannelModel,
587 include: [ accountInclude ],
591 const tagInclude: IIncludeOptions = {
595 const query: IFindOptions<VideoModel> = {
596 distinct: true, // Because we have tags
599 order: [ getSort(sort) ],
603 // TODO: search on tags too
604 // const escapedValue = Video['sequelize'].escape('%' + value + '%')
605 // query.where['id'][Sequelize.Op.in] = Video['sequelize'].literal(
606 // `(SELECT "VideoTags"."videoId"
608 // INNER JOIN "VideoTags" ON "Tags"."id" = "VideoTags"."tagId"
609 // WHERE name ILIKE ${escapedValue}
613 // TODO: search on account too
614 // accountInclude.where = {
616 // [Sequelize.Op.iLike]: '%' + value + '%'
619 query.where['name'] = {
620 [Sequelize.Op.iLike]: '%' + value + '%'
624 videoChannelInclude, tagInclude
627 return VideoModel.scope([ ScopeNames.AVAILABLE_FOR_LIST ])
628 .findAndCountAll(query).then(({ rows, count }) => {
637 if (Array.isArray(this.VideoFiles) === false) return undefined
639 // The original file is the file that have the higher resolution
640 return maxBy(this.VideoFiles, file => file.resolution)
643 getVideoFilename (videoFile: VideoFileModel) {
644 return this.uuid + '-' + videoFile.resolution + videoFile.extname
647 getThumbnailName () {
648 // We always have a copy of the thumbnail
649 const extension = '.jpg'
650 return this.uuid + extension
654 const extension = '.jpg'
655 return this.uuid + extension
658 getTorrentFileName (videoFile: VideoFileModel) {
659 const extension = '.torrent'
660 return this.uuid + '-' + videoFile.resolution + extension
664 return this.remote === false
667 createPreview (videoFile: VideoFileModel) {
668 const imageSize = PREVIEWS_SIZE.width + 'x' + PREVIEWS_SIZE.height
670 return generateImageFromVideoFile(
671 this.getVideoFilePath(videoFile),
672 CONFIG.STORAGE.PREVIEWS_DIR,
673 this.getPreviewName(),
678 createThumbnail (videoFile: VideoFileModel) {
679 const imageSize = THUMBNAILS_SIZE.width + 'x' + THUMBNAILS_SIZE.height
681 return generateImageFromVideoFile(
682 this.getVideoFilePath(videoFile),
683 CONFIG.STORAGE.THUMBNAILS_DIR,
684 this.getThumbnailName(),
689 getVideoFilePath (videoFile: VideoFileModel) {
690 return join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
693 createTorrentAndSetInfoHash = async function (videoFile: VideoFileModel) {
696 [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ]
699 CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
703 const torrent = await createTorrentPromise(this.getVideoFilePath(videoFile), options)
705 const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
706 logger.info('Creating torrent %s.', filePath)
708 await writeFilePromise(filePath, torrent)
710 const parsedTorrent = parseTorrent(torrent)
711 videoFile.infoHash = parsedTorrent.infoHash
715 return '/videos/embed/' + this.uuid
718 getThumbnailPath () {
719 return join(STATIC_PATHS.THUMBNAILS, this.getThumbnailName())
723 return join(STATIC_PATHS.PREVIEWS, this.getPreviewName())
729 if (this.VideoChannel.Account.Actor.Server) {
730 serverHost = this.VideoChannel.Account.Actor.Server.host
732 // It means it's our video
733 serverHost = CONFIG.WEBSERVER.HOST
740 category: this.category,
741 categoryLabel: this.getCategoryLabel(),
742 licence: this.licence,
743 licenceLabel: this.getLicenceLabel(),
744 language: this.language,
745 languageLabel: this.getLanguageLabel(),
747 description: this.getTruncatedDescription(),
749 isLocal: this.isOwned(),
750 accountName: this.VideoChannel.Account.name,
751 duration: this.duration,
754 dislikes: this.dislikes,
755 thumbnailPath: this.getThumbnailPath(),
756 previewPath: this.getPreviewPath(),
757 embedPath: this.getEmbedPath(),
758 createdAt: this.createdAt,
759 updatedAt: this.updatedAt
763 toFormattedDetailsJSON () {
764 const formattedJson = this.toFormattedJSON()
766 // Maybe our server is not up to date and there are new privacy settings since our version
767 let privacyLabel = VIDEO_PRIVACIES[this.privacy]
768 if (!privacyLabel) privacyLabel = 'Unknown'
770 const detailsJson = {
772 privacy: this.privacy,
773 descriptionPath: this.getDescriptionPath(),
774 channel: this.VideoChannel.toFormattedJSON(),
775 account: this.VideoChannel.Account.toFormattedJSON(),
776 tags: map<TagModel, string>(this.Tags, 'name'),
780 // Format and sort video files
781 const { baseUrlHttp, baseUrlWs } = this.getBaseUrls()
782 detailsJson.files = this.VideoFiles
784 let resolutionLabel = videoFile.resolution + 'p'
787 resolution: videoFile.resolution,
789 magnetUri: this.generateMagnetUri(videoFile, baseUrlHttp, baseUrlWs),
790 size: videoFile.size,
791 torrentUrl: this.getTorrentUrl(videoFile, baseUrlHttp),
792 fileUrl: this.getVideoFileUrl(videoFile, baseUrlHttp)
796 if (a.resolution < b.resolution) return 1
797 if (a.resolution === b.resolution) return 0
801 return Object.assign(formattedJson, detailsJson) as VideoDetails
804 toActivityPubObject (): VideoTorrentObject {
805 const { baseUrlHttp, baseUrlWs } = this.getBaseUrls()
806 if (!this.Tags) this.Tags = []
808 const tag = this.Tags.map(t => ({
809 type: 'Hashtag' as 'Hashtag',
816 identifier: this.language + '',
817 name: this.getLanguageLabel()
824 identifier: this.category + '',
825 name: this.getCategoryLabel()
832 identifier: this.licence + '',
833 name: this.getLicenceLabel()
840 if (Array.isArray(this.AccountVideoRates)) {
841 const likes: string[] = []
842 const dislikes: string[] = []
844 for (const rate of this.AccountVideoRates) {
845 if (rate.type === 'like') {
846 likes.push(rate.Account.Actor.url)
847 } else if (rate.type === 'dislike') {
848 dislikes.push(rate.Account.Actor.url)
852 likesObject = activityPubCollection(likes)
853 dislikesObject = activityPubCollection(dislikes)
857 if (Array.isArray(this.VideoShares)) {
858 const shares: string[] = []
860 for (const videoShare of this.VideoShares) {
861 const shareUrl = getAnnounceActivityPubUrl(this.url, videoShare.Actor)
862 shares.push(shareUrl)
865 sharesObject = activityPubCollection(shares)
869 for (const file of this.VideoFiles) {
872 mimeType: 'video/' + file.extname.replace('.', ''),
873 url: this.getVideoFileUrl(file, baseUrlHttp),
874 width: file.resolution,
880 mimeType: 'application/x-bittorrent',
881 url: this.getTorrentUrl(file, baseUrlHttp),
882 width: file.resolution
887 mimeType: 'application/x-bittorrent;x-scheme-handler/magnet',
888 url: this.generateMagnetUri(file, baseUrlHttp, baseUrlWs),
889 width: file.resolution
896 mimeType: 'text/html',
897 url: CONFIG.WEBSERVER.URL + '/videos/watch/' + this.uuid
901 type: 'Video' as 'Video',
904 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
905 duration: 'PT' + this.duration + 'S',
913 published: this.createdAt.toISOString(),
914 updated: this.updatedAt.toISOString(),
915 mediaType: 'text/markdown',
916 content: this.getTruncatedDescription(),
919 url: this.getThumbnailUrl(baseUrlHttp),
920 mediaType: 'image/jpeg',
921 width: THUMBNAILS_SIZE.width,
922 height: THUMBNAILS_SIZE.height
926 dislikes: dislikesObject,
927 shares: sharesObject,
931 id: this.VideoChannel.Actor.url
937 getTruncatedDescription () {
938 if (!this.description) return null
941 length: CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max
944 return truncate(this.description, options)
947 optimizeOriginalVideofile = async function () {
948 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
949 const newExtname = '.mp4'
950 const inputVideoFile = this.getOriginalFile()
951 const videoInputPath = join(videosDirectory, this.getVideoFilename(inputVideoFile))
952 const videoOutputPath = join(videosDirectory, this.id + '-transcoded' + newExtname)
954 const transcodeOptions = {
955 inputPath: videoInputPath,
956 outputPath: videoOutputPath
960 // Could be very long!
961 await transcode(transcodeOptions)
963 await unlinkPromise(videoInputPath)
965 // Important to do this before getVideoFilename() to take in account the new file extension
966 inputVideoFile.set('extname', newExtname)
968 await renamePromise(videoOutputPath, this.getVideoFilePath(inputVideoFile))
969 const stats = await statPromise(this.getVideoFilePath(inputVideoFile))
971 inputVideoFile.set('size', stats.size)
973 await this.createTorrentAndSetInfoHash(inputVideoFile)
974 await inputVideoFile.save()
977 // Auto destruction...
978 this.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', err))
984 transcodeOriginalVideofile = async function (resolution: VideoResolution) {
985 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
986 const extname = '.mp4'
988 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
989 const videoInputPath = join(videosDirectory, this.getVideoFilename(this.getOriginalFile()))
991 const newVideoFile = new VideoFileModel({
997 const videoOutputPath = join(videosDirectory, this.getVideoFilename(newVideoFile))
999 const transcodeOptions = {
1000 inputPath: videoInputPath,
1001 outputPath: videoOutputPath,
1005 await transcode(transcodeOptions)
1007 const stats = await statPromise(videoOutputPath)
1009 newVideoFile.set('size', stats.size)
1011 await this.createTorrentAndSetInfoHash(newVideoFile)
1013 await newVideoFile.save()
1015 this.VideoFiles.push(newVideoFile)
1018 getOriginalFileHeight () {
1019 const originalFilePath = this.getVideoFilePath(this.getOriginalFile())
1021 return getVideoFileHeight(originalFilePath)
1024 getDescriptionPath () {
1025 return `/api/${API_VERSION}/videos/${this.uuid}/description`
1028 getCategoryLabel () {
1029 let categoryLabel = VIDEO_CATEGORIES[this.category]
1030 if (!categoryLabel) categoryLabel = 'Misc'
1032 return categoryLabel
1035 getLicenceLabel () {
1036 let licenceLabel = VIDEO_LICENCES[this.licence]
1037 if (!licenceLabel) licenceLabel = 'Unknown'
1042 getLanguageLabel () {
1043 let languageLabel = VIDEO_LANGUAGES[this.language]
1044 if (!languageLabel) languageLabel = 'Unknown'
1046 return languageLabel
1049 removeThumbnail () {
1050 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
1051 return unlinkPromise(thumbnailPath)
1055 // Same name than video thumbnail
1056 return unlinkPromise(CONFIG.STORAGE.PREVIEWS_DIR + this.getPreviewName())
1059 removeFile (videoFile: VideoFileModel) {
1060 const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
1061 return unlinkPromise(filePath)
1064 removeTorrent (videoFile: VideoFileModel) {
1065 const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
1066 return unlinkPromise(torrentPath)
1069 private getBaseUrls () {
1073 if (this.isOwned()) {
1074 baseUrlHttp = CONFIG.WEBSERVER.URL
1075 baseUrlWs = CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
1077 baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + this.VideoChannel.Account.Actor.Server.host
1078 baseUrlWs = REMOTE_SCHEME.WS + '://' + this.VideoChannel.Account.Actor.Server.host
1081 return { baseUrlHttp, baseUrlWs }
1084 private getThumbnailUrl (baseUrlHttp: string) {
1085 return baseUrlHttp + STATIC_PATHS.THUMBNAILS + this.getThumbnailName()
1088 private getTorrentUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1089 return baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentFileName(videoFile)
1092 private getVideoFileUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1093 return baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
1096 private generateMagnetUri (videoFile: VideoFileModel, baseUrlHttp: string, baseUrlWs: string) {
1097 const xs = this.getTorrentUrl(videoFile, baseUrlHttp)
1098 const announce = [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
1099 const urlList = [ this.getVideoFileUrl(videoFile, baseUrlHttp) ]
1101 const magnetHash = {
1105 infoHash: videoFile.infoHash,
1109 return magnetUtil.encode(magnetHash)