Split types and typings
[oweals/peertube.git] / server / helpers / video.ts
1 import { VideoModel } from '../models/video/video'
2 import * as Bluebird from 'bluebird'
3 import {
4   isStreamingPlaylist,
5   MStreamingPlaylistVideo,
6   MVideo,
7   MVideoAccountLightBlacklistAllFiles,
8   MVideoFile,
9   MVideoFullLight,
10   MVideoIdThumbnail,
11   MVideoImmutable,
12   MVideoThumbnail,
13   MVideoWithRights
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"
20
21 type VideoFetchType = 'all' | 'only-video' | 'only-video-with-rights' | 'id' | 'none' | 'only-immutable-attributes'
22
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>
28 function fetchVideo (
29   id: number | string,
30   fetchType: VideoFetchType,
31   userId?: number
32 ): Bluebird<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail | MVideoImmutable>
33 function 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
49 type VideoFetchByUrlType = 'all' | 'only-video' | 'only-immutable-attributes'
50
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 (
55   url: string,
56   fetchType: VideoFetchByUrlType
57 ): Bluebird<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable>
58 function 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
69 function getVideoWithAttributes (res: Response) {
70   return res.locals.videoAll || res.locals.onlyVideo || res.locals.onlyVideoWithRights
71 }
72
73 function 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
94 function extractVideo (videoOrPlaylist: MVideo | MStreamingPlaylistVideo) {
95   return isStreamingPlaylist(videoOrPlaylist)
96     ? videoOrPlaylist.Video
97     : videoOrPlaylist
98 }
99
100 function 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
107 function getPrivaciesForFederation () {
108   return (CONFIG.FEDERATION.VIDEOS.FEDERATE_UNLISTED === true)
109     ? [ { privacy: VideoPrivacy.PUBLIC }, { privacy: VideoPrivacy.UNLISTED } ]
110     : [ { privacy: VideoPrivacy.PUBLIC } ]
111 }
112
113 export {
114   VideoFetchType,
115   VideoFetchByUrlType,
116   fetchVideo,
117   getVideoWithAttributes,
118   fetchVideoByUrl,
119   addOptimizeOrMergeAudioJob,
120   extractVideo,
121   isPrivacyForFederation,
122   getPrivaciesForFederation
123 }