]>
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 | 5 | import { AuthService, Notifier } from '@app/core' |
8ba9c205 RK |
6 | import { VideoPrivacy, VideoCaption } from '@shared/models' |
7 | ||
8 | type DownloadType = 'video' | 'subtitles' | |
cf02fbfb C |
9 | |
10 | @Component({ | |
a96aed15 C |
11 | selector: 'my-video-download', |
12 | templateUrl: './video-download.component.html', | |
0727cab0 | 13 | styleUrls: [ './video-download.component.scss' ] |
cf02fbfb | 14 | }) |
3a0fb65c | 15 | export class VideoDownloadComponent { |
f36da21e | 16 | @ViewChild('modal', { static: true }) modal: ElementRef |
cf02fbfb | 17 | |
7c51916a | 18 | downloadType: 'direct' | 'torrent' = 'torrent' |
09700934 | 19 | resolutionId: number | string = -1 |
8ba9c205 | 20 | subtitleLanguageId: string |
5f0805d3 | 21 | |
8dfceec4 | 22 | video: VideoDetails |
8ba9c205 | 23 | videoCaptions: VideoCaption[] |
11b3f14c | 24 | activeModal: NgbActiveModal |
3a0fb65c | 25 | |
8ba9c205 RK |
26 | type: DownloadType = 'video' |
27 | ||
bb5d7428 | 28 | constructor ( |
f8b2c1b4 | 29 | private notifier: Notifier, |
bb5d7428 | 30 | private modalService: NgbModal, |
eccf70f0 | 31 | private auth: AuthService, |
bb5d7428 RK |
32 | private i18n: I18n |
33 | ) { } | |
cf02fbfb | 34 | |
8ba9c205 RK |
35 | get typeText () { |
36 | return this.type === 'video' | |
37 | ? this.i18n('video') | |
38 | : this.i18n('subtitles') | |
39 | } | |
40 | ||
5a71acd2 C |
41 | getVideoFiles () { |
42 | if (!this.video) return [] | |
43 | ||
44 | return this.video.getFiles() | |
45 | } | |
46 | ||
8ba9c205 | 47 | show (video: VideoDetails, videoCaptions?: VideoCaption[]) { |
3a0fb65c | 48 | this.video = video |
8ba9c205 | 49 | this.videoCaptions = videoCaptions && videoCaptions.length ? videoCaptions : undefined |
3a0fb65c | 50 | |
24e7916c | 51 | this.activeModal = this.modalService.open(this.modal, { centered: true }) |
3a0fb65c | 52 | |
5a71acd2 | 53 | this.resolutionId = this.getVideoFiles()[0].resolution.id |
8ba9c205 | 54 | if (this.videoCaptions) this.subtitleLanguageId = this.videoCaptions[0].language.id |
5f0805d3 C |
55 | } |
56 | ||
3a0fb65c C |
57 | onClose () { |
58 | this.video = undefined | |
8ba9c205 | 59 | this.videoCaptions = undefined |
cf02fbfb | 60 | } |
5f0805d3 C |
61 | |
62 | download () { | |
bb5d7428 | 63 | window.location.assign(this.getLink()) |
11b3f14c | 64 | this.activeModal.close() |
bb5d7428 RK |
65 | } |
66 | ||
67 | getLink () { | |
8ba9c205 RK |
68 | return this.type === 'subtitles' && this.videoCaptions |
69 | ? this.getSubtitlesLink() | |
70 | : this.getVideoLink() | |
71 | } | |
72 | ||
73 | getVideoLink () { | |
00336945 | 74 | // HTML select send us a string, so convert it to a number |
09700934 | 75 | this.resolutionId = parseInt(this.resolutionId.toString(), 10) |
00336945 | 76 | |
5a71acd2 | 77 | const file = this.getVideoFiles().find(f => f.resolution.id === this.resolutionId) |
5f0805d3 | 78 | if (!file) { |
09700934 | 79 | console.error('Could not find file with resolution %d.', this.resolutionId) |
5f0805d3 C |
80 | return |
81 | } | |
82 | ||
22a73cb8 | 83 | const suffix = this.video.privacy.id === VideoPrivacy.PRIVATE || this.video.privacy.id === VideoPrivacy.INTERNAL |
eccf70f0 C |
84 | ? '?access_token=' + this.auth.getAccessToken() |
85 | : '' | |
86 | ||
3a0fb65c C |
87 | switch (this.downloadType) { |
88 | case 'direct': | |
eccf70f0 | 89 | return file.fileDownloadUrl + suffix |
3a0fb65c C |
90 | |
91 | case 'torrent': | |
eccf70f0 | 92 | return file.torrentDownloadUrl + suffix |
3a0fb65c | 93 | } |
bb5d7428 RK |
94 | } |
95 | ||
8ba9c205 RK |
96 | getSubtitlesLink () { |
97 | return window.location.origin + this.videoCaptions.find(caption => caption.language.id === this.subtitleLanguageId).captionPath | |
98 | } | |
99 | ||
bb5d7428 | 100 | activateCopiedMessage () { |
f8b2c1b4 | 101 | this.notifier.success(this.i18n('Copied')) |
5f0805d3 | 102 | } |
8ba9c205 RK |
103 | |
104 | switchToType (type: DownloadType) { | |
105 | this.type = type | |
106 | } | |
cf02fbfb | 107 | } |