]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - 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
1 import { Component, ElementRef, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core';
2 import { ActivatedRoute } from '@angular/router';
3
4 import { ModalDirective } from 'ng2-bootstrap/components/modal';
5 import { MetaService } from 'ng2-meta';
6
7 import { Video, VideoService } from '../shared';
8 import { WebTorrentService } from './webtorrent.service';
9
10 @Component({
11 selector: 'my-video-watch',
12 templateUrl: './video-watch.component.html',
13 styleUrls: [ './video-watch.component.scss' ]
14 })
15
16 export class VideoWatchComponent implements OnInit, OnDestroy {
17 private static LOADTIME_TOO_LONG: number = 30000;
18
19 @ViewChild('magnetUriModal') magnetUriModal: ModalDirective;
20
21 downloadSpeed: number;
22 error: boolean = false;
23 loading: boolean = false;
24 numPeers: number;
25 uploadSpeed: number;
26 video: Video = null;
27
28 private errorTimer: NodeJS.Timer;
29 private sub: any;
30 private torrentInfosInterval: NodeJS.Timer;
31
32 constructor(
33 private elementRef: ElementRef,
34 private ngZone: NgZone,
35 private route: ActivatedRoute,
36 private videoService: VideoService,
37 private metaService: MetaService,
38 private webTorrentService: WebTorrentService
39 ) {}
40
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;
47 this.setOpenGraphTags();
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
64 loadVideo() {
65
66 console.log('<iframe width="560" height="315" src="' + window.location.origin + '/videos/embed/' + this.video.id + '" frameborder="0" allowfullscreen></iframe>');
67
68 // Reset the error
69 this.error = false;
70 // We are loading the video
71 this.loading = true;
72
73 console.log('Adding ' + this.video.magnetUri + '.');
74
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
79 this.webTorrentService.add(this.video.magnetUri, (torrent) => {
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
86 this.loading = false;
87
88 console.log('Added ' + this.video.magnetUri + '.');
89 torrent.files[0].appendTo(this.elementRef.nativeElement.querySelector('.embed-responsive'), (err) => {
90 if (err) {
91 alert('Cannot append the file.');
92 console.error(err);
93 }
94 });
95
96 this.runInProgress(torrent);
97 });
98 }
99
100 showMagnetUriModal() {
101 this.magnetUriModal.show();
102 }
103
104 hideMagnetUriModal() {
105 this.magnetUriModal.hide();
106 }
107
108 private loadTooLong() {
109 this.error = true;
110 console.error('The video load seems to be abnormally long.');
111 }
112
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
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 }
142 }