]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/video.ts
emit more specific status codes on video upload (#3423)
[github/Chocobozzz/PeerTube.git] / server / helpers / video.ts
1 import * as Bluebird from 'bluebird'
2 import { Response } from 'express'
3 import { CONFIG } from '@server/initializers/config'
4 import { DEFAULT_AUDIO_RESOLUTION } from '@server/initializers/constants'
5 import { JobQueue } from '@server/lib/job-queue'
6 import {
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'
18 import { VideoPrivacy, VideoState, VideoTranscodingPayload } from '@shared/models'
19 import { VideoModel } from '../models/video/video'
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 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
114 function getPrivaciesForFederation () {
115 return (CONFIG.FEDERATION.VIDEOS.FEDERATE_UNLISTED === true)
116 ? [ { privacy: VideoPrivacy.PUBLIC }, { privacy: VideoPrivacy.UNLISTED } ]
117 : [ { privacy: VideoPrivacy.PUBLIC } ]
118 }
119
120 function 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
128 export {
129 VideoFetchType,
130 VideoFetchByUrlType,
131 fetchVideo,
132 getVideoWithAttributes,
133 fetchVideoByUrl,
134 addOptimizeOrMergeAudioJob,
135 extractVideo,
136 getExtFromMimetype,
137 isStateForFederation,
138 isPrivacyForFederation,
139 getPrivaciesForFederation
140 }