]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/video/modals/video-download.component.ts
Add internal privacy mode
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / modals / video-download.component.ts
index d6d10d29ed86b7d5f7303957fdc6e9894fd7d628..71274008648b92c858dba599754091ae89968f65 100644 (file)
@@ -1,8 +1,9 @@
 import { Component, ElementRef, ViewChild } from '@angular/core'
 import { VideoDetails } from '../../../shared/video/video-details.model'
-import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
+import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap'
 import { I18n } from '@ngx-translate/i18n-polyfill'
-import { Notifier } from '@app/core'
+import { AuthService, Notifier } from '@app/core'
+import { VideoPrivacy } from '@shared/models'
 
 @Component({
   selector: 'my-video-download',
@@ -10,27 +11,33 @@ import { Notifier } from '@app/core'
   styleUrls: [ './video-download.component.scss' ]
 })
 export class VideoDownloadComponent {
-  @ViewChild('modal') modal: ElementRef
+  @ViewChild('modal', { static: true }) modal: ElementRef
 
-  downloadType: 'direct' | 'torrent' | 'magnet' = 'torrent'
+  downloadType: 'direct' | 'torrent' = 'torrent'
   resolutionId: number | string = -1
 
   video: VideoDetails
+  activeModal: NgbActiveModal
 
   constructor (
     private notifier: Notifier,
     private modalService: NgbModal,
+    private auth: AuthService,
     private i18n: I18n
   ) { }
 
+  getVideoFiles () {
+    if (!this.video) return []
+
+    return this.video.getFiles()
+  }
+
   show (video: VideoDetails) {
     this.video = video
 
-    const m = this.modalService.open(this.modal)
-    m.result.then(() => this.onClose())
-     .catch(() => this.onClose())
+    this.activeModal = this.modalService.open(this.modal)
 
-    this.resolutionId = this.video.files[0].resolution.id
+    this.resolutionId = this.getVideoFiles()[0].resolution.id
   }
 
   onClose () {
@@ -39,27 +46,29 @@ export class VideoDownloadComponent {
 
   download () {
     window.location.assign(this.getLink())
+    this.activeModal.close()
   }
 
   getLink () {
     // HTML select send us a string, so convert it to a number
     this.resolutionId = parseInt(this.resolutionId.toString(), 10)
 
-    const file = this.video.files.find(f => f.resolution.id === this.resolutionId)
+    const file = this.getVideoFiles().find(f => f.resolution.id === this.resolutionId)
     if (!file) {
       console.error('Could not find file with resolution %d.', this.resolutionId)
       return
     }
 
+    const suffix = this.video.privacy.id === VideoPrivacy.PRIVATE || this.video.privacy.id === VideoPrivacy.INTERNAL
+      ? '?access_token=' + this.auth.getAccessToken()
+      : ''
+
     switch (this.downloadType) {
       case 'direct':
-        return file.fileDownloadUrl
+        return file.fileDownloadUrl + suffix
 
       case 'torrent':
-        return file.torrentDownloadUrl
-
-      case 'magnet':
-        return file.magnetUri
+        return file.torrentDownloadUrl + suffix
     }
   }