]>
Commit | Line | Data |
---|---|---|
4157cdb1 | 1 | import { VideoModel } from '../models/video/video' |
453e83ea | 2 | import * as Bluebird from 'bluebird' |
0283eaac | 3 | import { |
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 | 15 | import { Response } from 'express' |
8dc8a34e C |
16 | import { DEFAULT_AUDIO_RESOLUTION } from '@server/initializers/constants' |
17 | import { JobQueue } from '@server/lib/job-queue' | |
18 | import { VideoTranscodingPayload } from '@shared/models' | |
4157cdb1 | 19 | |
7eba5e1f | 20 | type VideoFetchType = 'all' | 'only-video' | 'only-video-with-rights' | 'id' | 'none' | 'only-immutable-attributes' |
4157cdb1 | 21 | |
453e83ea | 22 | function fetchVideo (id: number | string, fetchType: 'all', userId?: number): Bluebird<MVideoFullLight> |
7eba5e1f | 23 | function fetchVideo (id: number | string, fetchType: 'only-immutable-attributes'): Bluebird<MVideoImmutable> |
453e83ea C |
24 | function fetchVideo (id: number | string, fetchType: 'only-video', userId?: number): Bluebird<MVideoThumbnail> |
25 | function fetchVideo (id: number | string, fetchType: 'only-video-with-rights', userId?: number): Bluebird<MVideoWithRights> | |
26 | function fetchVideo (id: number | string, fetchType: 'id' | 'none', userId?: number): Bluebird<MVideoIdThumbnail> | |
27 | function fetchVideo ( | |
28 | id: number | string, | |
29 | fetchType: VideoFetchType, | |
30 | userId?: number | |
7eba5e1f | 31 | ): Bluebird<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail | MVideoImmutable> |
453e83ea C |
32 | function fetchVideo ( |
33 | id: number | string, | |
34 | fetchType: VideoFetchType, | |
35 | userId?: number | |
7eba5e1f | 36 | ): Bluebird<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail | MVideoImmutable> { |
6e46de09 | 37 | if (fetchType === 'all') return VideoModel.loadAndPopulateAccountAndServerAndTags(id, undefined, userId) |
4157cdb1 | 38 | |
7eba5e1f C |
39 | if (fetchType === 'only-immutable-attributes') return VideoModel.loadImmutableAttributes(id) |
40 | ||
09209296 C |
41 | if (fetchType === 'only-video-with-rights') return VideoModel.loadWithRights(id) |
42 | ||
4157cdb1 C |
43 | if (fetchType === 'only-video') return VideoModel.load(id) |
44 | ||
45 | if (fetchType === 'id' || fetchType === 'none') return VideoModel.loadOnlyId(id) | |
46 | } | |
47 | ||
943e5193 | 48 | type VideoFetchByUrlType = 'all' | 'only-video' | 'only-immutable-attributes' |
453e83ea | 49 | |
0283eaac | 50 | function fetchVideoByUrl (url: string, fetchType: 'all'): Bluebird<MVideoAccountLightBlacklistAllFiles> |
943e5193 | 51 | function fetchVideoByUrl (url: string, fetchType: 'only-immutable-attributes'): Bluebird<MVideoImmutable> |
453e83ea | 52 | function fetchVideoByUrl (url: string, fetchType: 'only-video'): Bluebird<MVideoThumbnail> |
943e5193 C |
53 | function fetchVideoByUrl ( |
54 | url: string, | |
55 | fetchType: VideoFetchByUrlType | |
56 | ): Bluebird<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable> | |
57 | function fetchVideoByUrl ( | |
58 | url: string, | |
59 | fetchType: VideoFetchByUrlType | |
60 | ): Bluebird<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable> { | |
4157cdb1 C |
61 | if (fetchType === 'all') return VideoModel.loadByUrlAndPopulateAccount(url) |
62 | ||
943e5193 C |
63 | if (fetchType === 'only-immutable-attributes') return VideoModel.loadByUrlImmutableAttributes(url) |
64 | ||
4157cdb1 C |
65 | if (fetchType === 'only-video') return VideoModel.loadByUrl(url) |
66 | } | |
67 | ||
0283eaac C |
68 | function getVideoWithAttributes (res: Response) { |
69 | return res.locals.videoAll || res.locals.onlyVideo || res.locals.onlyVideoWithRights | |
70 | } | |
71 | ||
8dc8a34e C |
72 | function addOptimizeOrMergeAudioJob (video: MVideo, videoFile: MVideoFile) { |
73 | let dataInput: VideoTranscodingPayload | |
74 | ||
75 | if (videoFile.isAudio()) { | |
76 | dataInput = { | |
77 | type: 'merge-audio' as 'merge-audio', | |
78 | resolution: DEFAULT_AUDIO_RESOLUTION, | |
79 | videoUUID: video.uuid, | |
80 | isNewVideo: true | |
81 | } | |
82 | } else { | |
83 | dataInput = { | |
84 | type: 'optimize' as 'optimize', | |
85 | videoUUID: video.uuid, | |
86 | isNewVideo: true | |
87 | } | |
88 | } | |
89 | ||
90 | return JobQueue.Instance.createJobWithPromise({ type: 'video-transcoding', payload: dataInput }) | |
91 | } | |
92 | ||
93 | function extractVideo (videoOrPlaylist: MVideo | MStreamingPlaylistVideo) { | |
94 | return isStreamingPlaylist(videoOrPlaylist) | |
95 | ? videoOrPlaylist.Video | |
96 | : videoOrPlaylist | |
97 | } | |
98 | ||
4157cdb1 C |
99 | export { |
100 | VideoFetchType, | |
101 | VideoFetchByUrlType, | |
102 | fetchVideo, | |
0283eaac | 103 | getVideoWithAttributes, |
8dc8a34e C |
104 | fetchVideoByUrl, |
105 | addOptimizeOrMergeAudioJob, | |
106 | extractVideo | |
4157cdb1 | 107 | } |