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/index'
70 import { AccountModel } from '../account/account'
71 import { AccountVideoRateModel } from '../account/account-video-rate'
72 import { ServerModel } from '../server/server'
73 import { getSort, throwIfNotValid } from '../utils'
74 import { TagModel } from './tag'
75 import { VideoAbuseModel } from './video-abuse'
76 import { VideoChannelModel } from './video-channel'
77 import { VideoFileModel } from './video-file'
78 import { VideoShareModel } from './video-share'
79 import { VideoTagModel } from './video-tag'
82 NOT_IN_BLACKLIST = 'NOT_IN_BLACKLIST',
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.NOT_IN_BLACKLIST]: {
95 [Sequelize.Op.notIn]: Sequelize.literal(
96 '(SELECT "videoBlacklist"."videoId" FROM "videoBlacklist")'
101 [ScopeNames.PUBLIC]: {
103 privacy: VideoPrivacy.PUBLIC
106 [ScopeNames.WITH_ACCOUNT]: {
109 model: () => VideoChannelModel,
113 model: () => AccountModel,
117 model: () => ServerModel,
126 [ScopeNames.WITH_TAGS]: {
127 include: [ () => TagModel ]
129 [ScopeNames.WITH_FILES]: {
132 model: () => VideoFileModel,
137 [ScopeNames.WITH_SHARES]: {
140 model: () => VideoShareModel,
141 include: [ () => AccountModel ]
145 [ScopeNames.WITH_RATES]: {
148 model: () => AccountVideoRateModel,
149 include: [ () => AccountModel ]
161 fields: [ 'createdAt' ]
164 fields: [ 'duration' ]
176 fields: [ 'channelId' ]
180 export class VideoModel extends Model<VideoModel> {
183 @Default(DataType.UUIDV4)
185 @Column(DataType.UUID)
189 @Is('VideoName', value => throwIfNotValid(value, isVideoNameValid, 'name'))
195 @Is('VideoCategory', value => throwIfNotValid(value, isVideoCategoryValid, 'category'))
201 @Is('VideoLicence', value => throwIfNotValid(value, isVideoLicenceValid, 'licence'))
207 @Is('VideoLanguage', value => throwIfNotValid(value, isVideoLanguageValid, 'language'))
212 @Is('VideoPrivacy', value => throwIfNotValid(value, isVideoPrivacyValid, 'privacy'))
217 @Is('VideoNSFW', value => throwIfNotValid(value, isVideoNSFWValid, 'NSFW boolean'))
223 @Is('VideoDescription', value => throwIfNotValid(value, isVideoDescriptionValid, 'description'))
224 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max))
228 @Is('VideoDuration', value => throwIfNotValid(value, isVideoDurationValid, 'duration'))
258 @Is('VideoUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
259 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
268 @ForeignKey(() => VideoChannelModel)
272 @BelongsTo(() => VideoChannelModel, {
278 VideoChannel: VideoChannelModel
280 @BelongsToMany(() => TagModel, {
281 foreignKey: 'videoId',
282 through: () => VideoTagModel,
287 @HasMany(() => VideoAbuseModel, {
294 VideoAbuses: VideoAbuseModel[]
296 @HasMany(() => VideoFileModel, {
303 VideoFiles: VideoFileModel[]
305 @HasMany(() => VideoShareModel, {
312 VideoShares: VideoShareModel[]
314 @HasMany(() => AccountVideoRateModel, {
321 AccountVideoRates: AccountVideoRateModel[]
324 static removeFilesAndSendDelete (instance: VideoModel) {
328 instance.removeThumbnail()
331 if (instance.isOwned()) {
333 instance.removePreview(),
334 sendDeleteVideo(instance, undefined)
337 // Remove physical files and torrents
338 instance.VideoFiles.forEach(file => {
339 tasks.push(instance.removeFile(file))
340 tasks.push(instance.removeTorrent(file))
344 return Promise.all(tasks)
346 logger.error('Some errors when removing files of video %s in after destroy hook.', instance.uuid, err)
351 return VideoModel.scope(ScopeNames.WITH_FILES).findAll()
354 static listAllAndSharedByAccountForOutbox (accountId: number, start: number, count: number) {
355 function getRawQuery (select: string) {
356 const queryVideo = 'SELECT ' + select + ' FROM "video" AS "Video" ' +
357 'INNER JOIN "videoChannel" AS "VideoChannel" ON "VideoChannel"."id" = "Video"."channelId" ' +
358 'WHERE "VideoChannel"."accountId" = ' + accountId
359 const queryVideoShare = 'SELECT ' + select + ' FROM "videoShare" AS "VideoShare" ' +
360 'INNER JOIN "video" AS "Video" ON "Video"."id" = "VideoShare"."videoId" ' +
361 'WHERE "VideoShare"."accountId" = ' + accountId
363 return `(${queryVideo}) UNION (${queryVideoShare})`
366 const rawQuery = getRawQuery('"Video"."id"')
367 const rawCountQuery = getRawQuery('COUNT("Video"."id") as "total"')
373 order: [ getSort('createdAt'), [ 'Tags', 'name', 'ASC' ] ],
376 [Sequelize.Op.in]: Sequelize.literal('(' + rawQuery + ')')
381 model: VideoShareModel,
384 [Sequelize.Op.and]: [
387 [Sequelize.Op.not]: null
395 include: [ AccountModel ]
398 model: VideoChannelModel,
408 model: AccountVideoRateModel,
409 include: [ AccountModel ]
416 return Bluebird.all([
417 // FIXME: typing issue
418 VideoModel.findAll(query as any),
419 VideoModel.sequelize.query(rawCountQuery, { type: Sequelize.QueryTypes.SELECT })
420 ]).then(([ rows, totals ]) => {
421 // totals: totalVideos + totalVideoShares
423 let totalVideoShares = 0
424 if (totals[0]) totalVideos = parseInt(totals[0].total, 10)
425 if (totals[1]) totalVideoShares = parseInt(totals[1].total, 10)
427 const total = totalVideos + totalVideoShares
435 static listUserVideosForApi (userId: number, start: number, count: number, sort: string) {
439 order: [ getSort(sort) ],
442 model: VideoChannelModel,
457 return VideoModel.findAndCountAll(query).then(({ rows, count }) => {
465 static listForApi (start: number, count: number, sort: string) {
469 order: [ getSort(sort) ]
472 return VideoModel.scope([ ScopeNames.NOT_IN_BLACKLIST, ScopeNames.PUBLIC, ScopeNames.WITH_ACCOUNT ])
473 .findAndCountAll(query)
474 .then(({ rows, count }) => {
482 static load (id: number) {
483 return VideoModel.findById(id)
486 static loadByUrlAndPopulateAccount (url: string, t?: Sequelize.Transaction) {
487 const query: IFindOptions<VideoModel> = {
493 if (t !== undefined) query.transaction = t
495 return VideoModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_FILES ]).findOne(query)
498 static loadByUUIDOrURL (uuid: string, url: string, t?: Sequelize.Transaction) {
499 const query: IFindOptions<VideoModel> = {
508 if (t !== undefined) query.transaction = t
510 return VideoModel.scope(ScopeNames.WITH_FILES).findOne(query)
513 static loadAndPopulateAccountAndServerAndTags (id: number) {
515 order: [ [ 'Tags', 'name', 'ASC' ] ]
519 .scope([ ScopeNames.WITH_RATES, ScopeNames.WITH_SHARES, ScopeNames.WITH_TAGS, ScopeNames.WITH_FILES, ScopeNames.WITH_ACCOUNT ])
520 .findById(id, options)
523 static loadByUUIDAndPopulateAccountAndServerAndTags (uuid: string) {
525 order: [ [ 'Tags', 'name', 'ASC' ] ],
532 .scope([ ScopeNames.WITH_RATES, ScopeNames.WITH_SHARES, ScopeNames.WITH_TAGS, ScopeNames.WITH_FILES, ScopeNames.WITH_ACCOUNT ])
536 static searchAndPopulateAccountAndServerAndTags (value: string, start: number, count: number, sort: string) {
537 const serverInclude: IIncludeOptions = {
542 const accountInclude: IIncludeOptions = {
544 include: [ serverInclude ]
547 const videoChannelInclude: IIncludeOptions = {
548 model: VideoChannelModel,
549 include: [ accountInclude ],
553 const tagInclude: IIncludeOptions = {
557 const query: IFindOptions<VideoModel> = {
558 distinct: true, // Because we have tags
561 order: [ getSort(sort) ],
565 // TODO: search on tags too
566 // const escapedValue = Video['sequelize'].escape('%' + value + '%')
567 // query.where['id'][Sequelize.Op.in] = Video['sequelize'].literal(
568 // `(SELECT "VideoTags"."videoId"
570 // INNER JOIN "VideoTags" ON "Tags"."id" = "VideoTags"."tagId"
571 // WHERE name ILIKE ${escapedValue}
575 // TODO: search on account too
576 // accountInclude.where = {
578 // [Sequelize.Op.iLike]: '%' + value + '%'
581 query.where['name'] = {
582 [Sequelize.Op.iLike]: '%' + value + '%'
586 videoChannelInclude, tagInclude
589 return VideoModel.scope([ ScopeNames.NOT_IN_BLACKLIST, ScopeNames.PUBLIC ])
590 .findAndCountAll(query).then(({ rows, count }) => {
599 if (Array.isArray(this.VideoFiles) === false) return undefined
601 // The original file is the file that have the higher resolution
602 return maxBy(this.VideoFiles, file => file.resolution)
605 getVideoFilename (videoFile: VideoFileModel) {
606 return this.uuid + '-' + videoFile.resolution + videoFile.extname
609 getThumbnailName () {
610 // We always have a copy of the thumbnail
611 const extension = '.jpg'
612 return this.uuid + extension
616 const extension = '.jpg'
617 return this.uuid + extension
620 getTorrentFileName (videoFile: VideoFileModel) {
621 const extension = '.torrent'
622 return this.uuid + '-' + videoFile.resolution + extension
626 return this.remote === false
629 createPreview (videoFile: VideoFileModel) {
630 const imageSize = PREVIEWS_SIZE.width + 'x' + PREVIEWS_SIZE.height
632 return generateImageFromVideoFile(
633 this.getVideoFilePath(videoFile),
634 CONFIG.STORAGE.PREVIEWS_DIR,
635 this.getPreviewName(),
640 createThumbnail (videoFile: VideoFileModel) {
641 const imageSize = THUMBNAILS_SIZE.width + 'x' + THUMBNAILS_SIZE.height
643 return generateImageFromVideoFile(
644 this.getVideoFilePath(videoFile),
645 CONFIG.STORAGE.THUMBNAILS_DIR,
646 this.getThumbnailName(),
651 getVideoFilePath (videoFile: VideoFileModel) {
652 return join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
655 createTorrentAndSetInfoHash = async function (videoFile: VideoFileModel) {
658 [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ]
661 CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
665 const torrent = await createTorrentPromise(this.getVideoFilePath(videoFile), options)
667 const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
668 logger.info('Creating torrent %s.', filePath)
670 await writeFilePromise(filePath, torrent)
672 const parsedTorrent = parseTorrent(torrent)
673 videoFile.infoHash = parsedTorrent.infoHash
677 return '/videos/embed/' + this.uuid
680 getThumbnailPath () {
681 return join(STATIC_PATHS.THUMBNAILS, this.getThumbnailName())
685 return join(STATIC_PATHS.PREVIEWS, this.getPreviewName())
691 if (this.VideoChannel.Account.Server) {
692 serverHost = this.VideoChannel.Account.Server.host
694 // It means it's our video
695 serverHost = CONFIG.WEBSERVER.HOST
702 category: this.category,
703 categoryLabel: this.getCategoryLabel(),
704 licence: this.licence,
705 licenceLabel: this.getLicenceLabel(),
706 language: this.language,
707 languageLabel: this.getLanguageLabel(),
709 description: this.getTruncatedDescription(),
711 isLocal: this.isOwned(),
712 accountName: this.VideoChannel.Account.name,
713 duration: this.duration,
716 dislikes: this.dislikes,
717 thumbnailPath: this.getThumbnailPath(),
718 previewPath: this.getPreviewPath(),
719 embedPath: this.getEmbedPath(),
720 createdAt: this.createdAt,
721 updatedAt: this.updatedAt
725 toFormattedDetailsJSON () {
726 const formattedJson = this.toFormattedJSON()
728 // Maybe our server is not up to date and there are new privacy settings since our version
729 let privacyLabel = VIDEO_PRIVACIES[this.privacy]
730 if (!privacyLabel) privacyLabel = 'Unknown'
732 const detailsJson = {
734 privacy: this.privacy,
735 descriptionPath: this.getDescriptionPath(),
736 channel: this.VideoChannel.toFormattedJSON(),
737 account: this.VideoChannel.Account.toFormattedJSON(),
738 tags: map<TagModel, string>(this.Tags, 'name'),
742 // Format and sort video files
743 const { baseUrlHttp, baseUrlWs } = this.getBaseUrls()
744 detailsJson.files = this.VideoFiles
746 let resolutionLabel = videoFile.resolution + 'p'
749 resolution: videoFile.resolution,
751 magnetUri: this.generateMagnetUri(videoFile, baseUrlHttp, baseUrlWs),
752 size: videoFile.size,
753 torrentUrl: this.getTorrentUrl(videoFile, baseUrlHttp),
754 fileUrl: this.getVideoFileUrl(videoFile, baseUrlHttp)
758 if (a.resolution < b.resolution) return 1
759 if (a.resolution === b.resolution) return 0
763 return Object.assign(formattedJson, detailsJson) as VideoDetails
766 toActivityPubObject (): VideoTorrentObject {
767 const { baseUrlHttp, baseUrlWs } = this.getBaseUrls()
768 if (!this.Tags) this.Tags = []
770 const tag = this.Tags.map(t => ({
771 type: 'Hashtag' as 'Hashtag',
778 identifier: this.language + '',
779 name: this.getLanguageLabel()
786 identifier: this.category + '',
787 name: this.getCategoryLabel()
794 identifier: this.licence + '',
795 name: this.getLicenceLabel()
802 if (Array.isArray(this.AccountVideoRates)) {
803 const likes: string[] = []
804 const dislikes: string[] = []
806 for (const rate of this.AccountVideoRates) {
807 if (rate.type === 'like') {
808 likes.push(rate.Account.url)
809 } else if (rate.type === 'dislike') {
810 dislikes.push(rate.Account.url)
814 likesObject = activityPubCollection(likes)
815 dislikesObject = activityPubCollection(dislikes)
819 if (Array.isArray(this.VideoShares)) {
820 const shares: string[] = []
822 for (const videoShare of this.VideoShares) {
823 const shareUrl = getAnnounceActivityPubUrl(this.url, videoShare.Account)
824 shares.push(shareUrl)
827 sharesObject = activityPubCollection(shares)
831 for (const file of this.VideoFiles) {
834 mimeType: 'video/' + file.extname.replace('.', ''),
835 url: this.getVideoFileUrl(file, baseUrlHttp),
836 width: file.resolution,
842 mimeType: 'application/x-bittorrent',
843 url: this.getTorrentUrl(file, baseUrlHttp),
844 width: file.resolution
849 mimeType: 'application/x-bittorrent;x-scheme-handler/magnet',
850 url: this.generateMagnetUri(file, baseUrlHttp, baseUrlWs),
851 width: file.resolution
858 mimeType: 'text/html',
859 url: CONFIG.WEBSERVER.URL + '/videos/watch/' + this.uuid
863 type: 'Video' as 'Video',
866 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
867 duration: 'PT' + this.duration + 'S',
875 published: this.createdAt.toISOString(),
876 updated: this.updatedAt.toISOString(),
877 mediaType: 'text/markdown',
878 content: this.getTruncatedDescription(),
881 url: this.getThumbnailUrl(baseUrlHttp),
882 mediaType: 'image/jpeg',
883 width: THUMBNAILS_SIZE.width,
884 height: THUMBNAILS_SIZE.height
888 dislikes: dislikesObject,
893 getTruncatedDescription () {
894 if (!this.description) return null
897 length: CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max
900 return truncate(this.description, options)
903 optimizeOriginalVideofile = async function () {
904 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
905 const newExtname = '.mp4'
906 const inputVideoFile = this.getOriginalFile()
907 const videoInputPath = join(videosDirectory, this.getVideoFilename(inputVideoFile))
908 const videoOutputPath = join(videosDirectory, this.id + '-transcoded' + newExtname)
910 const transcodeOptions = {
911 inputPath: videoInputPath,
912 outputPath: videoOutputPath
916 // Could be very long!
917 await transcode(transcodeOptions)
919 await unlinkPromise(videoInputPath)
921 // Important to do this before getVideoFilename() to take in account the new file extension
922 inputVideoFile.set('extname', newExtname)
924 await renamePromise(videoOutputPath, this.getVideoFilePath(inputVideoFile))
925 const stats = await statPromise(this.getVideoFilePath(inputVideoFile))
927 inputVideoFile.set('size', stats.size)
929 await this.createTorrentAndSetInfoHash(inputVideoFile)
930 await inputVideoFile.save()
933 // Auto destruction...
934 this.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', err))
940 transcodeOriginalVideofile = async function (resolution: VideoResolution) {
941 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
942 const extname = '.mp4'
944 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
945 const videoInputPath = join(videosDirectory, this.getVideoFilename(this.getOriginalFile()))
947 const newVideoFile = new VideoFileModel({
953 const videoOutputPath = join(videosDirectory, this.getVideoFilename(newVideoFile))
955 const transcodeOptions = {
956 inputPath: videoInputPath,
957 outputPath: videoOutputPath,
961 await transcode(transcodeOptions)
963 const stats = await statPromise(videoOutputPath)
965 newVideoFile.set('size', stats.size)
967 await this.createTorrentAndSetInfoHash(newVideoFile)
969 await newVideoFile.save()
971 this.VideoFiles.push(newVideoFile)
974 getOriginalFileHeight () {
975 const originalFilePath = this.getVideoFilePath(this.getOriginalFile())
977 return getVideoFileHeight(originalFilePath)
980 getDescriptionPath () {
981 return `/api/${API_VERSION}/videos/${this.uuid}/description`
984 getCategoryLabel () {
985 let categoryLabel = VIDEO_CATEGORIES[this.category]
986 if (!categoryLabel) categoryLabel = 'Misc'
992 let licenceLabel = VIDEO_LICENCES[this.licence]
993 if (!licenceLabel) licenceLabel = 'Unknown'
998 getLanguageLabel () {
999 let languageLabel = VIDEO_LANGUAGES[this.language]
1000 if (!languageLabel) languageLabel = 'Unknown'
1002 return languageLabel
1005 removeThumbnail () {
1006 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
1007 return unlinkPromise(thumbnailPath)
1011 // Same name than video thumbnail
1012 return unlinkPromise(CONFIG.STORAGE.PREVIEWS_DIR + this.getPreviewName())
1015 removeFile (videoFile: VideoFileModel) {
1016 const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
1017 return unlinkPromise(filePath)
1020 removeTorrent (videoFile: VideoFileModel) {
1021 const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
1022 return unlinkPromise(torrentPath)
1025 private getBaseUrls () {
1029 if (this.isOwned()) {
1030 baseUrlHttp = CONFIG.WEBSERVER.URL
1031 baseUrlWs = CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
1033 baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + this.VideoChannel.Account.Server.host
1034 baseUrlWs = REMOTE_SCHEME.WS + '://' + this.VideoChannel.Account.Server.host
1037 return { baseUrlHttp, baseUrlWs }
1040 private getThumbnailUrl (baseUrlHttp: string) {
1041 return baseUrlHttp + STATIC_PATHS.THUMBNAILS + this.getThumbnailName()
1044 private getTorrentUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1045 return baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentFileName(videoFile)
1048 private getVideoFileUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1049 return baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
1052 private generateMagnetUri (videoFile: VideoFileModel, baseUrlHttp: string, baseUrlWs: string) {
1053 const xs = this.getTorrentUrl(videoFile, baseUrlHttp)
1054 const announce = [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
1055 const urlList = [ this.getVideoFileUrl(videoFile, baseUrlHttp) ]
1057 const magnetHash = {
1061 infoHash: videoFile.infoHash,
1065 return magnetUtil.encode(magnetHash)