]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/shared-video-miniature/video-download.component.ts
Fix stats anchor link
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-video-miniature / video-download.component.ts
index 355ce8be3bd04bdd81082cd2aede0e028c9dfeb7..4135542dc59c5e9f01b0f4b72710777c78ac865a 100644 (file)
@@ -1,13 +1,16 @@
 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',
@@ -30,6 +33,8 @@ export class VideoDownloadComponent {
 
   type: DownloadType = 'video'
 
+  videoFileToken: string
+
   private activeModal: NgbModalRef
 
   private bytesPipe: BytesPipe
@@ -40,10 +45,9 @@ export class VideoDownloadComponent {
 
   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()
@@ -69,6 +73,8 @@ export class VideoDownloadComponent {
   }
 
   show (video: VideoDetails, videoCaptions?: VideoCaption[]) {
+    this.videoFileToken = undefined
+
     this.video = video
     this.videoCaptions = videoCaptions
 
@@ -82,6 +88,11 @@ export class VideoDownloadComponent {
       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')
     })
@@ -141,7 +152,7 @@ export class VideoDownloadComponent {
                      .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
     }
 
@@ -153,7 +164,7 @@ export class VideoDownloadComponent {
     if (!file) return ''
 
     const suffix = this.isConfidentialVideo()
-      ? '?access_token=' + this.auth.getAccessToken()
+      ? '?videoFileToken=' + this.videoFileToken
       : ''
 
     switch (this.downloadType) {
@@ -174,7 +185,7 @@ export class VideoDownloadComponent {
                         .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
     }
 
@@ -192,10 +203,6 @@ export class VideoDownloadComponent {
     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
   }
@@ -209,10 +216,10 @@ export class VideoDownloadComponent {
 
   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`
       })
@@ -233,9 +240,9 @@ export class VideoDownloadComponent {
     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`
       })
@@ -243,15 +250,15 @@ export class VideoDownloadComponent {
 
     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 })
       })
     }
 
@@ -265,6 +272,6 @@ export class VideoDownloadComponent {
     const observable = this.videoService.getVideoFileMetadata(file.metadataUrl)
       .pipe(tap(res => file.metadata = res))
 
-    return observable.toPromise()
+    return firstValueFrom(observable)
   }
 }