1
0
Fork 0
peertube/client/src/app/videos/+video-watch/modal/video-download.component.ts

69 lines
1.8 KiB
TypeScript
Raw Normal View History

import { Component, ElementRef, Input, OnInit, ViewChild } from '@angular/core'
2017-12-27 10:11:53 -05:00
import { VideoDetails } from '../../../shared/video/video-details.model'
import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
2018-09-23 13:43:41 -04:00
import { I18n } from '@ngx-translate/i18n-polyfill'
import { NotificationsService } from 'angular2-notifications'
@Component({
selector: 'my-video-download',
templateUrl: './video-download.component.html',
2017-12-07 04:27:33 -05:00
styleUrls: [ './video-download.component.scss' ]
})
2017-12-20 11:49:58 -05:00
export class VideoDownloadComponent implements OnInit {
2017-10-25 10:43:19 -04:00
@Input() video: VideoDetails = null
@ViewChild('modal') modal: ElementRef
downloadType: 'direct' | 'torrent' | 'magnet' = 'torrent'
resolutionId: number | string = -1
2017-12-20 11:49:58 -05:00
2018-09-23 13:43:41 -04:00
constructor (
private notificationsService: NotificationsService,
private modalService: NgbModal,
private i18n: I18n
) { }
2017-12-20 11:49:58 -05:00
ngOnInit () {
this.resolutionId = this.video.files[0].resolution.id
2017-12-20 11:49:58 -05:00
}
show () {
this.modalService.open(this.modal)
}
2017-12-20 11:49:58 -05:00
download () {
2018-09-23 13:43:41 -04:00
window.location.assign(this.getLink())
}
getLink () {
2018-02-07 05:05:59 -05:00
// HTML select send us a string, so convert it to a number
this.resolutionId = parseInt(this.resolutionId.toString(), 10)
2018-02-07 05:05:59 -05:00
const file = this.video.files.find(f => f.resolution.id === this.resolutionId)
2017-12-20 11:49:58 -05:00
if (!file) {
console.error('Could not find file with resolution %d.', this.resolutionId)
2017-12-20 11:49:58 -05:00
return
}
const link = (() => {
switch (this.downloadType) {
case 'direct': {
return file.fileDownloadUrl
}
case 'torrent': {
return file.torrentDownloadUrl
}
case 'magnet': {
return file.magnetUri
}
}
})()
2018-09-23 13:43:41 -04:00
return link
}
activateCopiedMessage () {
this.notificationsService.success(this.i18n('Success'), this.i18n('Copied'))
2017-12-20 11:49:58 -05:00
}
}