]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/video.ts
Remove uneccessary details to link titles
[github/Chocobozzz/PeerTube.git] / server / helpers / video.ts
CommitLineData
4157cdb1 1import { VideoModel } from '../models/video/video'
453e83ea 2import * as Bluebird from 'bluebird'
0283eaac 3import {
8dc8a34e
C
4 isStreamingPlaylist,
5 MStreamingPlaylistVideo,
6 MVideo,
0283eaac 7 MVideoAccountLightBlacklistAllFiles,
8dc8a34e 8 MVideoFile,
0283eaac
C
9 MVideoFullLight,
10 MVideoIdThumbnail,
8dc8a34e 11 MVideoImmutable,
0283eaac 12 MVideoThumbnail,
8dc8a34e 13 MVideoWithRights
0283eaac 14} from '@server/typings/models'
453e83ea 15import { Response } from 'express'
8dc8a34e
C
16import { DEFAULT_AUDIO_RESOLUTION } from '@server/initializers/constants'
17import { JobQueue } from '@server/lib/job-queue'
3092e9bb
LB
18import { VideoPrivacy, VideoTranscodingPayload } from '@shared/models'
19import { CONFIG } from "@server/initializers/config"
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
107function getPrivaciesForFederation () {
108 return (CONFIG.FEDERATION.VIDEOS.FEDERATE_UNLISTED === true)
109 ? [ { privacy: VideoPrivacy.PUBLIC }, { privacy: VideoPrivacy.UNLISTED } ]
110 : [ { privacy: VideoPrivacy.PUBLIC } ]
111}
112
4157cdb1
C
113export {
114 VideoFetchType,
115 VideoFetchByUrlType,
116 fetchVideo,
0283eaac 117 getVideoWithAttributes,
8dc8a34e
C
118 fetchVideoByUrl,
119 addOptimizeOrMergeAudioJob,
3092e9bb
LB
120 extractVideo,
121 isPrivacyForFederation,
122 getPrivaciesForFederation
4157cdb1 123}