]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-video-miniature/video-download.component.ts
Add warning in download modal if needed
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-video-miniature / video-download.component.ts
CommitLineData
8319d6ae 1import { mapValues, pick } from 'lodash-es'
68018040 2import { Component, ElementRef, Inject, LOCALE_ID, ViewChild } from '@angular/core'
67ed6552
C
3import { AuthService, Notifier } from '@app/core'
4import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap'
67ed6552 5import { VideoCaption, VideoFile, VideoPrivacy } from '@shared/models'
94676e63 6import { BytesPipe, NumberFormatterPipe, VideoDetails, VideoService } from '../shared-main'
8ba9c205
RK
7
8type DownloadType = 'video' | 'subtitles'
8319d6ae 9type FileMetadata = { [key: string]: { label: string, value: string }}
cf02fbfb
C
10
11@Component({
a96aed15
C
12 selector: 'my-video-download',
13 templateUrl: './video-download.component.html',
0727cab0 14 styleUrls: [ './video-download.component.scss' ]
cf02fbfb 15})
3a0fb65c 16export class VideoDownloadComponent {
f36da21e 17 @ViewChild('modal', { static: true }) modal: ElementRef
cf02fbfb 18
7c51916a 19 downloadType: 'direct' | 'torrent' = 'torrent'
09700934 20 resolutionId: number | string = -1
8ba9c205 21 subtitleLanguageId: string
5f0805d3 22
8dfceec4 23 video: VideoDetails
8319d6ae
RK
24 videoFile: VideoFile
25 videoFileMetadataFormat: FileMetadata
26 videoFileMetadataVideoStream: FileMetadata | undefined
27 videoFileMetadataAudioStream: FileMetadata | undefined
8ba9c205 28 videoCaptions: VideoCaption[]
11b3f14c 29 activeModal: NgbActiveModal
3a0fb65c 30
8ba9c205
RK
31 type: DownloadType = 'video'
32
8319d6ae
RK
33 private bytesPipe: BytesPipe
34 private numbersPipe: NumberFormatterPipe
35
bb5d7428 36 constructor (
68018040 37 @Inject(LOCALE_ID) private localeId: string,
f8b2c1b4 38 private notifier: Notifier,
bb5d7428 39 private modalService: NgbModal,
8319d6ae 40 private videoService: VideoService,
66357162 41 private auth: AuthService
8319d6ae
RK
42 ) {
43 this.bytesPipe = new BytesPipe()
68018040 44 this.numbersPipe = new NumberFormatterPipe(this.localeId)
8319d6ae 45 }
cf02fbfb 46
8ba9c205
RK
47 get typeText () {
48 return this.type === 'video'
66357162
C
49 ? $localize`video`
50 : $localize`subtitles`
8ba9c205
RK
51 }
52
5a71acd2
C
53 getVideoFiles () {
54 if (!this.video) return []
55
56 return this.video.getFiles()
57 }
58
8ba9c205 59 show (video: VideoDetails, videoCaptions?: VideoCaption[]) {
3a0fb65c 60 this.video = video
8ba9c205 61 this.videoCaptions = videoCaptions && videoCaptions.length ? videoCaptions : undefined
3a0fb65c 62
24e7916c 63 this.activeModal = this.modalService.open(this.modal, { centered: true })
3a0fb65c 64
5a71acd2 65 this.resolutionId = this.getVideoFiles()[0].resolution.id
8319d6ae 66 this.onResolutionIdChange()
8ba9c205 67 if (this.videoCaptions) this.subtitleLanguageId = this.videoCaptions[0].language.id
5f0805d3
C
68 }
69
3a0fb65c
C
70 onClose () {
71 this.video = undefined
8ba9c205 72 this.videoCaptions = undefined
cf02fbfb 73 }
5f0805d3
C
74
75 download () {
bb5d7428 76 window.location.assign(this.getLink())
11b3f14c 77 this.activeModal.close()
bb5d7428
RK
78 }
79
80 getLink () {
8ba9c205
RK
81 return this.type === 'subtitles' && this.videoCaptions
82 ? this.getSubtitlesLink()
8319d6ae 83 : this.getVideoFileLink()
8ba9c205
RK
84 }
85
8319d6ae
RK
86 async onResolutionIdChange () {
87 this.videoFile = this.getVideoFile()
88 if (this.videoFile.metadata || !this.videoFile.metadataUrl) return
89
90 await this.hydrateMetadataFromMetadataUrl(this.videoFile)
91
92 this.videoFileMetadataFormat = this.videoFile
93 ? this.getMetadataFormat(this.videoFile.metadata.format)
94 : undefined
95 this.videoFileMetadataVideoStream = this.videoFile
96 ? this.getMetadataStream(this.videoFile.metadata.streams, 'video')
97 : undefined
98 this.videoFileMetadataAudioStream = this.videoFile
99 ? this.getMetadataStream(this.videoFile.metadata.streams, 'audio')
100 : undefined
101 }
102
103 getVideoFile () {
00336945 104 // HTML select send us a string, so convert it to a number
09700934 105 this.resolutionId = parseInt(this.resolutionId.toString(), 10)
00336945 106
5a71acd2 107 const file = this.getVideoFiles().find(f => f.resolution.id === this.resolutionId)
5f0805d3 108 if (!file) {
09700934 109 console.error('Could not find file with resolution %d.', this.resolutionId)
5f0805d3
C
110 return
111 }
8319d6ae
RK
112 return file
113 }
114
115 getVideoFileLink () {
116 const file = this.videoFile
117 if (!file) return
5f0805d3 118
51294901 119 const suffix = this.isConfidentialVideo()
eccf70f0
C
120 ? '?access_token=' + this.auth.getAccessToken()
121 : ''
122
3a0fb65c
C
123 switch (this.downloadType) {
124 case 'direct':
eccf70f0 125 return file.fileDownloadUrl + suffix
3a0fb65c
C
126
127 case 'torrent':
eccf70f0 128 return file.torrentDownloadUrl + suffix
3a0fb65c 129 }
bb5d7428
RK
130 }
131
51294901
C
132 isConfidentialVideo () {
133 return this.video.privacy.id === VideoPrivacy.PRIVATE || this.video.privacy.id === VideoPrivacy.INTERNAL
134 }
135
8ba9c205
RK
136 getSubtitlesLink () {
137 return window.location.origin + this.videoCaptions.find(caption => caption.language.id === this.subtitleLanguageId).captionPath
138 }
139
bb5d7428 140 activateCopiedMessage () {
66357162 141 this.notifier.success($localize`Copied`)
5f0805d3 142 }
8ba9c205
RK
143
144 switchToType (type: DownloadType) {
145 this.type = type
146 }
8319d6ae 147
583eb04b 148 getMetadataFormat (format: any) {
8319d6ae 149 const keyToTranslateFunction = {
66357162
C
150 'encoder': (value: string) => ({ label: $localize`Encoder`, value }),
151 'format_long_name': (value: string) => ({ label: $localize`Format name`, value }),
152 'size': (value: number) => ({ label: $localize`Size`, value: this.bytesPipe.transform(value, 2) }),
8319d6ae 153 'bit_rate': (value: number) => ({
66357162 154 label: $localize`Bitrate`,
8319d6ae
RK
155 value: `${this.numbersPipe.transform(value)}bps`
156 })
157 }
158
159 // flattening format
160 const sanitizedFormat = Object.assign(format, format.tags)
161 delete sanitizedFormat.tags
162
163 return mapValues(
164 pick(sanitizedFormat, Object.keys(keyToTranslateFunction)),
165 (val, key) => keyToTranslateFunction[key](val)
166 )
167 }
168
583eb04b 169 getMetadataStream (streams: any[], type: 'video' | 'audio') {
8319d6ae
RK
170 const stream = streams.find(s => s.codec_type === type)
171 if (!stream) return undefined
172
173 let keyToTranslateFunction = {
66357162
C
174 'codec_long_name': (value: string) => ({ label: $localize`Codec`, value }),
175 'profile': (value: string) => ({ label: $localize`Profile`, value }),
8319d6ae 176 'bit_rate': (value: number) => ({
66357162 177 label: $localize`Bitrate`,
8319d6ae
RK
178 value: `${this.numbersPipe.transform(value)}bps`
179 })
180 }
181
182 if (type === 'video') {
183 keyToTranslateFunction = Object.assign(keyToTranslateFunction, {
66357162
C
184 'width': (value: number) => ({ label: $localize`Resolution`, value: `${value}x${stream.height}` }),
185 'display_aspect_ratio': (value: string) => ({ label: $localize`Aspect ratio`, value }),
186 'avg_frame_rate': (value: string) => ({ label: $localize`Average frame rate`, value }),
187 'pix_fmt': (value: string) => ({ label: $localize`Pixel format`, value })
8319d6ae
RK
188 })
189 } else {
190 keyToTranslateFunction = Object.assign(keyToTranslateFunction, {
66357162
C
191 'sample_rate': (value: number) => ({ label: $localize`Sample rate`, value }),
192 'channel_layout': (value: number) => ({ label: $localize`Channel Layout`, value })
8319d6ae
RK
193 })
194 }
195
196 return mapValues(
197 pick(stream, Object.keys(keyToTranslateFunction)),
198 (val, key) => keyToTranslateFunction[key](val)
199 )
200 }
201
202 private hydrateMetadataFromMetadataUrl (file: VideoFile) {
203 const observable = this.videoService.getVideoFileMetadata(file.metadataUrl)
204 observable.subscribe(res => file.metadata = res)
583eb04b 205
8319d6ae
RK
206 return observable.toPromise()
207 }
cf02fbfb 208}