]>
Commit | Line | Data |
---|---|---|
3a0fb65c | 1 | import { Component, ElementRef, ViewChild } from '@angular/core' |
4635f59d | 2 | import { VideoDetails } from '../../../shared/video/video-details.model' |
11b3f14c | 3 | import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap' |
bb5d7428 | 4 | import { I18n } from '@ngx-translate/i18n-polyfill' |
eccf70f0 C |
5 | import { AuthService, Notifier } from '@app/core' |
6 | import { VideoPrivacy } from '@shared/models' | |
cf02fbfb C |
7 | |
8 | @Component({ | |
a96aed15 C |
9 | selector: 'my-video-download', |
10 | templateUrl: './video-download.component.html', | |
0727cab0 | 11 | styleUrls: [ './video-download.component.scss' ] |
cf02fbfb | 12 | }) |
3a0fb65c | 13 | export class VideoDownloadComponent { |
f36da21e | 14 | @ViewChild('modal', { static: true }) modal: ElementRef |
cf02fbfb | 15 | |
7c51916a | 16 | downloadType: 'direct' | 'torrent' = 'torrent' |
09700934 | 17 | resolutionId: number | string = -1 |
5f0805d3 | 18 | |
8dfceec4 | 19 | video: VideoDetails |
11b3f14c | 20 | activeModal: NgbActiveModal |
3a0fb65c | 21 | |
bb5d7428 | 22 | constructor ( |
f8b2c1b4 | 23 | private notifier: Notifier, |
bb5d7428 | 24 | private modalService: NgbModal, |
eccf70f0 | 25 | private auth: AuthService, |
bb5d7428 RK |
26 | private i18n: I18n |
27 | ) { } | |
cf02fbfb | 28 | |
5a71acd2 C |
29 | getVideoFiles () { |
30 | if (!this.video) return [] | |
31 | ||
32 | return this.video.getFiles() | |
33 | } | |
34 | ||
3a0fb65c C |
35 | show (video: VideoDetails) { |
36 | this.video = video | |
37 | ||
11b3f14c | 38 | this.activeModal = this.modalService.open(this.modal) |
3a0fb65c | 39 | |
5a71acd2 | 40 | this.resolutionId = this.getVideoFiles()[0].resolution.id |
5f0805d3 C |
41 | } |
42 | ||
3a0fb65c C |
43 | onClose () { |
44 | this.video = undefined | |
cf02fbfb | 45 | } |
5f0805d3 C |
46 | |
47 | download () { | |
bb5d7428 | 48 | window.location.assign(this.getLink()) |
11b3f14c | 49 | this.activeModal.close() |
bb5d7428 RK |
50 | } |
51 | ||
52 | getLink () { | |
00336945 | 53 | // HTML select send us a string, so convert it to a number |
09700934 | 54 | this.resolutionId = parseInt(this.resolutionId.toString(), 10) |
00336945 | 55 | |
5a71acd2 | 56 | const file = this.getVideoFiles().find(f => f.resolution.id === this.resolutionId) |
5f0805d3 | 57 | if (!file) { |
09700934 | 58 | console.error('Could not find file with resolution %d.', this.resolutionId) |
5f0805d3 C |
59 | return |
60 | } | |
61 | ||
eccf70f0 C |
62 | const suffix = this.video.privacy.id === VideoPrivacy.PRIVATE |
63 | ? '?access_token=' + this.auth.getAccessToken() | |
64 | : '' | |
65 | ||
3a0fb65c C |
66 | switch (this.downloadType) { |
67 | case 'direct': | |
eccf70f0 | 68 | return file.fileDownloadUrl + suffix |
3a0fb65c C |
69 | |
70 | case 'torrent': | |
eccf70f0 | 71 | return file.torrentDownloadUrl + suffix |
3a0fb65c | 72 | } |
bb5d7428 RK |
73 | } |
74 | ||
75 | activateCopiedMessage () { | |
f8b2c1b4 | 76 | this.notifier.success(this.i18n('Copied')) |
5f0805d3 | 77 | } |
cf02fbfb | 78 | } |