aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/videos/+video-watch/modal/video-download.component.ts
blob: b06a7eef1741f88c629a1fb9ee3605bb15d6e45e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { Component, Input, OnInit, ViewChild } from '@angular/core'
import { ModalDirective } from 'ngx-bootstrap/modal'
import { VideoDetails } from '../../../shared/video/video-details.model'

@Component({
  selector: 'my-video-download',
  templateUrl: './video-download.component.html',
  styleUrls: [ './video-download.component.scss' ]
})
export class VideoDownloadComponent implements OnInit {
  @Input() video: VideoDetails = null

  @ViewChild('modal') modal: ModalDirective

  downloadType: 'direct' | 'torrent' = 'torrent'
  resolutionId: number | string = -1

  constructor () {
    // empty
  }

  ngOnInit () {
    this.resolutionId = this.video.files[0].resolution.id
  }

  show () {
    this.modal.show()
  }

  hide () {
    this.modal.hide()
  }

  download () {
    // HTML select send us a string, so convert it to a number
    this.resolutionId = parseInt(this.resolutionId.toString(), 10)

    const file = this.video.files.find(f => f.resolution.id === this.resolutionId)
    if (!file) {
      console.error('Could not find file with resolution %d.', this.resolutionId)
      return
    }

    const link = this.downloadType === 'direct' ? file.fileUrl : file.torrentUrl
    window.open(link)
  }
}