import { mapValues, pick } from 'lodash-es'
+import { firstValueFrom } from 'rxjs'
import { tap } from 'rxjs/operators'
import { Component, ElementRef, Inject, LOCALE_ID, ViewChild } from '@angular/core'
-import { AuthService, HooksService, Notifier } from '@app/core'
+import { HooksService } from '@app/core'
import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
+import { logger } from '@root-helpers/logger'
+import { videoRequiresAuth } from '@root-helpers/video'
import { VideoCaption, VideoFile, VideoPrivacy } from '@shared/models'
-import { BytesPipe, NumberFormatterPipe, VideoDetails, VideoService } from '../shared-main'
+import { BytesPipe, NumberFormatterPipe, VideoDetails, VideoFileTokenService, VideoService } from '../shared-main'
type DownloadType = 'video' | 'subtitles'
-type FileMetadata = { [key: string]: { label: string, value: string }}
+type FileMetadata = { [key: string]: { label: string, value: string } }
@Component({
selector: 'my-video-download',
type: DownloadType = 'video'
+ videoFileToken: string
+
private activeModal: NgbModalRef
private bytesPipe: BytesPipe
constructor (
@Inject(LOCALE_ID) private localeId: string,
- private notifier: Notifier,
private modalService: NgbModal,
private videoService: VideoService,
- private auth: AuthService,
+ private videoFileTokenService: VideoFileTokenService,
private hooks: HooksService
) {
this.bytesPipe = new BytesPipe()
}
show (video: VideoDetails, videoCaptions?: VideoCaption[]) {
+ this.videoFileToken = undefined
+
this.video = video
this.videoCaptions = videoCaptions
this.subtitleLanguageId = this.videoCaptions[0].language.id
}
+ if (videoRequiresAuth(this.video)) {
+ this.videoFileTokenService.getVideoFileToken(this.video.uuid)
+ .subscribe(({ token }) => this.videoFileToken = token)
+ }
+
this.activeModal.shown.subscribe(() => {
this.hooks.runAction('action:modal.video-download.shown', 'common')
})
.find(f => f.resolution.id === this.resolutionId)
if (!file) {
- console.error('Could not find file with resolution %d.', this.resolutionId)
+ logger.error(`Could not find file with resolution ${this.resolutionId}`)
return undefined
}
if (!file) return ''
const suffix = this.isConfidentialVideo()
- ? '?access_token=' + this.auth.getAccessToken()
+ ? '?videoFileToken=' + this.videoFileToken
: ''
switch (this.downloadType) {
.find(c => c.language.id === this.subtitleLanguageId)
if (!caption) {
- console.error('Cannot find caption %s.', this.subtitleLanguageId)
+ logger.error(`Cannot find caption ${this.subtitleLanguageId}`)
return undefined
}
return this.video.privacy.id === VideoPrivacy.PRIVATE || this.video.privacy.id === VideoPrivacy.INTERNAL
}
- activateCopiedMessage () {
- this.notifier.success($localize`Copied`)
- }
-
switchToType (type: DownloadType) {
this.type = type
}
private getMetadataFormat (format: any) {
const keyToTranslateFunction = {
- 'encoder': (value: string) => ({ label: $localize`Encoder`, value }),
- 'format_long_name': (value: string) => ({ label: $localize`Format name`, value }),
- 'size': (value: number) => ({ label: $localize`Size`, value: this.bytesPipe.transform(value, 2) }),
- 'bit_rate': (value: number) => ({
+ encoder: (value: string) => ({ label: $localize`Encoder`, value }),
+ format_long_name: (value: string) => ({ label: $localize`Format name`, value }),
+ size: (value: number) => ({ label: $localize`Size`, value: this.bytesPipe.transform(value, 2) }),
+ bit_rate: (value: number) => ({
label: $localize`Bitrate`,
value: `${this.numbersPipe.transform(value)}bps`
})
if (!stream) return undefined
let keyToTranslateFunction = {
- 'codec_long_name': (value: string) => ({ label: $localize`Codec`, value }),
- 'profile': (value: string) => ({ label: $localize`Profile`, value }),
- 'bit_rate': (value: number) => ({
+ codec_long_name: (value: string) => ({ label: $localize`Codec`, value }),
+ profile: (value: string) => ({ label: $localize`Profile`, value }),
+ bit_rate: (value: number) => ({
label: $localize`Bitrate`,
value: `${this.numbersPipe.transform(value)}bps`
})
if (type === 'video') {
keyToTranslateFunction = Object.assign(keyToTranslateFunction, {
- 'width': (value: number) => ({ label: $localize`Resolution`, value: `${value}x${stream.height}` }),
- 'display_aspect_ratio': (value: string) => ({ label: $localize`Aspect ratio`, value }),
- 'avg_frame_rate': (value: string) => ({ label: $localize`Average frame rate`, value }),
- 'pix_fmt': (value: string) => ({ label: $localize`Pixel format`, value })
+ width: (value: number) => ({ label: $localize`Resolution`, value: `${value}x${stream.height}` }),
+ display_aspect_ratio: (value: string) => ({ label: $localize`Aspect ratio`, value }),
+ avg_frame_rate: (value: string) => ({ label: $localize`Average frame rate`, value }),
+ pix_fmt: (value: string) => ({ label: $localize`Pixel format`, value })
})
} else {
keyToTranslateFunction = Object.assign(keyToTranslateFunction, {
- 'sample_rate': (value: number) => ({ label: $localize`Sample rate`, value }),
- 'channel_layout': (value: number) => ({ label: $localize`Channel Layout`, value })
+ sample_rate: (value: number) => ({ label: $localize`Sample rate`, value }),
+ channel_layout: (value: number) => ({ label: $localize`Channel Layout`, value })
})
}
const observable = this.videoService.getVideoFileMetadata(file.metadataUrl)
.pipe(tap(res => file.metadata = res))
- return observable.toPromise()
+ return firstValueFrom(observable)
}
}