]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/video.ts
emit more specific status codes on video upload (#3423)
[github/Chocobozzz/PeerTube.git] / server / helpers / video.ts
CommitLineData
453e83ea 1import * as Bluebird from 'bluebird'
30bc55c8
C
2import { Response } from 'express'
3import { CONFIG } from '@server/initializers/config'
4import { DEFAULT_AUDIO_RESOLUTION } from '@server/initializers/constants'
5import { JobQueue } from '@server/lib/job-queue'
0283eaac 6import {
8dc8a34e
C
7 isStreamingPlaylist,
8 MStreamingPlaylistVideo,
9 MVideo,
0283eaac 10 MVideoAccountLightBlacklistAllFiles,
8dc8a34e 11 MVideoFile,
0283eaac
C
12 MVideoFullLight,
13 MVideoIdThumbnail,
8dc8a34e 14 MVideoImmutable,
0283eaac 15 MVideoThumbnail,
8dc8a34e 16 MVideoWithRights
26d6bf65 17} from '@server/types/models'
68e70a74 18import { VideoPrivacy, VideoState, VideoTranscodingPayload } from '@shared/models'
30bc55c8 19import { VideoModel } from '../models/video/video'
4157cdb1 20
7eba5e1f 21type VideoFetchType = 'all' | 'only-video' | 'only-video-with-rights' | 'id' | 'none' | 'only-immutable-attributes'
4157cdb1 22
453e83ea 23function fetchVideo (id: number | string, fetchType: 'all', userId?: number): Bluebird<MVideoFullLight>
7eba5e1f 24function fetchVideo (id: number | string, fetchType: 'only-immutable-attributes'): Bluebird<MVideoImmutable>
453e83ea
C
25function fetchVideo (id: number | string, fetchType: 'only-video', userId?: number): Bluebird<MVideoThumbnail>
26function fetchVideo (id: number | string, fetchType: 'only-video-with-rights', userId?: number): Bluebird<MVideoWithRights>
27function fetchVideo (id: number | string, fetchType: 'id' | 'none', userId?: number): Bluebird<MVideoIdThumbnail>
28function fetchVideo (
29 id: number | string,
30 fetchType: VideoFetchType,
31 userId?: number
7eba5e1f 32): Bluebird<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail | MVideoImmutable>
453e83ea
C
33function fetchVideo (
34 id: number | string,
35 fetchType: VideoFetchType,
36 userId?: number
7eba5e1f 37): Bluebird<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail | MVideoImmutable> {
6e46de09 38 if (fetchType === 'all') return VideoModel.loadAndPopulateAccountAndServerAndTags(id, undefined, userId)
4157cdb1 39
7eba5e1f
C
40 if (fetchType === 'only-immutable-attributes') return VideoModel.loadImmutableAttributes(id)
41
09209296
C
42 if (fetchType === 'only-video-with-rights') return VideoModel.loadWithRights(id)
43
4157cdb1
C
44 if (fetchType === 'only-video') return VideoModel.load(id)
45
46 if (fetchType === 'id' || fetchType === 'none') return VideoModel.loadOnlyId(id)
47}
48
943e5193 49type VideoFetchByUrlType = 'all' | 'only-video' | 'only-immutable-attributes'
453e83ea 50
0283eaac 51function fetchVideoByUrl (url: string, fetchType: 'all'): Bluebird<MVideoAccountLightBlacklistAllFiles>
943e5193 52function fetchVideoByUrl (url: string, fetchType: 'only-immutable-attributes'): Bluebird<MVideoImmutable>
453e83ea 53function fetchVideoByUrl (url: string, fetchType: 'only-video'): Bluebird<MVideoThumbnail>
943e5193
C
54function fetchVideoByUrl (
55 url: string,
56 fetchType: VideoFetchByUrlType
57): Bluebird<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable>
58function fetchVideoByUrl (
59 url: string,
60 fetchType: VideoFetchByUrlType
61): Bluebird<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable> {
4157cdb1
C
62 if (fetchType === 'all') return VideoModel.loadByUrlAndPopulateAccount(url)
63
943e5193
C
64 if (fetchType === 'only-immutable-attributes') return VideoModel.loadByUrlImmutableAttributes(url)
65
4157cdb1
C
66 if (fetchType === 'only-video') return VideoModel.loadByUrl(url)
67}
68
0283eaac
C
69function getVideoWithAttributes (res: Response) {
70 return res.locals.videoAll || res.locals.onlyVideo || res.locals.onlyVideoWithRights
71}
72
8dc8a34e
C
73function 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
94function extractVideo (videoOrPlaylist: MVideo | MStreamingPlaylistVideo) {
95 return isStreamingPlaylist(videoOrPlaylist)
96 ? videoOrPlaylist.Video
97 : videoOrPlaylist
98}
99
3092e9bb
LB
100function 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
68e70a74
C
107function 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
3092e9bb
LB
114function getPrivaciesForFederation () {
115 return (CONFIG.FEDERATION.VIDEOS.FEDERATE_UNLISTED === true)
116 ? [ { privacy: VideoPrivacy.PUBLIC }, { privacy: VideoPrivacy.UNLISTED } ]
117 : [ { privacy: VideoPrivacy.PUBLIC } ]
118}
119
30bc55c8
C
120function 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
4157cdb1
C
128export {
129 VideoFetchType,
130 VideoFetchByUrlType,
131 fetchVideo,
0283eaac 132 getVideoWithAttributes,
8dc8a34e
C
133 fetchVideoByUrl,
134 addOptimizeOrMergeAudioJob,
3092e9bb 135 extractVideo,
30bc55c8 136 getExtFromMimetype,
68e70a74 137 isStateForFederation,
3092e9bb
LB
138 isPrivacyForFederation,
139 getPrivaciesForFederation
4157cdb1 140}