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