Add internal privacy mode
[oweals/peertube.git] / client / src / app / shared / video / modals / video-download.component.ts
1 import { Component, ElementRef, ViewChild } from '@angular/core'
2 import { VideoDetails } from '../../../shared/video/video-details.model'
3 import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap'
4 import { I18n } from '@ngx-translate/i18n-polyfill'
5 import { AuthService, Notifier } from '@app/core'
6 import { VideoPrivacy } from '@shared/models'
7
8 @Component({
9   selector: 'my-video-download',
10   templateUrl: './video-download.component.html',
11   styleUrls: [ './video-download.component.scss' ]
12 })
13 export class VideoDownloadComponent {
14   @ViewChild('modal', { static: true }) modal: ElementRef
15
16   downloadType: 'direct' | 'torrent' = 'torrent'
17   resolutionId: number | string = -1
18
19   video: VideoDetails
20   activeModal: NgbActiveModal
21
22   constructor (
23     private notifier: Notifier,
24     private modalService: NgbModal,
25     private auth: AuthService,
26     private i18n: I18n
27   ) { }
28
29   getVideoFiles () {
30     if (!this.video) return []
31
32     return this.video.getFiles()
33   }
34
35   show (video: VideoDetails) {
36     this.video = video
37
38     this.activeModal = this.modalService.open(this.modal)
39
40     this.resolutionId = this.getVideoFiles()[0].resolution.id
41   }
42
43   onClose () {
44     this.video = undefined
45   }
46
47   download () {
48     window.location.assign(this.getLink())
49     this.activeModal.close()
50   }
51
52   getLink () {
53     // HTML select send us a string, so convert it to a number
54     this.resolutionId = parseInt(this.resolutionId.toString(), 10)
55
56     const file = this.getVideoFiles().find(f => f.resolution.id === this.resolutionId)
57     if (!file) {
58       console.error('Could not find file with resolution %d.', this.resolutionId)
59       return
60     }
61
62     const suffix = this.video.privacy.id === VideoPrivacy.PRIVATE || this.video.privacy.id === VideoPrivacy.INTERNAL
63       ? '?access_token=' + this.auth.getAccessToken()
64       : ''
65
66     switch (this.downloadType) {
67       case 'direct':
68         return file.fileDownloadUrl + suffix
69
70       case 'torrent':
71         return file.torrentDownloadUrl + suffix
72     }
73   }
74
75   activateCopiedMessage () {
76     this.notifier.success(this.i18n('Copied'))
77   }
78 }