1 import { VideoModel } from '../models/video/video'
2 import * as Bluebird from 'bluebird'
5 MStreamingPlaylistVideo,
7 MVideoAccountLightBlacklistAllFiles,
14 } from '@server/types/models'
15 import { Response } from 'express'
16 import { DEFAULT_AUDIO_RESOLUTION } from '@server/initializers/constants'
17 import { JobQueue } from '@server/lib/job-queue'
18 import { VideoPrivacy, VideoTranscodingPayload } from '@shared/models'
19 import { CONFIG } from "@server/initializers/config"
21 type VideoFetchType = 'all' | 'only-video' | 'only-video-with-rights' | 'id' | 'none' | 'only-immutable-attributes'
23 function fetchVideo (id: number | string, fetchType: 'all', userId?: number): Bluebird<MVideoFullLight>
24 function fetchVideo (id: number | string, fetchType: 'only-immutable-attributes'): Bluebird<MVideoImmutable>
25 function fetchVideo (id: number | string, fetchType: 'only-video', userId?: number): Bluebird<MVideoThumbnail>
26 function fetchVideo (id: number | string, fetchType: 'only-video-with-rights', userId?: number): Bluebird<MVideoWithRights>
27 function fetchVideo (id: number | string, fetchType: 'id' | 'none', userId?: number): Bluebird<MVideoIdThumbnail>
30 fetchType: VideoFetchType,
32 ): Bluebird<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail | MVideoImmutable>
35 fetchType: VideoFetchType,
37 ): Bluebird<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail | MVideoImmutable> {
38 if (fetchType === 'all') return VideoModel.loadAndPopulateAccountAndServerAndTags(id, undefined, userId)
40 if (fetchType === 'only-immutable-attributes') return VideoModel.loadImmutableAttributes(id)
42 if (fetchType === 'only-video-with-rights') return VideoModel.loadWithRights(id)
44 if (fetchType === 'only-video') return VideoModel.load(id)
46 if (fetchType === 'id' || fetchType === 'none') return VideoModel.loadOnlyId(id)
49 type VideoFetchByUrlType = 'all' | 'only-video' | 'only-immutable-attributes'
51 function fetchVideoByUrl (url: string, fetchType: 'all'): Bluebird<MVideoAccountLightBlacklistAllFiles>
52 function fetchVideoByUrl (url: string, fetchType: 'only-immutable-attributes'): Bluebird<MVideoImmutable>
53 function fetchVideoByUrl (url: string, fetchType: 'only-video'): Bluebird<MVideoThumbnail>
54 function fetchVideoByUrl (
56 fetchType: VideoFetchByUrlType
57 ): Bluebird<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable>
58 function fetchVideoByUrl (
60 fetchType: VideoFetchByUrlType
61 ): Bluebird<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable> {
62 if (fetchType === 'all') return VideoModel.loadByUrlAndPopulateAccount(url)
64 if (fetchType === 'only-immutable-attributes') return VideoModel.loadByUrlImmutableAttributes(url)
66 if (fetchType === 'only-video') return VideoModel.loadByUrl(url)
69 function getVideoWithAttributes (res: Response) {
70 return res.locals.videoAll || res.locals.onlyVideo || res.locals.onlyVideoWithRights
73 function addOptimizeOrMergeAudioJob (video: MVideo, videoFile: MVideoFile) {
74 let dataInput: VideoTranscodingPayload
76 if (videoFile.isAudio()) {
78 type: 'merge-audio' as 'merge-audio',
79 resolution: DEFAULT_AUDIO_RESOLUTION,
80 videoUUID: video.uuid,
85 type: 'optimize' as 'optimize',
86 videoUUID: video.uuid,
91 return JobQueue.Instance.createJobWithPromise({ type: 'video-transcoding', payload: dataInput })
94 function extractVideo (videoOrPlaylist: MVideo | MStreamingPlaylistVideo) {
95 return isStreamingPlaylist(videoOrPlaylist)
96 ? videoOrPlaylist.Video
100 function isPrivacyForFederation (privacy: VideoPrivacy) {
101 const castedPrivacy = parseInt(privacy + '', 10)
103 return castedPrivacy === VideoPrivacy.PUBLIC ||
104 (CONFIG.FEDERATION.VIDEOS.FEDERATE_UNLISTED === true && castedPrivacy === VideoPrivacy.UNLISTED)
107 function getPrivaciesForFederation () {
108 return (CONFIG.FEDERATION.VIDEOS.FEDERATE_UNLISTED === true)
109 ? [ { privacy: VideoPrivacy.PUBLIC }, { privacy: VideoPrivacy.UNLISTED } ]
110 : [ { privacy: VideoPrivacy.PUBLIC } ]
117 getVideoWithAttributes,
119 addOptimizeOrMergeAudioJob,
121 isPrivacyForFederation,
122 getPrivaciesForFederation