]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/helpers/video.ts
Add live notification tests
[github/Chocobozzz/PeerTube.git] / server / helpers / video.ts
... / ...
CommitLineData
1import * as Bluebird from 'bluebird'
2import { Response } from 'express'
3import { CONFIG } from '@server/initializers/config'
4import { DEFAULT_AUDIO_RESOLUTION } from '@server/initializers/constants'
5import { JobQueue } from '@server/lib/job-queue'
6import {
7 isStreamingPlaylist,
8 MStreamingPlaylistVideo,
9 MVideo,
10 MVideoAccountLightBlacklistAllFiles,
11 MVideoFile,
12 MVideoFullLight,
13 MVideoIdThumbnail,
14 MVideoImmutable,
15 MVideoThumbnail,
16 MVideoWithRights
17} from '@server/types/models'
18import { VideoPrivacy, VideoState, VideoTranscodingPayload } from '@shared/models'
19import { VideoModel } from '../models/video/video'
20
21type VideoFetchType = 'all' | 'only-video' | 'only-video-with-rights' | 'id' | 'none' | 'only-immutable-attributes'
22
23function fetchVideo (id: number | string, fetchType: 'all', userId?: number): Bluebird<MVideoFullLight>
24function fetchVideo (id: number | string, fetchType: 'only-immutable-attributes'): Bluebird<MVideoImmutable>
25function fetchVideo (id: number | string, fetchType: 'only-video', userId?: number): Bluebird<MVideoThumbnail>
26function fetchVideo (id: number | string, fetchType: 'only-video-with-rights', userId?: number): Bluebird<MVideoWithRights>
27function fetchVideo (id: number | string, fetchType: 'id' | 'none', userId?: number): Bluebird<MVideoIdThumbnail>
28function fetchVideo (
29 id: number | string,
30 fetchType: VideoFetchType,
31 userId?: number
32): Bluebird<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail | MVideoImmutable>
33function fetchVideo (
34 id: number | string,
35 fetchType: VideoFetchType,
36 userId?: number
37): Bluebird<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail | MVideoImmutable> {
38 if (fetchType === 'all') return VideoModel.loadAndPopulateAccountAndServerAndTags(id, undefined, userId)
39
40 if (fetchType === 'only-immutable-attributes') return VideoModel.loadImmutableAttributes(id)
41
42 if (fetchType === 'only-video-with-rights') return VideoModel.loadWithRights(id)
43
44 if (fetchType === 'only-video') return VideoModel.load(id)
45
46 if (fetchType === 'id' || fetchType === 'none') return VideoModel.loadOnlyId(id)
47}
48
49type VideoFetchByUrlType = 'all' | 'only-video' | 'only-immutable-attributes'
50
51function fetchVideoByUrl (url: string, fetchType: 'all'): Bluebird<MVideoAccountLightBlacklistAllFiles>
52function fetchVideoByUrl (url: string, fetchType: 'only-immutable-attributes'): Bluebird<MVideoImmutable>
53function fetchVideoByUrl (url: string, fetchType: 'only-video'): Bluebird<MVideoThumbnail>
54function fetchVideoByUrl (
55 url: string,
56 fetchType: VideoFetchByUrlType
57): Bluebird<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable>
58function fetchVideoByUrl (
59 url: string,
60 fetchType: VideoFetchByUrlType
61): Bluebird<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable> {
62 if (fetchType === 'all') return VideoModel.loadByUrlAndPopulateAccount(url)
63
64 if (fetchType === 'only-immutable-attributes') return VideoModel.loadByUrlImmutableAttributes(url)
65
66 if (fetchType === 'only-video') return VideoModel.loadByUrl(url)
67}
68
69function getVideoWithAttributes (res: Response) {
70 return res.locals.videoAll || res.locals.onlyVideo || res.locals.onlyVideoWithRights
71}
72
73function addOptimizeOrMergeAudioJob (video: MVideo, videoFile: MVideoFile) {
74 let dataInput: VideoTranscodingPayload
75
76 if (videoFile.isAudio()) {
77 dataInput = {
78 type: 'merge-audio' as 'merge-audio',
79 resolution: DEFAULT_AUDIO_RESOLUTION,
80 videoUUID: video.uuid,
81 isNewVideo: true
82 }
83 } else {
84 dataInput = {
85 type: 'optimize' as 'optimize',
86 videoUUID: video.uuid,
87 isNewVideo: true
88 }
89 }
90
91 return JobQueue.Instance.createJobWithPromise({ type: 'video-transcoding', payload: dataInput })
92}
93
94function extractVideo (videoOrPlaylist: MVideo | MStreamingPlaylistVideo) {
95 return isStreamingPlaylist(videoOrPlaylist)
96 ? videoOrPlaylist.Video
97 : videoOrPlaylist
98}
99
100function isPrivacyForFederation (privacy: VideoPrivacy) {
101 const castedPrivacy = parseInt(privacy + '', 10)
102
103 return castedPrivacy === VideoPrivacy.PUBLIC ||
104 (CONFIG.FEDERATION.VIDEOS.FEDERATE_UNLISTED === true && castedPrivacy === VideoPrivacy.UNLISTED)
105}
106
107function isStateForFederation (state: VideoState) {
108 const castedState = parseInt(state + '', 10)
109
110 return castedState === VideoState.PUBLISHED || castedState === VideoState.WAITING_FOR_LIVE || castedState === VideoState.LIVE_ENDED
111
112}
113
114function getPrivaciesForFederation () {
115 return (CONFIG.FEDERATION.VIDEOS.FEDERATE_UNLISTED === true)
116 ? [ { privacy: VideoPrivacy.PUBLIC }, { privacy: VideoPrivacy.UNLISTED } ]
117 : [ { privacy: VideoPrivacy.PUBLIC } ]
118}
119
120function getExtFromMimetype (mimeTypes: { [id: string]: string | string[] }, mimeType: string) {
121 const value = mimeTypes[mimeType]
122
123 if (Array.isArray(value)) return value[0]
124
125 return value
126}
127
128export {
129 VideoFetchType,
130 VideoFetchByUrlType,
131 fetchVideo,
132 getVideoWithAttributes,
133 fetchVideoByUrl,
134 addOptimizeOrMergeAudioJob,
135 extractVideo,
136 getExtFromMimetype,
137 isStateForFederation,
138 isPrivacyForFederation,
139 getPrivaciesForFederation
140}