c1ceca26325974a1e43b10b101e52508b1e9d55f
[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, VideoCaption } from '@shared/models'
7
8 type DownloadType = 'video' | 'subtitles'
9
10 @Component({
11   selector: 'my-video-download',
12   templateUrl: './video-download.component.html',
13   styleUrls: [ './video-download.component.scss' ]
14 })
15 export class VideoDownloadComponent {
16   @ViewChild('modal', { static: true }) modal: ElementRef
17
18   downloadType: 'direct' | 'torrent' = 'torrent'
19   resolutionId: number | string = -1
20   subtitleLanguageId: string
21
22   video: VideoDetails
23   videoCaptions: VideoCaption[]
24   activeModal: NgbActiveModal
25
26   type: DownloadType = 'video'
27
28   constructor (
29     private notifier: Notifier,
30     private modalService: NgbModal,
31     private auth: AuthService,
32     private i18n: I18n
33   ) { }
34
35   get typeText () {
36     return this.type === 'video'
37       ? this.i18n('video')
38       : this.i18n('subtitles')
39   }
40
41   getVideoFiles () {
42     if (!this.video) return []
43
44     return this.video.getFiles()
45   }
46
47   show (video: VideoDetails, videoCaptions?: VideoCaption[]) {
48     this.video = video
49     this.videoCaptions = videoCaptions && videoCaptions.length ? videoCaptions : undefined
50
51     this.activeModal = this.modalService.open(this.modal)
52
53     this.resolutionId = this.getVideoFiles()[0].resolution.id
54     if (this.videoCaptions) this.subtitleLanguageId = this.videoCaptions[0].language.id
55   }
56
57   onClose () {
58     this.video = undefined
59     this.videoCaptions = undefined
60   }
61
62   download () {
63     window.location.assign(this.getLink())
64     this.activeModal.close()
65   }
66
67   getLink () {
68     return this.type === 'subtitles' && this.videoCaptions
69       ? this.getSubtitlesLink()
70       : this.getVideoLink()
71   }
72
73   getVideoLink () {
74     // HTML select send us a string, so convert it to a number
75     this.resolutionId = parseInt(this.resolutionId.toString(), 10)
76
77     const file = this.getVideoFiles().find(f => f.resolution.id === this.resolutionId)
78     if (!file) {
79       console.error('Could not find file with resolution %d.', this.resolutionId)
80       return
81     }
82
83     const suffix = this.video.privacy.id === VideoPrivacy.PRIVATE || this.video.privacy.id === VideoPrivacy.INTERNAL
84       ? '?access_token=' + this.auth.getAccessToken()
85       : ''
86
87     switch (this.downloadType) {
88       case 'direct':
89         return file.fileDownloadUrl + suffix
90
91       case 'torrent':
92         return file.torrentDownloadUrl + suffix
93     }
94   }
95
96   getSubtitlesLink () {
97     return window.location.origin + this.videoCaptions.find(caption => caption.language.id === this.subtitleLanguageId).captionPath
98   }
99
100   activateCopiedMessage () {
101     this.notifier.success(this.i18n('Copied'))
102   }
103
104   switchToType (type: DownloadType) {
105     this.type = type
106   }
107 }