]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-watch/modal/video-download.component.ts
f4d9003ee74db62ec99d6008624b473aacfcabaa
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / modal / video-download.component.ts
1 import { Component, ElementRef, Input, OnInit, ViewChild } from '@angular/core'
2 import { VideoDetails } from '../../../shared/video/video-details.model'
3 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
4
5 @Component({
6 selector: 'my-video-download',
7 templateUrl: './video-download.component.html',
8 styleUrls: [ './video-download.component.scss' ]
9 })
10 export class VideoDownloadComponent implements OnInit {
11 @Input() video: VideoDetails = null
12
13 @ViewChild('modal') modal: ElementRef
14
15 downloadType: 'direct' | 'torrent' | 'magnet' = 'torrent'
16 resolutionId: number | string = -1
17
18 constructor (private modalService: NgbModal) { }
19
20 ngOnInit () {
21 this.resolutionId = this.video.files[0].resolution.id
22 }
23
24 show () {
25 this.modalService.open(this.modal)
26 }
27
28 download () {
29 // HTML select send us a string, so convert it to a number
30 this.resolutionId = parseInt(this.resolutionId.toString(), 10)
31
32 const file = this.video.files.find(f => f.resolution.id === this.resolutionId)
33 if (!file) {
34 console.error('Could not find file with resolution %d.', this.resolutionId)
35 return
36 }
37
38 const link = (() => {
39 switch (this.downloadType) {
40 case 'direct': {
41 return file.fileDownloadUrl
42 }
43 case 'torrent': {
44 return file.torrentDownloadUrl
45 }
46 case 'magnet': {
47 return file.magnetUri
48 }
49 }
50 })()
51 window.location.assign(link)
52 }
53 }