Merge branch 'feature/audio-upload' into develop
[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 { Notifier } from '@app/core'
6
7 @Component({
8   selector: 'my-video-download',
9   templateUrl: './video-download.component.html',
10   styleUrls: [ './video-download.component.scss' ]
11 })
12 export class VideoDownloadComponent {
13   @ViewChild('modal') modal: ElementRef
14
15   downloadType: 'direct' | 'torrent' | 'magnet' = 'torrent'
16   resolutionId: number | string = -1
17
18   video: VideoDetails
19   activeModal: NgbActiveModal
20
21   constructor (
22     private notifier: Notifier,
23     private modalService: NgbModal,
24     private i18n: I18n
25   ) { }
26
27   show (video: VideoDetails) {
28     this.video = video
29
30     this.activeModal = this.modalService.open(this.modal)
31
32     this.resolutionId = this.video.files[0].resolution.id
33   }
34
35   onClose () {
36     this.video = undefined
37   }
38
39   download () {
40     window.location.assign(this.getLink())
41     this.activeModal.close()
42   }
43
44   getLink () {
45     // HTML select send us a string, so convert it to a number
46     this.resolutionId = parseInt(this.resolutionId.toString(), 10)
47
48     const file = this.video.files.find(f => f.resolution.id === this.resolutionId)
49     if (!file) {
50       console.error('Could not find file with resolution %d.', this.resolutionId)
51       return
52     }
53
54     switch (this.downloadType) {
55       case 'direct':
56         return file.fileDownloadUrl
57
58       case 'torrent':
59         return file.torrentDownloadUrl
60
61       case 'magnet':
62         return file.magnetUri
63     }
64   }
65
66   activateCopiedMessage () {
67     this.notifier.success(this.i18n('Copied'))
68   }
69 }