]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/video-watch/video-watch.component.ts
Prepare embed page
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / video-watch / video-watch.component.ts
CommitLineData
3154f382 1import { Component, ElementRef, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core';
0629423c 2import { ActivatedRoute } from '@angular/router';
8140a704 3
3154f382 4import { ModalDirective } from 'ng2-bootstrap/components/modal';
3ec343a4 5import { MetaService } from 'ng2-meta';
3154f382 6
ab32b0fc 7import { Video, VideoService } from '../shared';
d3ef341a 8import { WebTorrentService } from './webtorrent.service';
dc8bc31b 9
dc8bc31b
C
10@Component({
11 selector: 'my-video-watch',
ec8d8440
C
12 templateUrl: './video-watch.component.html',
13 styleUrls: [ './video-watch.component.scss' ]
dc8bc31b
C
14})
15
0629423c 16export class VideoWatchComponent implements OnInit, OnDestroy {
3ad109e4
C
17 private static LOADTIME_TOO_LONG: number = 30000;
18
3154f382
C
19 @ViewChild('magnetUriModal') magnetUriModal: ModalDirective;
20
8cfecb2a 21 downloadSpeed: number;
3ad109e4 22 error: boolean = false;
da932efc 23 loading: boolean = false;
4fd8aa32
C
24 numPeers: number;
25 uploadSpeed: number;
d1992b93 26 video: Video = null;
dc8bc31b 27
3ad109e4 28 private errorTimer: NodeJS.Timer;
0629423c 29 private sub: any;
3ad109e4 30 private torrentInfosInterval: NodeJS.Timer;
dc8bc31b
C
31
32 constructor(
4fd8aa32 33 private elementRef: ElementRef,
c323efb9 34 private ngZone: NgZone,
0629423c 35 private route: ActivatedRoute,
d3ef341a 36 private videoService: VideoService,
3ec343a4 37 private metaService: MetaService,
d3ef341a
C
38 private webTorrentService: WebTorrentService
39 ) {}
dc8bc31b 40
d1992b93
C
41 ngOnInit() {
42 this.sub = this.route.params.subscribe(routeParams => {
43 let id = routeParams['id'];
44 this.videoService.getVideo(id).subscribe(
45 video => {
46 this.video = video;
3ec343a4 47 this.setOpenGraphTags();
d1992b93
C
48 this.loadVideo();
49 },
50 error => alert(error.text)
51 );
52 });
53 }
54
55 ngOnDestroy() {
56 console.log('Removing video from webtorrent.');
57 clearInterval(this.torrentInfosInterval);
58 clearTimeout(this.errorTimer);
59 this.webTorrentService.remove(this.video.magnetUri);
60
61 this.sub.unsubscribe();
62 }
63
3ad109e4 64 loadVideo() {
3bb2c7f9
C
65
66 console.log('<iframe width="560" height="315" src="' + window.location.origin + '/videos/embed/' + this.video.id + '" frameborder="0" allowfullscreen></iframe>');
67
3ad109e4
C
68 // Reset the error
69 this.error = false;
70 // We are loading the video
da932efc 71 this.loading = true;
3ad109e4 72
2c4a0b5d 73 console.log('Adding ' + this.video.magnetUri + '.');
d3ef341a 74
3ad109e4
C
75 // The callback might never return if there are network issues
76 // So we create a timer to inform the user the load is abnormally long
77 this.errorTimer = setTimeout(() => this.loadTooLong(), VideoWatchComponent.LOADTIME_TOO_LONG);
78
d3ef341a 79 this.webTorrentService.add(this.video.magnetUri, (torrent) => {
3ad109e4
C
80 // Clear the error timer
81 clearTimeout(this.errorTimer);
82 // Maybe the error was fired by the timer, so reset it
83 this.error = false;
84
85 // We are not loading the video anymore
da932efc 86 this.loading = false;
3ad109e4 87
2c4a0b5d 88 console.log('Added ' + this.video.magnetUri + '.');
ccf6ed16 89 torrent.files[0].appendTo(this.elementRef.nativeElement.querySelector('.embed-responsive'), (err) => {
dc8bc31b
C
90 if (err) {
91 alert('Cannot append the file.');
92 console.error(err);
93 }
44124980 94 });
8cfecb2a 95
c323efb9 96 this.runInProgress(torrent);
44124980 97 });
dc8bc31b 98 }
98b01bac 99
3154f382
C
100 showMagnetUriModal() {
101 this.magnetUriModal.show();
102 }
103
104 hideMagnetUriModal() {
105 this.magnetUriModal.hide();
106 }
107
3ad109e4
C
108 private loadTooLong() {
109 this.error = true;
110 console.error('The video load seems to be abnormally long.');
111 }
c323efb9 112
3ec343a4
C
113 private setOpenGraphTags() {
114 this.metaService.setTag('og:type', 'video');
115
116 this.metaService.setTag('og:title', this.video.name);
117 this.metaService.setTag('name', this.video.name);
118
119 this.metaService.setTag('og:description', this.video.description);
120 this.metaService.setTag('description', this.video.description);
121
122 this.metaService.setTag('og:image', this.video.thumbnailPath);
123
124 this.metaService.setTag('og:duration', this.video.duration);
125
126 this.metaService.setTag('og:site_name', 'PeerTube');
127
128 this.metaService.setTag('og:url', window.location.href);
129 this.metaService.setTag('url', window.location.href);
130 }
131
c323efb9
C
132 private runInProgress(torrent: any) {
133 // Refresh each second
134 this.torrentInfosInterval = setInterval(() => {
135 this.ngZone.run(() => {
136 this.downloadSpeed = torrent.downloadSpeed;
137 this.numPeers = torrent.numPeers;
138 this.uploadSpeed = torrent.uploadSpeed;
139 });
140 }, 1000);
141 }
dc8bc31b 142}