]>
Commit | Line | Data |
---|---|---|
63347a0f | 1 | import { Component, ElementRef, Input, OnInit, ViewChild } from '@angular/core' |
4635f59d | 2 | import { VideoDetails } from '../../../shared/video/video-details.model' |
63347a0f | 3 | import { NgbModal } from '@ng-bootstrap/ng-bootstrap' |
bb5d7428 RK |
4 | import { I18n } from '@ngx-translate/i18n-polyfill' |
5 | import { NotificationsService } from 'angular2-notifications' | |
cf02fbfb C |
6 | |
7 | @Component({ | |
a96aed15 C |
8 | selector: 'my-video-download', |
9 | templateUrl: './video-download.component.html', | |
0727cab0 | 10 | styleUrls: [ './video-download.component.scss' ] |
cf02fbfb | 11 | }) |
5f0805d3 | 12 | export class VideoDownloadComponent implements OnInit { |
404b54e1 | 13 | @Input() video: VideoDetails = null |
cf02fbfb | 14 | |
63347a0f | 15 | @ViewChild('modal') modal: ElementRef |
cf02fbfb | 16 | |
5511da62 | 17 | downloadType: 'direct' | 'torrent' | 'magnet' = 'torrent' |
09700934 | 18 | resolutionId: number | string = -1 |
5f0805d3 | 19 | |
bb5d7428 RK |
20 | constructor ( |
21 | private notificationsService: NotificationsService, | |
22 | private modalService: NgbModal, | |
23 | private i18n: I18n | |
24 | ) { } | |
cf02fbfb | 25 | |
5f0805d3 | 26 | ngOnInit () { |
09700934 | 27 | this.resolutionId = this.video.files[0].resolution.id |
5f0805d3 C |
28 | } |
29 | ||
df98563e | 30 | show () { |
63347a0f | 31 | this.modalService.open(this.modal) |
cf02fbfb | 32 | } |
5f0805d3 C |
33 | |
34 | download () { | |
bb5d7428 RK |
35 | window.location.assign(this.getLink()) |
36 | } | |
37 | ||
38 | getLink () { | |
00336945 | 39 | // HTML select send us a string, so convert it to a number |
09700934 | 40 | this.resolutionId = parseInt(this.resolutionId.toString(), 10) |
00336945 | 41 | |
09700934 | 42 | const file = this.video.files.find(f => f.resolution.id === this.resolutionId) |
5f0805d3 | 43 | if (!file) { |
09700934 | 44 | console.error('Could not find file with resolution %d.', this.resolutionId) |
5f0805d3 C |
45 | return |
46 | } | |
47 | ||
5511da62 RK |
48 | const link = (() => { |
49 | switch (this.downloadType) { | |
50 | case 'direct': { | |
51 | return file.fileDownloadUrl | |
52 | } | |
53 | case 'torrent': { | |
54 | return file.torrentDownloadUrl | |
55 | } | |
56 | case 'magnet': { | |
57 | return file.magnetUri | |
58 | } | |
59 | } | |
60 | })() | |
bb5d7428 RK |
61 | |
62 | return link | |
63 | } | |
64 | ||
65 | activateCopiedMessage () { | |
66 | this.notificationsService.success(this.i18n('Success'), this.i18n('Copied')) | |
5f0805d3 | 67 | } |
cf02fbfb | 68 | } |