]>
Commit | Line | Data |
---|---|---|
30bc55c8 C |
1 | import { Response } from 'express' |
2 | import { CONFIG } from '@server/initializers/config' | |
0283eaac | 3 | import { |
8dc8a34e C |
4 | isStreamingPlaylist, |
5 | MStreamingPlaylistVideo, | |
6 | MVideo, | |
0283eaac C |
7 | MVideoAccountLightBlacklistAllFiles, |
8 | MVideoFullLight, | |
9 | MVideoIdThumbnail, | |
8dc8a34e | 10 | MVideoImmutable, |
0283eaac | 11 | MVideoThumbnail, |
8dc8a34e | 12 | MVideoWithRights |
26d6bf65 | 13 | } from '@server/types/models' |
77d7e851 | 14 | import { VideoPrivacy, VideoState } from '@shared/models' |
30bc55c8 | 15 | import { VideoModel } from '../models/video/video' |
4157cdb1 | 16 | |
7eba5e1f | 17 | type VideoFetchType = 'all' | 'only-video' | 'only-video-with-rights' | 'id' | 'none' | 'only-immutable-attributes' |
4157cdb1 | 18 | |
b49f22d8 C |
19 | function fetchVideo (id: number | string, fetchType: 'all', userId?: number): Promise<MVideoFullLight> |
20 | function fetchVideo (id: number | string, fetchType: 'only-immutable-attributes'): Promise<MVideoImmutable> | |
21 | function fetchVideo (id: number | string, fetchType: 'only-video', userId?: number): Promise<MVideoThumbnail> | |
22 | function fetchVideo (id: number | string, fetchType: 'only-video-with-rights', userId?: number): Promise<MVideoWithRights> | |
23 | function fetchVideo (id: number | string, fetchType: 'id' | 'none', userId?: number): Promise<MVideoIdThumbnail> | |
453e83ea C |
24 | function fetchVideo ( |
25 | id: number | string, | |
26 | fetchType: VideoFetchType, | |
27 | userId?: number | |
b49f22d8 | 28 | ): Promise<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail | MVideoImmutable> |
453e83ea C |
29 | function fetchVideo ( |
30 | id: number | string, | |
31 | fetchType: VideoFetchType, | |
32 | userId?: number | |
b49f22d8 | 33 | ): Promise<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail | MVideoImmutable> { |
6e46de09 | 34 | if (fetchType === 'all') return VideoModel.loadAndPopulateAccountAndServerAndTags(id, undefined, userId) |
4157cdb1 | 35 | |
7eba5e1f C |
36 | if (fetchType === 'only-immutable-attributes') return VideoModel.loadImmutableAttributes(id) |
37 | ||
09209296 C |
38 | if (fetchType === 'only-video-with-rights') return VideoModel.loadWithRights(id) |
39 | ||
4157cdb1 C |
40 | if (fetchType === 'only-video') return VideoModel.load(id) |
41 | ||
42 | if (fetchType === 'id' || fetchType === 'none') return VideoModel.loadOnlyId(id) | |
43 | } | |
44 | ||
943e5193 | 45 | type VideoFetchByUrlType = 'all' | 'only-video' | 'only-immutable-attributes' |
453e83ea | 46 | |
b49f22d8 C |
47 | function fetchVideoByUrl (url: string, fetchType: 'all'): Promise<MVideoAccountLightBlacklistAllFiles> |
48 | function fetchVideoByUrl (url: string, fetchType: 'only-immutable-attributes'): Promise<MVideoImmutable> | |
49 | function fetchVideoByUrl (url: string, fetchType: 'only-video'): Promise<MVideoThumbnail> | |
943e5193 C |
50 | function fetchVideoByUrl ( |
51 | url: string, | |
52 | fetchType: VideoFetchByUrlType | |
b49f22d8 | 53 | ): Promise<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable> |
943e5193 C |
54 | function fetchVideoByUrl ( |
55 | url: string, | |
56 | fetchType: VideoFetchByUrlType | |
b49f22d8 | 57 | ): Promise<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable> { |
4157cdb1 C |
58 | if (fetchType === 'all') return VideoModel.loadByUrlAndPopulateAccount(url) |
59 | ||
943e5193 C |
60 | if (fetchType === 'only-immutable-attributes') return VideoModel.loadByUrlImmutableAttributes(url) |
61 | ||
4157cdb1 C |
62 | if (fetchType === 'only-video') return VideoModel.loadByUrl(url) |
63 | } | |
64 | ||
0283eaac C |
65 | function getVideoWithAttributes (res: Response) { |
66 | return res.locals.videoAll || res.locals.onlyVideo || res.locals.onlyVideoWithRights | |
67 | } | |
68 | ||
8dc8a34e C |
69 | function extractVideo (videoOrPlaylist: MVideo | MStreamingPlaylistVideo) { |
70 | return isStreamingPlaylist(videoOrPlaylist) | |
71 | ? videoOrPlaylist.Video | |
72 | : videoOrPlaylist | |
73 | } | |
74 | ||
3092e9bb LB |
75 | function 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 | ||
68e70a74 C |
82 | function isStateForFederation (state: VideoState) { |
83 | const castedState = parseInt(state + '', 10) | |
84 | ||
85 | return castedState === VideoState.PUBLISHED || castedState === VideoState.WAITING_FOR_LIVE || castedState === VideoState.LIVE_ENDED | |
68e70a74 C |
86 | } |
87 | ||
3092e9bb LB |
88 | function getPrivaciesForFederation () { |
89 | return (CONFIG.FEDERATION.VIDEOS.FEDERATE_UNLISTED === true) | |
90 | ? [ { privacy: VideoPrivacy.PUBLIC }, { privacy: VideoPrivacy.UNLISTED } ] | |
91 | : [ { privacy: VideoPrivacy.PUBLIC } ] | |
92 | } | |
93 | ||
30bc55c8 C |
94 | function 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 | ||
4157cdb1 C |
102 | export { |
103 | VideoFetchType, | |
104 | VideoFetchByUrlType, | |
105 | fetchVideo, | |
0283eaac | 106 | getVideoWithAttributes, |
8dc8a34e | 107 | fetchVideoByUrl, |
3092e9bb | 108 | extractVideo, |
30bc55c8 | 109 | getExtFromMimetype, |
68e70a74 | 110 | isStateForFederation, |
3092e9bb LB |
111 | isPrivacyForFederation, |
112 | getPrivaciesForFederation | |
4157cdb1 | 113 | } |