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'
26 } from 'sequelize-typescript'
27 import { IIncludeOptions } from 'sequelize-typescript/lib/interfaces/IIncludeOptions'
28 import { VideoPrivacy, VideoResolution } from '../../../shared'
29 import { VideoTorrentObject } from '../../../shared/models/activitypub/objects'
31 activityPubCollection,
33 generateImageFromVideoFile,
41 } from '../../helpers'
42 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub'
45 isVideoDescriptionValid,
52 } from '../../helpers/custom-validators/videos'
65 } from '../../initializers'
66 import { getAnnounceActivityPubUrl } from '../../lib/activitypub'
67 import { sendDeleteVideo } from '../../lib/index'
68 import { AccountModel } from '../account/account'
69 import { AccountVideoRateModel } from '../account/account-video-rate'
70 import { ServerModel } from '../server/server'
71 import { getSort, throwIfNotValid } from '../utils'
72 import { TagModel } from './tag'
73 import { VideoAbuseModel } from './video-abuse'
74 import { VideoChannelModel } from './video-channel'
75 import { VideoFileModel } from './video-file'
76 import { VideoShareModel } from './video-share'
77 import { VideoTagModel } from './video-tag'
86 fields: [ 'createdAt' ]
89 fields: [ 'duration' ]
101 fields: [ 'channelId' ]
105 export class VideoModel extends Model<VideoModel> {
108 @Default(DataType.UUIDV4)
110 @Column(DataType.UUID)
114 @Is('VideoName', value => throwIfNotValid(value, isVideoNameValid, 'name'))
120 @Is('VideoCategory', value => throwIfNotValid(value, isVideoCategoryValid, 'category'))
126 @Is('VideoLicence', value => throwIfNotValid(value, isVideoLicenceValid, 'licence'))
132 @Is('VideoLanguage', value => throwIfNotValid(value, isVideoLanguageValid, 'language'))
137 @Is('VideoPrivacy', value => throwIfNotValid(value, isVideoPrivacyValid, 'privacy'))
142 @Is('VideoNSFW', value => throwIfNotValid(value, isVideoNSFWValid, 'NSFW boolean'))
148 @Is('VideoDescription', value => throwIfNotValid(value, isVideoDescriptionValid, 'description'))
149 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max))
153 @Is('VideoDuration', value => throwIfNotValid(value, isVideoDurationValid, 'duration'))
183 @Is('VideoUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
184 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
193 @ForeignKey(() => VideoChannelModel)
197 @BelongsTo(() => VideoChannelModel, {
203 VideoChannel: VideoChannelModel
205 @BelongsToMany(() => TagModel, {
206 foreignKey: 'videoId',
207 through: () => VideoTagModel,
212 @HasMany(() => VideoAbuseModel, {
219 VideoAbuses: VideoAbuseModel[]
221 @HasMany(() => VideoFileModel, {
228 VideoFiles: VideoFileModel[]
230 @HasMany(() => VideoShareModel, {
237 VideoShares: VideoShareModel[]
239 @HasMany(() => AccountVideoRateModel, {
246 AccountVideoRates: AccountVideoRateModel[]
249 static removeFilesAndSendDelete (instance: VideoModel) {
253 instance.removeThumbnail()
256 if (instance.isOwned()) {
258 instance.removePreview(),
259 sendDeleteVideo(instance, undefined)
262 // Remove physical files and torrents
263 instance.VideoFiles.forEach(file => {
264 tasks.push(instance.removeFile(file))
265 tasks.push(instance.removeTorrent(file))
269 return Promise.all(tasks)
271 logger.error('Some errors when removing files of video %s in after destroy hook.', instance.uuid, err)
277 include: [ VideoFileModel ]
280 return VideoModel.findAll(query)
283 static listAllAndSharedByAccountForOutbox (accountId: number, start: number, count: number) {
284 function getRawQuery (select: string) {
285 const queryVideo = 'SELECT ' + select + ' FROM "video" AS "Video" ' +
286 'INNER JOIN "videoChannel" AS "VideoChannel" ON "VideoChannel"."id" = "Video"."channelId" ' +
287 'WHERE "VideoChannel"."accountId" = ' + accountId
288 const queryVideoShare = 'SELECT ' + select + ' FROM "videoShare" AS "VideoShare" ' +
289 'INNER JOIN "video" AS "Video" ON "Video"."id" = "VideoShare"."videoId" ' +
290 'WHERE "VideoShare"."accountId" = ' + accountId
292 return `(${queryVideo}) UNION (${queryVideoShare})`
295 const rawQuery = getRawQuery('"Video"."id"')
296 const rawCountQuery = getRawQuery('COUNT("Video"."id") as "total"')
302 order: [ getSort('createdAt'), [ 'Tags', 'name', 'ASC' ] ],
305 [Sequelize.Op.in]: Sequelize.literal('(' + rawQuery + ')')
310 model: VideoShareModel,
313 [Sequelize.Op.and]: [
316 [Sequelize.Op.not]: null
324 include: [ AccountModel ]
327 model: VideoChannelModel,
337 model: AccountVideoRateModel,
338 include: [ AccountModel ]
345 return Bluebird.all([
346 // FIXME: typing issue
347 VideoModel.findAll(query as any),
348 VideoModel.sequelize.query(rawCountQuery, { type: Sequelize.QueryTypes.SELECT })
349 ]).then(([ rows, totals ]) => {
350 // totals: totalVideos + totalVideoShares
352 let totalVideoShares = 0
353 if (totals[0]) totalVideos = parseInt(totals[0].total, 10)
354 if (totals[1]) totalVideoShares = parseInt(totals[1].total, 10)
356 const total = totalVideos + totalVideoShares
364 static listUserVideosForApi (userId: number, start: number, count: number, sort: string) {
369 order: [ getSort(sort), [ 'Tags', 'name', 'ASC' ] ],
372 model: VideoChannelModel,
388 return VideoModel.findAndCountAll(query).then(({ rows, count }) => {
396 static listForApi (start: number, count: number, sort: string) {
401 order: [ getSort(sort), [ 'Tags', 'name', 'ASC' ] ],
404 model: VideoChannelModel,
421 where: this.createBaseVideosWhere()
424 return VideoModel.findAndCountAll(query).then(({ rows, count }) => {
432 static load (id: number) {
433 return VideoModel.findById(id)
436 static loadByUUID (uuid: string, t?: Sequelize.Transaction) {
437 const query: IFindOptions<VideoModel> = {
441 include: [ VideoFileModel ]
444 if (t !== undefined) query.transaction = t
446 return VideoModel.findOne(query)
449 static loadByUrlAndPopulateAccount (url: string, t?: Sequelize.Transaction) {
450 const query: IFindOptions<VideoModel> = {
457 model: VideoChannelModel,
458 include: [ AccountModel ]
463 if (t !== undefined) query.transaction = t
465 return VideoModel.findOne(query)
468 static loadByUUIDOrURL (uuid: string, url: string, t?: Sequelize.Transaction) {
469 const query: IFindOptions<VideoModel> = {
476 include: [ VideoFileModel ]
479 if (t !== undefined) query.transaction = t
481 return VideoModel.findOne(query)
484 static loadAndPopulateAccountAndServerAndTags (id: number) {
486 order: [ [ 'Tags', 'name', 'ASC' ] ],
489 model: VideoChannelModel,
493 include: [ { model: ServerModel, required: false } ]
498 model: AccountVideoRateModel,
499 include: [ AccountModel ]
502 model: VideoShareModel,
503 include: [ AccountModel ]
510 return VideoModel.findById(id, options)
513 static loadByUUIDAndPopulateAccountAndServerAndTags (uuid: string) {
515 order: [ [ 'Tags', 'name', 'ASC' ] ],
521 model: VideoChannelModel,
525 include: [ { model: ServerModel, required: false } ]
530 model: AccountVideoRateModel,
531 include: [ AccountModel ]
534 model: VideoShareModel,
535 include: [ AccountModel ]
542 return VideoModel.findOne(options)
545 static searchAndPopulateAccountAndServerAndTags (value: string, start: number, count: number, sort: string) {
546 const serverInclude: IIncludeOptions = {
551 const accountInclude: IIncludeOptions = {
553 include: [ serverInclude ]
556 const videoChannelInclude: IIncludeOptions = {
557 model: VideoChannelModel,
558 include: [ accountInclude ],
562 const tagInclude: IIncludeOptions = {
566 const query: IFindOptions<VideoModel> = {
568 where: this.createBaseVideosWhere(),
571 order: [ getSort(sort), [ 'Tags', 'name', 'ASC' ] ]
574 // TODO: search on tags too
575 // const escapedValue = Video['sequelize'].escape('%' + value + '%')
576 // query.where['id'][Sequelize.Op.in] = Video['sequelize'].literal(
577 // `(SELECT "VideoTags"."videoId"
579 // INNER JOIN "VideoTags" ON "Tags"."id" = "VideoTags"."tagId"
580 // WHERE name ILIKE ${escapedValue}
584 // TODO: search on account too
585 // accountInclude.where = {
587 // [Sequelize.Op.iLike]: '%' + value + '%'
590 query.where['name'] = {
591 [Sequelize.Op.iLike]: '%' + value + '%'
595 videoChannelInclude, tagInclude
598 return VideoModel.findAndCountAll(query).then(({ rows, count }) => {
606 private static createBaseVideosWhere () {
609 [Sequelize.Op.notIn]: VideoModel.sequelize.literal(
610 '(SELECT "videoBlacklist"."videoId" FROM "videoBlacklist")'
613 privacy: VideoPrivacy.PUBLIC
618 if (Array.isArray(this.VideoFiles) === false) return undefined
620 // The original file is the file that have the higher resolution
621 return maxBy(this.VideoFiles, file => file.resolution)
624 getVideoFilename (videoFile: VideoFileModel) {
625 return this.uuid + '-' + videoFile.resolution + videoFile.extname
628 getThumbnailName () {
629 // We always have a copy of the thumbnail
630 const extension = '.jpg'
631 return this.uuid + extension
635 const extension = '.jpg'
636 return this.uuid + extension
639 getTorrentFileName (videoFile: VideoFileModel) {
640 const extension = '.torrent'
641 return this.uuid + '-' + videoFile.resolution + extension
645 return this.remote === false
648 createPreview (videoFile: VideoFileModel) {
649 const imageSize = PREVIEWS_SIZE.width + 'x' + PREVIEWS_SIZE.height
651 return generateImageFromVideoFile(
652 this.getVideoFilePath(videoFile),
653 CONFIG.STORAGE.PREVIEWS_DIR,
654 this.getPreviewName(),
659 createThumbnail (videoFile: VideoFileModel) {
660 const imageSize = THUMBNAILS_SIZE.width + 'x' + THUMBNAILS_SIZE.height
662 return generateImageFromVideoFile(
663 this.getVideoFilePath(videoFile),
664 CONFIG.STORAGE.THUMBNAILS_DIR,
665 this.getThumbnailName(),
670 getVideoFilePath (videoFile: VideoFileModel) {
671 return join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
674 createTorrentAndSetInfoHash = async function (videoFile: VideoFileModel) {
677 [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ]
680 CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
684 const torrent = await createTorrentPromise(this.getVideoFilePath(videoFile), options)
686 const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
687 logger.info('Creating torrent %s.', filePath)
689 await writeFilePromise(filePath, torrent)
691 const parsedTorrent = parseTorrent(torrent)
692 videoFile.infoHash = parsedTorrent.infoHash
696 return '/videos/embed/' + this.uuid
699 getThumbnailPath () {
700 return join(STATIC_PATHS.THUMBNAILS, this.getThumbnailName())
704 return join(STATIC_PATHS.PREVIEWS, this.getPreviewName())
710 if (this.VideoChannel.Account.Server) {
711 serverHost = this.VideoChannel.Account.Server.host
713 // It means it's our video
714 serverHost = CONFIG.WEBSERVER.HOST
721 category: this.category,
722 categoryLabel: this.getCategoryLabel(),
723 licence: this.licence,
724 licenceLabel: this.getLicenceLabel(),
725 language: this.language,
726 languageLabel: this.getLanguageLabel(),
728 description: this.getTruncatedDescription(),
730 isLocal: this.isOwned(),
731 accountName: this.VideoChannel.Account.name,
732 duration: this.duration,
735 dislikes: this.dislikes,
736 tags: map<TagModel, string>(this.Tags, 'name'),
737 thumbnailPath: this.getThumbnailPath(),
738 previewPath: this.getPreviewPath(),
739 embedPath: this.getEmbedPath(),
740 createdAt: this.createdAt,
741 updatedAt: this.updatedAt
745 toFormattedDetailsJSON () {
746 const formattedJson = this.toFormattedJSON()
748 // Maybe our server is not up to date and there are new privacy settings since our version
749 let privacyLabel = VIDEO_PRIVACIES[this.privacy]
750 if (!privacyLabel) privacyLabel = 'Unknown'
752 const detailsJson = {
754 privacy: this.privacy,
755 descriptionPath: this.getDescriptionPath(),
756 channel: this.VideoChannel.toFormattedJSON(),
757 account: this.VideoChannel.Account.toFormattedJSON(),
761 // Format and sort video files
762 const { baseUrlHttp, baseUrlWs } = this.getBaseUrls()
763 detailsJson.files = this.VideoFiles
765 let resolutionLabel = videoFile.resolution + 'p'
768 resolution: videoFile.resolution,
770 magnetUri: this.generateMagnetUri(videoFile, baseUrlHttp, baseUrlWs),
771 size: videoFile.size,
772 torrentUrl: this.getTorrentUrl(videoFile, baseUrlHttp),
773 fileUrl: this.getVideoFileUrl(videoFile, baseUrlHttp)
777 if (a.resolution < b.resolution) return 1
778 if (a.resolution === b.resolution) return 0
782 return Object.assign(formattedJson, detailsJson)
785 toActivityPubObject (): VideoTorrentObject {
786 const { baseUrlHttp, baseUrlWs } = this.getBaseUrls()
787 if (!this.Tags) this.Tags = []
789 const tag = this.Tags.map(t => ({
790 type: 'Hashtag' as 'Hashtag',
797 identifier: this.language + '',
798 name: this.getLanguageLabel()
805 identifier: this.category + '',
806 name: this.getCategoryLabel()
813 identifier: this.licence + '',
814 name: this.getLicenceLabel()
821 if (Array.isArray(this.AccountVideoRates)) {
822 const likes: string[] = []
823 const dislikes: string[] = []
825 for (const rate of this.AccountVideoRates) {
826 if (rate.type === 'like') {
827 likes.push(rate.Account.url)
828 } else if (rate.type === 'dislike') {
829 dislikes.push(rate.Account.url)
833 likesObject = activityPubCollection(likes)
834 dislikesObject = activityPubCollection(dislikes)
838 if (Array.isArray(this.VideoShares)) {
839 const shares: string[] = []
841 for (const videoShare of this.VideoShares) {
842 const shareUrl = getAnnounceActivityPubUrl(this.url, videoShare.Account)
843 shares.push(shareUrl)
846 sharesObject = activityPubCollection(shares)
850 for (const file of this.VideoFiles) {
853 mimeType: 'video/' + file.extname.replace('.', ''),
854 url: this.getVideoFileUrl(file, baseUrlHttp),
855 width: file.resolution,
861 mimeType: 'application/x-bittorrent',
862 url: this.getTorrentUrl(file, baseUrlHttp),
863 width: file.resolution
868 mimeType: 'application/x-bittorrent;x-scheme-handler/magnet',
869 url: this.generateMagnetUri(file, baseUrlHttp, baseUrlWs),
870 width: file.resolution
877 mimeType: 'text/html',
878 url: CONFIG.WEBSERVER.URL + '/videos/watch/' + this.uuid
882 type: 'Video' as 'Video',
885 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
886 duration: 'PT' + this.duration + 'S',
894 published: this.createdAt.toISOString(),
895 updated: this.updatedAt.toISOString(),
896 mediaType: 'text/markdown',
897 content: this.getTruncatedDescription(),
900 url: this.getThumbnailUrl(baseUrlHttp),
901 mediaType: 'image/jpeg',
902 width: THUMBNAILS_SIZE.width,
903 height: THUMBNAILS_SIZE.height
907 dislikes: dislikesObject,
912 getTruncatedDescription () {
913 if (!this.description) return null
916 length: CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max
919 return truncate(this.description, options)
922 optimizeOriginalVideofile = async function () {
923 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
924 const newExtname = '.mp4'
925 const inputVideoFile = this.getOriginalFile()
926 const videoInputPath = join(videosDirectory, this.getVideoFilename(inputVideoFile))
927 const videoOutputPath = join(videosDirectory, this.id + '-transcoded' + newExtname)
929 const transcodeOptions = {
930 inputPath: videoInputPath,
931 outputPath: videoOutputPath
935 // Could be very long!
936 await transcode(transcodeOptions)
938 await unlinkPromise(videoInputPath)
940 // Important to do this before getVideoFilename() to take in account the new file extension
941 inputVideoFile.set('extname', newExtname)
943 await renamePromise(videoOutputPath, this.getVideoFilePath(inputVideoFile))
944 const stats = await statPromise(this.getVideoFilePath(inputVideoFile))
946 inputVideoFile.set('size', stats.size)
948 await this.createTorrentAndSetInfoHash(inputVideoFile)
949 await inputVideoFile.save()
952 // Auto destruction...
953 this.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', err))
959 transcodeOriginalVideofile = async function (resolution: VideoResolution) {
960 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
961 const extname = '.mp4'
963 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
964 const videoInputPath = join(videosDirectory, this.getVideoFilename(this.getOriginalFile()))
966 const newVideoFile = new VideoFileModel({
972 const videoOutputPath = join(videosDirectory, this.getVideoFilename(newVideoFile))
974 const transcodeOptions = {
975 inputPath: videoInputPath,
976 outputPath: videoOutputPath,
980 await transcode(transcodeOptions)
982 const stats = await statPromise(videoOutputPath)
984 newVideoFile.set('size', stats.size)
986 await this.createTorrentAndSetInfoHash(newVideoFile)
988 await newVideoFile.save()
990 this.VideoFiles.push(newVideoFile)
993 getOriginalFileHeight () {
994 const originalFilePath = this.getVideoFilePath(this.getOriginalFile())
996 return getVideoFileHeight(originalFilePath)
999 getDescriptionPath () {
1000 return `/api/${API_VERSION}/videos/${this.uuid}/description`
1003 getCategoryLabel () {
1004 let categoryLabel = VIDEO_CATEGORIES[this.category]
1005 if (!categoryLabel) categoryLabel = 'Misc'
1007 return categoryLabel
1010 getLicenceLabel () {
1011 let licenceLabel = VIDEO_LICENCES[this.licence]
1012 if (!licenceLabel) licenceLabel = 'Unknown'
1017 getLanguageLabel () {
1018 let languageLabel = VIDEO_LANGUAGES[this.language]
1019 if (!languageLabel) languageLabel = 'Unknown'
1021 return languageLabel
1024 removeThumbnail () {
1025 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
1026 return unlinkPromise(thumbnailPath)
1030 // Same name than video thumbnail
1031 return unlinkPromise(CONFIG.STORAGE.PREVIEWS_DIR + this.getPreviewName())
1034 removeFile (videoFile: VideoFileModel) {
1035 const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
1036 return unlinkPromise(filePath)
1039 removeTorrent (videoFile: VideoFileModel) {
1040 const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
1041 return unlinkPromise(torrentPath)
1044 private getBaseUrls () {
1048 if (this.isOwned()) {
1049 baseUrlHttp = CONFIG.WEBSERVER.URL
1050 baseUrlWs = CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
1052 baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + this.VideoChannel.Account.Server.host
1053 baseUrlWs = REMOTE_SCHEME.WS + '://' + this.VideoChannel.Account.Server.host
1056 return { baseUrlHttp, baseUrlWs }
1059 private getThumbnailUrl (baseUrlHttp: string) {
1060 return baseUrlHttp + STATIC_PATHS.THUMBNAILS + this.getThumbnailName()
1063 private getTorrentUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1064 return baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentFileName(videoFile)
1067 private getVideoFileUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1068 return baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
1071 private generateMagnetUri (videoFile: VideoFileModel, baseUrlHttp: string, baseUrlWs: string) {
1072 const xs = this.getTorrentUrl(videoFile, baseUrlHttp)
1073 const announce = [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
1074 const urlList = [ this.getVideoFileUrl(videoFile, baseUrlHttp) ]
1076 const magnetHash = {
1080 infoHash: videoFile.infoHash,
1084 return magnetUtil.encode(magnetHash)