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 loadByUrlAndPopulateAccount (url: string, t?: Sequelize.Transaction) {
495 const query: IFindOptions<VideoModel> = {
501 if (t !== undefined) query.transaction = t
503 return VideoModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_FILES ]).findOne(query)
506 static loadByUUIDOrURL (uuid: string, url: string, t?: Sequelize.Transaction) {
507 const query: IFindOptions<VideoModel> = {
516 if (t !== undefined) query.transaction = t
518 return VideoModel.scope(ScopeNames.WITH_FILES).findOne(query)
521 static loadAndPopulateAccountAndServerAndTags (id: number) {
523 order: [ [ 'Tags', 'name', 'ASC' ] ]
527 .scope([ ScopeNames.WITH_RATES, ScopeNames.WITH_SHARES, ScopeNames.WITH_TAGS, ScopeNames.WITH_FILES, ScopeNames.WITH_ACCOUNT ])
528 .findById(id, options)
531 static loadByUUIDAndPopulateAccountAndServerAndTags (uuid: string) {
533 order: [ [ 'Tags', 'name', 'ASC' ] ],
540 .scope([ ScopeNames.WITH_RATES, ScopeNames.WITH_SHARES, ScopeNames.WITH_TAGS, ScopeNames.WITH_FILES, ScopeNames.WITH_ACCOUNT ])
544 static searchAndPopulateAccountAndServerAndTags (value: string, start: number, count: number, sort: string) {
545 const serverInclude: IIncludeOptions = {
550 const accountInclude: IIncludeOptions = {
556 include: [ serverInclude ]
561 const videoChannelInclude: IIncludeOptions = {
562 model: VideoChannelModel,
563 include: [ accountInclude ],
567 const tagInclude: IIncludeOptions = {
571 const query: IFindOptions<VideoModel> = {
572 distinct: true, // Because we have tags
575 order: [ getSort(sort) ],
579 // TODO: search on tags too
580 // const escapedValue = Video['sequelize'].escape('%' + value + '%')
581 // query.where['id'][Sequelize.Op.in] = Video['sequelize'].literal(
582 // `(SELECT "VideoTags"."videoId"
584 // INNER JOIN "VideoTags" ON "Tags"."id" = "VideoTags"."tagId"
585 // WHERE name ILIKE ${escapedValue}
589 // TODO: search on account too
590 // accountInclude.where = {
592 // [Sequelize.Op.iLike]: '%' + value + '%'
595 query.where['name'] = {
596 [Sequelize.Op.iLike]: '%' + value + '%'
600 videoChannelInclude, tagInclude
603 return VideoModel.scope([ ScopeNames.AVAILABLE_FOR_LIST ])
604 .findAndCountAll(query).then(({ rows, count }) => {
613 if (Array.isArray(this.VideoFiles) === false) return undefined
615 // The original file is the file that have the higher resolution
616 return maxBy(this.VideoFiles, file => file.resolution)
619 getVideoFilename (videoFile: VideoFileModel) {
620 return this.uuid + '-' + videoFile.resolution + videoFile.extname
623 getThumbnailName () {
624 // We always have a copy of the thumbnail
625 const extension = '.jpg'
626 return this.uuid + extension
630 const extension = '.jpg'
631 return this.uuid + extension
634 getTorrentFileName (videoFile: VideoFileModel) {
635 const extension = '.torrent'
636 return this.uuid + '-' + videoFile.resolution + extension
640 return this.remote === false
643 createPreview (videoFile: VideoFileModel) {
644 const imageSize = PREVIEWS_SIZE.width + 'x' + PREVIEWS_SIZE.height
646 return generateImageFromVideoFile(
647 this.getVideoFilePath(videoFile),
648 CONFIG.STORAGE.PREVIEWS_DIR,
649 this.getPreviewName(),
654 createThumbnail (videoFile: VideoFileModel) {
655 const imageSize = THUMBNAILS_SIZE.width + 'x' + THUMBNAILS_SIZE.height
657 return generateImageFromVideoFile(
658 this.getVideoFilePath(videoFile),
659 CONFIG.STORAGE.THUMBNAILS_DIR,
660 this.getThumbnailName(),
665 getVideoFilePath (videoFile: VideoFileModel) {
666 return join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
669 createTorrentAndSetInfoHash = async function (videoFile: VideoFileModel) {
672 [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ]
675 CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
679 const torrent = await createTorrentPromise(this.getVideoFilePath(videoFile), options)
681 const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
682 logger.info('Creating torrent %s.', filePath)
684 await writeFilePromise(filePath, torrent)
686 const parsedTorrent = parseTorrent(torrent)
687 videoFile.infoHash = parsedTorrent.infoHash
691 return '/videos/embed/' + this.uuid
694 getThumbnailPath () {
695 return join(STATIC_PATHS.THUMBNAILS, this.getThumbnailName())
699 return join(STATIC_PATHS.PREVIEWS, this.getPreviewName())
705 if (this.VideoChannel.Account.Actor.Server) {
706 serverHost = this.VideoChannel.Account.Actor.Server.host
708 // It means it's our video
709 serverHost = CONFIG.WEBSERVER.HOST
716 category: this.category,
717 categoryLabel: this.getCategoryLabel(),
718 licence: this.licence,
719 licenceLabel: this.getLicenceLabel(),
720 language: this.language,
721 languageLabel: this.getLanguageLabel(),
723 description: this.getTruncatedDescription(),
725 isLocal: this.isOwned(),
726 accountName: this.VideoChannel.Account.name,
727 duration: this.duration,
730 dislikes: this.dislikes,
731 thumbnailPath: this.getThumbnailPath(),
732 previewPath: this.getPreviewPath(),
733 embedPath: this.getEmbedPath(),
734 createdAt: this.createdAt,
735 updatedAt: this.updatedAt
739 toFormattedDetailsJSON () {
740 const formattedJson = this.toFormattedJSON()
742 // Maybe our server is not up to date and there are new privacy settings since our version
743 let privacyLabel = VIDEO_PRIVACIES[this.privacy]
744 if (!privacyLabel) privacyLabel = 'Unknown'
746 const detailsJson = {
748 privacy: this.privacy,
749 descriptionPath: this.getDescriptionPath(),
750 channel: this.VideoChannel.toFormattedJSON(),
751 account: this.VideoChannel.Account.toFormattedJSON(),
752 tags: map<TagModel, string>(this.Tags, 'name'),
756 // Format and sort video files
757 const { baseUrlHttp, baseUrlWs } = this.getBaseUrls()
758 detailsJson.files = this.VideoFiles
760 let resolutionLabel = videoFile.resolution + 'p'
763 resolution: videoFile.resolution,
765 magnetUri: this.generateMagnetUri(videoFile, baseUrlHttp, baseUrlWs),
766 size: videoFile.size,
767 torrentUrl: this.getTorrentUrl(videoFile, baseUrlHttp),
768 fileUrl: this.getVideoFileUrl(videoFile, baseUrlHttp)
772 if (a.resolution < b.resolution) return 1
773 if (a.resolution === b.resolution) return 0
777 return Object.assign(formattedJson, detailsJson) as VideoDetails
780 toActivityPubObject (): VideoTorrentObject {
781 const { baseUrlHttp, baseUrlWs } = this.getBaseUrls()
782 if (!this.Tags) this.Tags = []
784 const tag = this.Tags.map(t => ({
785 type: 'Hashtag' as 'Hashtag',
792 identifier: this.language + '',
793 name: this.getLanguageLabel()
800 identifier: this.category + '',
801 name: this.getCategoryLabel()
808 identifier: this.licence + '',
809 name: this.getLicenceLabel()
816 if (Array.isArray(this.AccountVideoRates)) {
817 const likes: string[] = []
818 const dislikes: string[] = []
820 for (const rate of this.AccountVideoRates) {
821 if (rate.type === 'like') {
822 likes.push(rate.Account.Actor.url)
823 } else if (rate.type === 'dislike') {
824 dislikes.push(rate.Account.Actor.url)
828 likesObject = activityPubCollection(likes)
829 dislikesObject = activityPubCollection(dislikes)
833 if (Array.isArray(this.VideoShares)) {
834 const shares: string[] = []
836 for (const videoShare of this.VideoShares) {
837 const shareUrl = getAnnounceActivityPubUrl(this.url, videoShare.Actor)
838 shares.push(shareUrl)
841 sharesObject = activityPubCollection(shares)
845 for (const file of this.VideoFiles) {
848 mimeType: 'video/' + file.extname.replace('.', ''),
849 url: this.getVideoFileUrl(file, baseUrlHttp),
850 width: file.resolution,
856 mimeType: 'application/x-bittorrent',
857 url: this.getTorrentUrl(file, baseUrlHttp),
858 width: file.resolution
863 mimeType: 'application/x-bittorrent;x-scheme-handler/magnet',
864 url: this.generateMagnetUri(file, baseUrlHttp, baseUrlWs),
865 width: file.resolution
872 mimeType: 'text/html',
873 url: CONFIG.WEBSERVER.URL + '/videos/watch/' + this.uuid
877 type: 'Video' as 'Video',
880 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
881 duration: 'PT' + this.duration + 'S',
889 published: this.createdAt.toISOString(),
890 updated: this.updatedAt.toISOString(),
891 mediaType: 'text/markdown',
892 content: this.getTruncatedDescription(),
895 url: this.getThumbnailUrl(baseUrlHttp),
896 mediaType: 'image/jpeg',
897 width: THUMBNAILS_SIZE.width,
898 height: THUMBNAILS_SIZE.height
902 dislikes: dislikesObject,
903 shares: sharesObject,
907 id: this.VideoChannel.Actor.url
913 getTruncatedDescription () {
914 if (!this.description) return null
917 length: CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max
920 return truncate(this.description, options)
923 optimizeOriginalVideofile = async function () {
924 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
925 const newExtname = '.mp4'
926 const inputVideoFile = this.getOriginalFile()
927 const videoInputPath = join(videosDirectory, this.getVideoFilename(inputVideoFile))
928 const videoOutputPath = join(videosDirectory, this.id + '-transcoded' + newExtname)
930 const transcodeOptions = {
931 inputPath: videoInputPath,
932 outputPath: videoOutputPath
936 // Could be very long!
937 await transcode(transcodeOptions)
939 await unlinkPromise(videoInputPath)
941 // Important to do this before getVideoFilename() to take in account the new file extension
942 inputVideoFile.set('extname', newExtname)
944 await renamePromise(videoOutputPath, this.getVideoFilePath(inputVideoFile))
945 const stats = await statPromise(this.getVideoFilePath(inputVideoFile))
947 inputVideoFile.set('size', stats.size)
949 await this.createTorrentAndSetInfoHash(inputVideoFile)
950 await inputVideoFile.save()
953 // Auto destruction...
954 this.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', err))
960 transcodeOriginalVideofile = async function (resolution: VideoResolution) {
961 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
962 const extname = '.mp4'
964 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
965 const videoInputPath = join(videosDirectory, this.getVideoFilename(this.getOriginalFile()))
967 const newVideoFile = new VideoFileModel({
973 const videoOutputPath = join(videosDirectory, this.getVideoFilename(newVideoFile))
975 const transcodeOptions = {
976 inputPath: videoInputPath,
977 outputPath: videoOutputPath,
981 await transcode(transcodeOptions)
983 const stats = await statPromise(videoOutputPath)
985 newVideoFile.set('size', stats.size)
987 await this.createTorrentAndSetInfoHash(newVideoFile)
989 await newVideoFile.save()
991 this.VideoFiles.push(newVideoFile)
994 getOriginalFileHeight () {
995 const originalFilePath = this.getVideoFilePath(this.getOriginalFile())
997 return getVideoFileHeight(originalFilePath)
1000 getDescriptionPath () {
1001 return `/api/${API_VERSION}/videos/${this.uuid}/description`
1004 getCategoryLabel () {
1005 let categoryLabel = VIDEO_CATEGORIES[this.category]
1006 if (!categoryLabel) categoryLabel = 'Misc'
1008 return categoryLabel
1011 getLicenceLabel () {
1012 let licenceLabel = VIDEO_LICENCES[this.licence]
1013 if (!licenceLabel) licenceLabel = 'Unknown'
1018 getLanguageLabel () {
1019 let languageLabel = VIDEO_LANGUAGES[this.language]
1020 if (!languageLabel) languageLabel = 'Unknown'
1022 return languageLabel
1025 removeThumbnail () {
1026 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
1027 return unlinkPromise(thumbnailPath)
1031 // Same name than video thumbnail
1032 return unlinkPromise(CONFIG.STORAGE.PREVIEWS_DIR + this.getPreviewName())
1035 removeFile (videoFile: VideoFileModel) {
1036 const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
1037 return unlinkPromise(filePath)
1040 removeTorrent (videoFile: VideoFileModel) {
1041 const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
1042 return unlinkPromise(torrentPath)
1045 private getBaseUrls () {
1049 if (this.isOwned()) {
1050 baseUrlHttp = CONFIG.WEBSERVER.URL
1051 baseUrlWs = CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
1053 baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + this.VideoChannel.Account.Actor.Server.host
1054 baseUrlWs = REMOTE_SCHEME.WS + '://' + this.VideoChannel.Account.Actor.Server.host
1057 return { baseUrlHttp, baseUrlWs }
1060 private getThumbnailUrl (baseUrlHttp: string) {
1061 return baseUrlHttp + STATIC_PATHS.THUMBNAILS + this.getThumbnailName()
1064 private getTorrentUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1065 return baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentFileName(videoFile)
1068 private getVideoFileUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1069 return baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
1072 private generateMagnetUri (videoFile: VideoFileModel, baseUrlHttp: string, baseUrlWs: string) {
1073 const xs = this.getTorrentUrl(videoFile, baseUrlHttp)
1074 const announce = [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
1075 const urlList = [ this.getVideoFileUrl(videoFile, baseUrlHttp) ]
1077 const magnetHash = {
1081 infoHash: videoFile.infoHash,
1085 return magnetUtil.encode(magnetHash)