1
0
Fork 0
peertube/client/src/app/shared/video/modals/video-download.component.ts

79 lines
2.1 KiB
TypeScript
Raw Normal View History

2019-04-05 08:52:27 +00:00
import { Component, ElementRef, ViewChild } from '@angular/core'
2017-12-27 15:11:53 +00:00
import { VideoDetails } from '../../../shared/video/video-details.model'
import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap'
2018-09-23 17:43:41 +00:00
import { I18n } from '@ngx-translate/i18n-polyfill'
2019-12-03 09:41:23 +00:00
import { AuthService, Notifier } from '@app/core'
import { VideoPrivacy } from '@shared/models'
@Component({
selector: 'my-video-download',
templateUrl: './video-download.component.html',
2017-12-07 09:27:33 +00:00
styleUrls: [ './video-download.component.scss' ]
})
2019-04-05 08:52:27 +00:00
export class VideoDownloadComponent {
2019-07-24 14:05:59 +00:00
@ViewChild('modal', { static: true }) modal: ElementRef
downloadType: 'direct' | 'torrent' = 'torrent'
resolutionId: number | string = -1
2017-12-20 16:49:58 +00:00
video: VideoDetails
activeModal: NgbActiveModal
2019-04-05 08:52:27 +00:00
2018-09-23 17:43:41 +00:00
constructor (
private notifier: Notifier,
2018-09-23 17:43:41 +00:00
private modalService: NgbModal,
2019-12-03 09:41:23 +00:00
private auth: AuthService,
2018-09-23 17:43:41 +00:00
private i18n: I18n
) { }
2019-11-21 16:01:24 +00:00
getVideoFiles () {
if (!this.video) return []
return this.video.getFiles()
}
2019-04-05 08:52:27 +00:00
show (video: VideoDetails) {
this.video = video
this.activeModal = this.modalService.open(this.modal)
2019-04-05 08:52:27 +00:00
2019-11-21 16:01:24 +00:00
this.resolutionId = this.getVideoFiles()[0].resolution.id
2017-12-20 16:49:58 +00:00
}
2019-04-05 08:52:27 +00:00
onClose () {
this.video = undefined
}
2017-12-20 16:49:58 +00:00
download () {
2018-09-23 17:43:41 +00:00
window.location.assign(this.getLink())
this.activeModal.close()
2018-09-23 17:43:41 +00:00
}
getLink () {
2018-02-07 10:05:59 +00:00
// HTML select send us a string, so convert it to a number
this.resolutionId = parseInt(this.resolutionId.toString(), 10)
2018-02-07 10:05:59 +00:00
2019-11-21 16:01:24 +00:00
const file = this.getVideoFiles().find(f => f.resolution.id === this.resolutionId)
2017-12-20 16:49:58 +00:00
if (!file) {
console.error('Could not find file with resolution %d.', this.resolutionId)
2017-12-20 16:49:58 +00:00
return
}
2019-12-12 14:47:47 +00:00
const suffix = this.video.privacy.id === VideoPrivacy.PRIVATE || this.video.privacy.id === VideoPrivacy.INTERNAL
2019-12-03 09:41:23 +00:00
? '?access_token=' + this.auth.getAccessToken()
: ''
2019-04-05 08:52:27 +00:00
switch (this.downloadType) {
case 'direct':
2019-12-03 09:41:23 +00:00
return file.fileDownloadUrl + suffix
2019-04-05 08:52:27 +00:00
case 'torrent':
2019-12-03 09:41:23 +00:00
return file.torrentDownloadUrl + suffix
2019-04-05 08:52:27 +00:00
}
2018-09-23 17:43:41 +00:00
}
activateCopiedMessage () {
this.notifier.success(this.i18n('Copied'))
2017-12-20 16:49:58 +00:00
}
}