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 loadByUUID (uuid: string) {
539 .scope([ ScopeNames.WITH_FILES ])
543 static loadByUUIDAndPopulateAccountAndServerAndTags (uuid: string) {
545 order: [ [ 'Tags', 'name', 'ASC' ] ],
552 .scope([ ScopeNames.WITH_RATES, ScopeNames.WITH_SHARES, ScopeNames.WITH_TAGS, ScopeNames.WITH_FILES, ScopeNames.WITH_ACCOUNT ])
556 static searchAndPopulateAccountAndServerAndTags (value: string, start: number, count: number, sort: string) {
557 const serverInclude: IIncludeOptions = {
562 const accountInclude: IIncludeOptions = {
568 include: [ serverInclude ]
573 const videoChannelInclude: IIncludeOptions = {
574 model: VideoChannelModel,
575 include: [ accountInclude ],
579 const tagInclude: IIncludeOptions = {
583 const query: IFindOptions<VideoModel> = {
584 distinct: true, // Because we have tags
587 order: [ getSort(sort) ],
591 // TODO: search on tags too
592 // const escapedValue = Video['sequelize'].escape('%' + value + '%')
593 // query.where['id'][Sequelize.Op.in] = Video['sequelize'].literal(
594 // `(SELECT "VideoTags"."videoId"
596 // INNER JOIN "VideoTags" ON "Tags"."id" = "VideoTags"."tagId"
597 // WHERE name ILIKE ${escapedValue}
601 // TODO: search on account too
602 // accountInclude.where = {
604 // [Sequelize.Op.iLike]: '%' + value + '%'
607 query.where['name'] = {
608 [Sequelize.Op.iLike]: '%' + value + '%'
612 videoChannelInclude, tagInclude
615 return VideoModel.scope([ ScopeNames.AVAILABLE_FOR_LIST ])
616 .findAndCountAll(query).then(({ rows, count }) => {
625 if (Array.isArray(this.VideoFiles) === false) return undefined
627 // The original file is the file that have the higher resolution
628 return maxBy(this.VideoFiles, file => file.resolution)
631 getVideoFilename (videoFile: VideoFileModel) {
632 return this.uuid + '-' + videoFile.resolution + videoFile.extname
635 getThumbnailName () {
636 // We always have a copy of the thumbnail
637 const extension = '.jpg'
638 return this.uuid + extension
642 const extension = '.jpg'
643 return this.uuid + extension
646 getTorrentFileName (videoFile: VideoFileModel) {
647 const extension = '.torrent'
648 return this.uuid + '-' + videoFile.resolution + extension
652 return this.remote === false
655 createPreview (videoFile: VideoFileModel) {
656 const imageSize = PREVIEWS_SIZE.width + 'x' + PREVIEWS_SIZE.height
658 return generateImageFromVideoFile(
659 this.getVideoFilePath(videoFile),
660 CONFIG.STORAGE.PREVIEWS_DIR,
661 this.getPreviewName(),
666 createThumbnail (videoFile: VideoFileModel) {
667 const imageSize = THUMBNAILS_SIZE.width + 'x' + THUMBNAILS_SIZE.height
669 return generateImageFromVideoFile(
670 this.getVideoFilePath(videoFile),
671 CONFIG.STORAGE.THUMBNAILS_DIR,
672 this.getThumbnailName(),
677 getVideoFilePath (videoFile: VideoFileModel) {
678 return join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
681 createTorrentAndSetInfoHash = async function (videoFile: VideoFileModel) {
684 [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ]
687 CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
691 const torrent = await createTorrentPromise(this.getVideoFilePath(videoFile), options)
693 const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
694 logger.info('Creating torrent %s.', filePath)
696 await writeFilePromise(filePath, torrent)
698 const parsedTorrent = parseTorrent(torrent)
699 videoFile.infoHash = parsedTorrent.infoHash
703 return '/videos/embed/' + this.uuid
706 getThumbnailPath () {
707 return join(STATIC_PATHS.THUMBNAILS, this.getThumbnailName())
711 return join(STATIC_PATHS.PREVIEWS, this.getPreviewName())
717 if (this.VideoChannel.Account.Actor.Server) {
718 serverHost = this.VideoChannel.Account.Actor.Server.host
720 // It means it's our video
721 serverHost = CONFIG.WEBSERVER.HOST
728 category: this.category,
729 categoryLabel: this.getCategoryLabel(),
730 licence: this.licence,
731 licenceLabel: this.getLicenceLabel(),
732 language: this.language,
733 languageLabel: this.getLanguageLabel(),
735 description: this.getTruncatedDescription(),
737 isLocal: this.isOwned(),
738 accountName: this.VideoChannel.Account.name,
739 duration: this.duration,
742 dislikes: this.dislikes,
743 thumbnailPath: this.getThumbnailPath(),
744 previewPath: this.getPreviewPath(),
745 embedPath: this.getEmbedPath(),
746 createdAt: this.createdAt,
747 updatedAt: this.updatedAt
751 toFormattedDetailsJSON () {
752 const formattedJson = this.toFormattedJSON()
754 // Maybe our server is not up to date and there are new privacy settings since our version
755 let privacyLabel = VIDEO_PRIVACIES[this.privacy]
756 if (!privacyLabel) privacyLabel = 'Unknown'
758 const detailsJson = {
760 privacy: this.privacy,
761 descriptionPath: this.getDescriptionPath(),
762 channel: this.VideoChannel.toFormattedJSON(),
763 account: this.VideoChannel.Account.toFormattedJSON(),
764 tags: map<TagModel, string>(this.Tags, 'name'),
768 // Format and sort video files
769 const { baseUrlHttp, baseUrlWs } = this.getBaseUrls()
770 detailsJson.files = this.VideoFiles
772 let resolutionLabel = videoFile.resolution + 'p'
775 resolution: videoFile.resolution,
777 magnetUri: this.generateMagnetUri(videoFile, baseUrlHttp, baseUrlWs),
778 size: videoFile.size,
779 torrentUrl: this.getTorrentUrl(videoFile, baseUrlHttp),
780 fileUrl: this.getVideoFileUrl(videoFile, baseUrlHttp)
784 if (a.resolution < b.resolution) return 1
785 if (a.resolution === b.resolution) return 0
789 return Object.assign(formattedJson, detailsJson) as VideoDetails
792 toActivityPubObject (): VideoTorrentObject {
793 const { baseUrlHttp, baseUrlWs } = this.getBaseUrls()
794 if (!this.Tags) this.Tags = []
796 const tag = this.Tags.map(t => ({
797 type: 'Hashtag' as 'Hashtag',
804 identifier: this.language + '',
805 name: this.getLanguageLabel()
812 identifier: this.category + '',
813 name: this.getCategoryLabel()
820 identifier: this.licence + '',
821 name: this.getLicenceLabel()
828 if (Array.isArray(this.AccountVideoRates)) {
829 const likes: string[] = []
830 const dislikes: string[] = []
832 for (const rate of this.AccountVideoRates) {
833 if (rate.type === 'like') {
834 likes.push(rate.Account.Actor.url)
835 } else if (rate.type === 'dislike') {
836 dislikes.push(rate.Account.Actor.url)
840 likesObject = activityPubCollection(likes)
841 dislikesObject = activityPubCollection(dislikes)
845 if (Array.isArray(this.VideoShares)) {
846 const shares: string[] = []
848 for (const videoShare of this.VideoShares) {
849 const shareUrl = getAnnounceActivityPubUrl(this.url, videoShare.Actor)
850 shares.push(shareUrl)
853 sharesObject = activityPubCollection(shares)
857 for (const file of this.VideoFiles) {
860 mimeType: 'video/' + file.extname.replace('.', ''),
861 url: this.getVideoFileUrl(file, baseUrlHttp),
862 width: file.resolution,
868 mimeType: 'application/x-bittorrent',
869 url: this.getTorrentUrl(file, baseUrlHttp),
870 width: file.resolution
875 mimeType: 'application/x-bittorrent;x-scheme-handler/magnet',
876 url: this.generateMagnetUri(file, baseUrlHttp, baseUrlWs),
877 width: file.resolution
884 mimeType: 'text/html',
885 url: CONFIG.WEBSERVER.URL + '/videos/watch/' + this.uuid
889 type: 'Video' as 'Video',
892 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
893 duration: 'PT' + this.duration + 'S',
901 published: this.createdAt.toISOString(),
902 updated: this.updatedAt.toISOString(),
903 mediaType: 'text/markdown',
904 content: this.getTruncatedDescription(),
907 url: this.getThumbnailUrl(baseUrlHttp),
908 mediaType: 'image/jpeg',
909 width: THUMBNAILS_SIZE.width,
910 height: THUMBNAILS_SIZE.height
914 dislikes: dislikesObject,
915 shares: sharesObject,
919 id: this.VideoChannel.Actor.url
925 getTruncatedDescription () {
926 if (!this.description) return null
929 length: CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max
932 return truncate(this.description, options)
935 optimizeOriginalVideofile = async function () {
936 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
937 const newExtname = '.mp4'
938 const inputVideoFile = this.getOriginalFile()
939 const videoInputPath = join(videosDirectory, this.getVideoFilename(inputVideoFile))
940 const videoOutputPath = join(videosDirectory, this.id + '-transcoded' + newExtname)
942 const transcodeOptions = {
943 inputPath: videoInputPath,
944 outputPath: videoOutputPath
948 // Could be very long!
949 await transcode(transcodeOptions)
951 await unlinkPromise(videoInputPath)
953 // Important to do this before getVideoFilename() to take in account the new file extension
954 inputVideoFile.set('extname', newExtname)
956 await renamePromise(videoOutputPath, this.getVideoFilePath(inputVideoFile))
957 const stats = await statPromise(this.getVideoFilePath(inputVideoFile))
959 inputVideoFile.set('size', stats.size)
961 await this.createTorrentAndSetInfoHash(inputVideoFile)
962 await inputVideoFile.save()
965 // Auto destruction...
966 this.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', err))
972 transcodeOriginalVideofile = async function (resolution: VideoResolution) {
973 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
974 const extname = '.mp4'
976 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
977 const videoInputPath = join(videosDirectory, this.getVideoFilename(this.getOriginalFile()))
979 const newVideoFile = new VideoFileModel({
985 const videoOutputPath = join(videosDirectory, this.getVideoFilename(newVideoFile))
987 const transcodeOptions = {
988 inputPath: videoInputPath,
989 outputPath: videoOutputPath,
993 await transcode(transcodeOptions)
995 const stats = await statPromise(videoOutputPath)
997 newVideoFile.set('size', stats.size)
999 await this.createTorrentAndSetInfoHash(newVideoFile)
1001 await newVideoFile.save()
1003 this.VideoFiles.push(newVideoFile)
1006 getOriginalFileHeight () {
1007 const originalFilePath = this.getVideoFilePath(this.getOriginalFile())
1009 return getVideoFileHeight(originalFilePath)
1012 getDescriptionPath () {
1013 return `/api/${API_VERSION}/videos/${this.uuid}/description`
1016 getCategoryLabel () {
1017 let categoryLabel = VIDEO_CATEGORIES[this.category]
1018 if (!categoryLabel) categoryLabel = 'Misc'
1020 return categoryLabel
1023 getLicenceLabel () {
1024 let licenceLabel = VIDEO_LICENCES[this.licence]
1025 if (!licenceLabel) licenceLabel = 'Unknown'
1030 getLanguageLabel () {
1031 let languageLabel = VIDEO_LANGUAGES[this.language]
1032 if (!languageLabel) languageLabel = 'Unknown'
1034 return languageLabel
1037 removeThumbnail () {
1038 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
1039 return unlinkPromise(thumbnailPath)
1043 // Same name than video thumbnail
1044 return unlinkPromise(CONFIG.STORAGE.PREVIEWS_DIR + this.getPreviewName())
1047 removeFile (videoFile: VideoFileModel) {
1048 const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
1049 return unlinkPromise(filePath)
1052 removeTorrent (videoFile: VideoFileModel) {
1053 const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
1054 return unlinkPromise(torrentPath)
1057 private getBaseUrls () {
1061 if (this.isOwned()) {
1062 baseUrlHttp = CONFIG.WEBSERVER.URL
1063 baseUrlWs = CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
1065 baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + this.VideoChannel.Account.Actor.Server.host
1066 baseUrlWs = REMOTE_SCHEME.WS + '://' + this.VideoChannel.Account.Actor.Server.host
1069 return { baseUrlHttp, baseUrlWs }
1072 private getThumbnailUrl (baseUrlHttp: string) {
1073 return baseUrlHttp + STATIC_PATHS.THUMBNAILS + this.getThumbnailName()
1076 private getTorrentUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1077 return baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentFileName(videoFile)
1080 private getVideoFileUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1081 return baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
1084 private generateMagnetUri (videoFile: VideoFileModel, baseUrlHttp: string, baseUrlWs: string) {
1085 const xs = this.getTorrentUrl(videoFile, baseUrlHttp)
1086 const announce = [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
1087 const urlList = [ this.getVideoFileUrl(videoFile, baseUrlHttp) ]
1089 const magnetHash = {
1093 infoHash: videoFile.infoHash,
1097 return magnetUtil.encode(magnetHash)