]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-video-miniature/video-download.component.ts
Handle rejected follows in client
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-video-miniature / video-download.component.ts
CommitLineData
8319d6ae 1import { mapValues, pick } from 'lodash-es'
1378c0d3 2import { firstValueFrom } from 'rxjs'
4bc45da3 3import { tap } from 'rxjs/operators'
68018040 4import { Component, ElementRef, Inject, LOCALE_ID, ViewChild } from '@angular/core'
4bc45da3
C
5import { AuthService, HooksService, Notifier } from '@app/core'
6import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
42b40636 7import { logger } from '@root-helpers/logger'
67ed6552 8import { VideoCaption, VideoFile, VideoPrivacy } from '@shared/models'
94676e63 9import { BytesPipe, NumberFormatterPipe, VideoDetails, VideoService } from '../shared-main'
8ba9c205
RK
10
11type DownloadType = 'video' | 'subtitles'
8319d6ae 12type FileMetadata = { [key: string]: { label: string, value: string }}
cf02fbfb
C
13
14@Component({
a96aed15
C
15 selector: 'my-video-download',
16 templateUrl: './video-download.component.html',
0727cab0 17 styleUrls: [ './video-download.component.scss' ]
cf02fbfb 18})
3a0fb65c 19export class VideoDownloadComponent {
f36da21e 20 @ViewChild('modal', { static: true }) modal: ElementRef
cf02fbfb 21
7bd455cb 22 downloadType: 'direct' | 'torrent' = 'direct'
262f8ff6 23
09700934 24 resolutionId: number | string = -1
8ba9c205 25 subtitleLanguageId: string
5f0805d3 26
8319d6ae
RK
27 videoFileMetadataFormat: FileMetadata
28 videoFileMetadataVideoStream: FileMetadata | undefined
29 videoFileMetadataAudioStream: FileMetadata | undefined
3a0fb65c 30
cf3c3624 31 isAdvancedCustomizationCollapsed = true
32
8ba9c205
RK
33 type: DownloadType = 'video'
34
262f8ff6
C
35 private activeModal: NgbModalRef
36
8319d6ae
RK
37 private bytesPipe: BytesPipe
38 private numbersPipe: NumberFormatterPipe
39
262f8ff6
C
40 private video: VideoDetails
41 private videoCaptions: VideoCaption[]
42
bb5d7428 43 constructor (
68018040 44 @Inject(LOCALE_ID) private localeId: string,
f8b2c1b4 45 private notifier: Notifier,
bb5d7428 46 private modalService: NgbModal,
8319d6ae 47 private videoService: VideoService,
4bc45da3
C
48 private auth: AuthService,
49 private hooks: HooksService
8319d6ae
RK
50 ) {
51 this.bytesPipe = new BytesPipe()
68018040 52 this.numbersPipe = new NumberFormatterPipe(this.localeId)
8319d6ae 53 }
cf02fbfb 54
8ba9c205
RK
55 get typeText () {
56 return this.type === 'video'
66357162
C
57 ? $localize`video`
58 : $localize`subtitles`
8ba9c205
RK
59 }
60
5a71acd2
C
61 getVideoFiles () {
62 if (!this.video) return []
63
64 return this.video.getFiles()
65 }
66
262f8ff6
C
67 getCaptions () {
68 if (!this.videoCaptions) return []
69
70 return this.videoCaptions
71 }
72
8ba9c205 73 show (video: VideoDetails, videoCaptions?: VideoCaption[]) {
3a0fb65c 74 this.video = video
262f8ff6 75 this.videoCaptions = videoCaptions
3a0fb65c 76
24e7916c 77 this.activeModal = this.modalService.open(this.modal, { centered: true })
3a0fb65c 78
262f8ff6
C
79 if (this.hasFiles()) {
80 this.onResolutionIdChange(this.getVideoFiles()[0].resolution.id)
81 }
4bc45da3 82
262f8ff6
C
83 if (this.hasCaptions()) {
84 this.subtitleLanguageId = this.videoCaptions[0].language.id
85 }
4bc45da3
C
86
87 this.activeModal.shown.subscribe(() => {
88 this.hooks.runAction('action:modal.video-download.shown', 'common')
89 })
5f0805d3
C
90 }
91
3a0fb65c
C
92 onClose () {
93 this.video = undefined
8ba9c205 94 this.videoCaptions = undefined
cf02fbfb 95 }
5f0805d3
C
96
97 download () {
bb5d7428 98 window.location.assign(this.getLink())
262f8ff6 99
11b3f14c 100 this.activeModal.close()
bb5d7428
RK
101 }
102
103 getLink () {
8ba9c205 104 return this.type === 'subtitles' && this.videoCaptions
262f8ff6 105 ? this.getCaptionLink()
8319d6ae 106 : this.getVideoFileLink()
8ba9c205
RK
107 }
108
cf3c3624 109 async onResolutionIdChange (resolutionId: number) {
110 this.resolutionId = resolutionId
8319d6ae 111
262f8ff6
C
112 const videoFile = this.getVideoFile()
113
114 if (!videoFile.metadata) {
115 if (!videoFile.metadataUrl) return
cf3c3624 116
262f8ff6 117 await this.hydrateMetadataFromMetadataUrl(videoFile)
cf3c3624 118 }
8319d6ae 119
262f8ff6
C
120 if (!videoFile.metadata) return
121
122 this.videoFileMetadataFormat = videoFile
123 ? this.getMetadataFormat(videoFile.metadata.format)
8319d6ae 124 : undefined
262f8ff6
C
125 this.videoFileMetadataVideoStream = videoFile
126 ? this.getMetadataStream(videoFile.metadata.streams, 'video')
8319d6ae 127 : undefined
262f8ff6
C
128 this.videoFileMetadataAudioStream = videoFile
129 ? this.getMetadataStream(videoFile.metadata.streams, 'audio')
8319d6ae
RK
130 : undefined
131 }
132
262f8ff6
C
133 onSubtitleIdChange (subtitleId: string) {
134 this.subtitleLanguageId = subtitleId
135 }
136
137 hasFiles () {
138 return this.getVideoFiles().length !== 0
139 }
140
8319d6ae 141 getVideoFile () {
262f8ff6
C
142 const file = this.getVideoFiles()
143 .find(f => f.resolution.id === this.resolutionId)
144
5f0805d3 145 if (!file) {
42b40636 146 logger.error(`Could not find file with resolution ${this.resolutionId}`)
262f8ff6 147 return undefined
5f0805d3 148 }
262f8ff6 149
8319d6ae
RK
150 return file
151 }
152
153 getVideoFileLink () {
262f8ff6
C
154 const file = this.getVideoFile()
155 if (!file) return ''
5f0805d3 156
51294901 157 const suffix = this.isConfidentialVideo()
eccf70f0
C
158 ? '?access_token=' + this.auth.getAccessToken()
159 : ''
160
3a0fb65c
C
161 switch (this.downloadType) {
162 case 'direct':
eccf70f0 163 return file.fileDownloadUrl + suffix
3a0fb65c
C
164
165 case 'torrent':
eccf70f0 166 return file.torrentDownloadUrl + suffix
3a0fb65c 167 }
bb5d7428
RK
168 }
169
262f8ff6
C
170 hasCaptions () {
171 return this.getCaptions().length !== 0
172 }
173
174 getCaption () {
175 const caption = this.getCaptions()
176 .find(c => c.language.id === this.subtitleLanguageId)
177
178 if (!caption) {
42b40636 179 logger.error(`Cannot find caption ${this.subtitleLanguageId}`)
262f8ff6
C
180 return undefined
181 }
182
183 return caption
51294901
C
184 }
185
262f8ff6
C
186 getCaptionLink () {
187 const caption = this.getCaption()
188 if (!caption) return ''
189
190 return window.location.origin + caption.captionPath
191 }
192
193 isConfidentialVideo () {
194 return this.video.privacy.id === VideoPrivacy.PRIVATE || this.video.privacy.id === VideoPrivacy.INTERNAL
8ba9c205
RK
195 }
196
8ba9c205
RK
197 switchToType (type: DownloadType) {
198 this.type = type
199 }
8319d6ae 200
262f8ff6
C
201 getFileMetadata () {
202 const file = this.getVideoFile()
203 if (!file) return undefined
204
205 return file.metadata
206 }
207
208 private getMetadataFormat (format: any) {
8319d6ae 209 const keyToTranslateFunction = {
9df52d66
C
210 encoder: (value: string) => ({ label: $localize`Encoder`, value }),
211 format_long_name: (value: string) => ({ label: $localize`Format name`, value }),
212 size: (value: number) => ({ label: $localize`Size`, value: this.bytesPipe.transform(value, 2) }),
213 bit_rate: (value: number) => ({
66357162 214 label: $localize`Bitrate`,
8319d6ae
RK
215 value: `${this.numbersPipe.transform(value)}bps`
216 })
217 }
218
219 // flattening format
220 const sanitizedFormat = Object.assign(format, format.tags)
221 delete sanitizedFormat.tags
222
223 return mapValues(
224 pick(sanitizedFormat, Object.keys(keyToTranslateFunction)),
225 (val, key) => keyToTranslateFunction[key](val)
226 )
227 }
228
262f8ff6 229 private getMetadataStream (streams: any[], type: 'video' | 'audio') {
8319d6ae
RK
230 const stream = streams.find(s => s.codec_type === type)
231 if (!stream) return undefined
232
233 let keyToTranslateFunction = {
9df52d66
C
234 codec_long_name: (value: string) => ({ label: $localize`Codec`, value }),
235 profile: (value: string) => ({ label: $localize`Profile`, value }),
236 bit_rate: (value: number) => ({
66357162 237 label: $localize`Bitrate`,
8319d6ae
RK
238 value: `${this.numbersPipe.transform(value)}bps`
239 })
240 }
241
242 if (type === 'video') {
243 keyToTranslateFunction = Object.assign(keyToTranslateFunction, {
9df52d66
C
244 width: (value: number) => ({ label: $localize`Resolution`, value: `${value}x${stream.height}` }),
245 display_aspect_ratio: (value: string) => ({ label: $localize`Aspect ratio`, value }),
246 avg_frame_rate: (value: string) => ({ label: $localize`Average frame rate`, value }),
247 pix_fmt: (value: string) => ({ label: $localize`Pixel format`, value })
8319d6ae
RK
248 })
249 } else {
250 keyToTranslateFunction = Object.assign(keyToTranslateFunction, {
9df52d66
C
251 sample_rate: (value: number) => ({ label: $localize`Sample rate`, value }),
252 channel_layout: (value: number) => ({ label: $localize`Channel Layout`, value })
8319d6ae
RK
253 })
254 }
255
256 return mapValues(
257 pick(stream, Object.keys(keyToTranslateFunction)),
258 (val, key) => keyToTranslateFunction[key](val)
259 )
260 }
261
262 private hydrateMetadataFromMetadataUrl (file: VideoFile) {
263 const observable = this.videoService.getVideoFileMetadata(file.metadataUrl)
4bc45da3 264 .pipe(tap(res => file.metadata = res))
583eb04b 265
1378c0d3 266 return firstValueFrom(observable)
8319d6ae 267 }
cf02fbfb 268}