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'
8 type DownloadType = 'video' | 'subtitles'
11 selector: 'my-video-download',
12 templateUrl: './video-download.component.html',
13 styleUrls: [ './video-download.component.scss' ]
15 export class VideoDownloadComponent {
16 @ViewChild('modal', { static: true }) modal: ElementRef
18 downloadType: 'direct' | 'torrent' = 'torrent'
19 resolutionId: number | string = -1
20 subtitleLanguageId: string
23 videoCaptions: VideoCaption[]
24 activeModal: NgbActiveModal
26 type: DownloadType = 'video'
29 private notifier: Notifier,
30 private modalService: NgbModal,
31 private auth: AuthService,
36 return this.type === 'video'
38 : this.i18n('subtitles')
42 if (!this.video) return []
44 return this.video.getFiles()
47 show (video: VideoDetails, videoCaptions?: VideoCaption[]) {
49 this.videoCaptions = videoCaptions && videoCaptions.length ? videoCaptions : undefined
51 this.activeModal = this.modalService.open(this.modal, { centered: true })
53 this.resolutionId = this.getVideoFiles()[0].resolution.id
54 if (this.videoCaptions) this.subtitleLanguageId = this.videoCaptions[0].language.id
58 this.video = undefined
59 this.videoCaptions = undefined
63 window.location.assign(this.getLink())
64 this.activeModal.close()
68 return this.type === 'subtitles' && this.videoCaptions
69 ? this.getSubtitlesLink()
74 // HTML select send us a string, so convert it to a number
75 this.resolutionId = parseInt(this.resolutionId.toString(), 10)
77 const file = this.getVideoFiles().find(f => f.resolution.id === this.resolutionId)
79 console.error('Could not find file with resolution %d.', this.resolutionId)
83 const suffix = this.video.privacy.id === VideoPrivacy.PRIVATE || this.video.privacy.id === VideoPrivacy.INTERNAL
84 ? '?access_token=' + this.auth.getAccessToken()
87 switch (this.downloadType) {
89 return file.fileDownloadUrl + suffix
92 return file.torrentDownloadUrl + suffix
97 return window.location.origin + this.videoCaptions.find(caption => caption.language.id === this.subtitleLanguageId).captionPath
100 activateCopiedMessage () {
101 this.notifier.success(this.i18n('Copied'))
104 switchToType (type: DownloadType) {