]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/helpers/video.ts
Increase timeouts
[github/Chocobozzz/PeerTube.git] / server / helpers / video.ts
... / ...
CommitLineData
1import { Response } from 'express'
2import { CONFIG } from '@server/initializers/config'
3import {
4 isStreamingPlaylist,
5 MStreamingPlaylistVideo,
6 MVideo,
7 MVideoAccountLightBlacklistAllFiles,
8 MVideoFullLight,
9 MVideoIdThumbnail,
10 MVideoImmutable,
11 MVideoThumbnail,
12 MVideoWithRights
13} from '@server/types/models'
14import { VideoPrivacy, VideoState } from '@shared/models'
15import { VideoModel } from '../models/video/video'
16
17type VideoFetchType = 'all' | 'only-video' | 'only-video-with-rights' | 'id' | 'none' | 'only-immutable-attributes'
18
19function fetchVideo (id: number | string, fetchType: 'all', userId?: number): Promise<MVideoFullLight>
20function fetchVideo (id: number | string, fetchType: 'only-immutable-attributes'): Promise<MVideoImmutable>
21function fetchVideo (id: number | string, fetchType: 'only-video', userId?: number): Promise<MVideoThumbnail>
22function fetchVideo (id: number | string, fetchType: 'only-video-with-rights', userId?: number): Promise<MVideoWithRights>
23function fetchVideo (id: number | string, fetchType: 'id' | 'none', userId?: number): Promise<MVideoIdThumbnail>
24function fetchVideo (
25 id: number | string,
26 fetchType: VideoFetchType,
27 userId?: number
28): Promise<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail | MVideoImmutable>
29function fetchVideo (
30 id: number | string,
31 fetchType: VideoFetchType,
32 userId?: number
33): Promise<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail | MVideoImmutable> {
34 if (fetchType === 'all') return VideoModel.loadAndPopulateAccountAndServerAndTags(id, undefined, userId)
35
36 if (fetchType === 'only-immutable-attributes') return VideoModel.loadImmutableAttributes(id)
37
38 if (fetchType === 'only-video-with-rights') return VideoModel.loadWithRights(id)
39
40 if (fetchType === 'only-video') return VideoModel.load(id)
41
42 if (fetchType === 'id' || fetchType === 'none') return VideoModel.loadOnlyId(id)
43}
44
45type VideoFetchByUrlType = 'all' | 'only-video' | 'only-immutable-attributes'
46
47function fetchVideoByUrl (url: string, fetchType: 'all'): Promise<MVideoAccountLightBlacklistAllFiles>
48function fetchVideoByUrl (url: string, fetchType: 'only-immutable-attributes'): Promise<MVideoImmutable>
49function fetchVideoByUrl (url: string, fetchType: 'only-video'): Promise<MVideoThumbnail>
50function fetchVideoByUrl (
51 url: string,
52 fetchType: VideoFetchByUrlType
53): Promise<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable>
54function fetchVideoByUrl (
55 url: string,
56 fetchType: VideoFetchByUrlType
57): Promise<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable> {
58 if (fetchType === 'all') return VideoModel.loadByUrlAndPopulateAccount(url)
59
60 if (fetchType === 'only-immutable-attributes') return VideoModel.loadByUrlImmutableAttributes(url)
61
62 if (fetchType === 'only-video') return VideoModel.loadByUrl(url)
63}
64
65function getVideoWithAttributes (res: Response) {
66 return res.locals.videoAll || res.locals.onlyVideo || res.locals.onlyVideoWithRights
67}
68
69function extractVideo (videoOrPlaylist: MVideo | MStreamingPlaylistVideo) {
70 return isStreamingPlaylist(videoOrPlaylist)
71 ? videoOrPlaylist.Video
72 : videoOrPlaylist
73}
74
75function isPrivacyForFederation (privacy: VideoPrivacy) {
76 const castedPrivacy = parseInt(privacy + '', 10)
77
78 return castedPrivacy === VideoPrivacy.PUBLIC ||
79 (CONFIG.FEDERATION.VIDEOS.FEDERATE_UNLISTED === true && castedPrivacy === VideoPrivacy.UNLISTED)
80}
81
82function isStateForFederation (state: VideoState) {
83 const castedState = parseInt(state + '', 10)
84
85 return castedState === VideoState.PUBLISHED || castedState === VideoState.WAITING_FOR_LIVE || castedState === VideoState.LIVE_ENDED
86}
87
88function getPrivaciesForFederation () {
89 return (CONFIG.FEDERATION.VIDEOS.FEDERATE_UNLISTED === true)
90 ? [ { privacy: VideoPrivacy.PUBLIC }, { privacy: VideoPrivacy.UNLISTED } ]
91 : [ { privacy: VideoPrivacy.PUBLIC } ]
92}
93
94function getExtFromMimetype (mimeTypes: { [id: string]: string | string[] }, mimeType: string) {
95 const value = mimeTypes[mimeType]
96
97 if (Array.isArray(value)) return value[0]
98
99 return value
100}
101
102export {
103 VideoFetchType,
104 VideoFetchByUrlType,
105 fetchVideo,
106 getVideoWithAttributes,
107 fetchVideoByUrl,
108 extractVideo,
109 getExtFromMimetype,
110 isStateForFederation,
111 isPrivacyForFederation,
112 getPrivaciesForFederation
113}