1 import { Component, ElementRef, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core';
2 import { ActivatedRoute } from '@angular/router';
4 import { ModalDirective } from 'ng2-bootstrap/components/modal';
5 import { MetaService } from 'ng2-meta';
7 import { Video, VideoService } from '../shared';
8 import { WebTorrentService } from './webtorrent.service';
11 selector: 'my-video-watch',
12 templateUrl: './video-watch.component.html',
13 styleUrls: [ './video-watch.component.scss' ]
16 export class VideoWatchComponent implements OnInit, OnDestroy {
17 private static LOADTIME_TOO_LONG: number = 30000;
19 @ViewChild('magnetUriModal') magnetUriModal: ModalDirective;
21 downloadSpeed: number;
22 error: boolean = false;
23 loading: boolean = false;
28 private errorTimer: NodeJS.Timer;
30 private torrentInfosInterval: NodeJS.Timer;
33 private elementRef: ElementRef,
34 private ngZone: NgZone,
35 private route: ActivatedRoute,
36 private videoService: VideoService,
37 private metaService: MetaService,
38 private webTorrentService: WebTorrentService
42 this.sub = this.route.params.subscribe(routeParams => {
43 let id = routeParams['id'];
44 this.videoService.getVideo(id).subscribe(
47 this.setOpenGraphTags();
50 error => alert(error.text)
56 console.log('Removing video from webtorrent.');
57 clearInterval(this.torrentInfosInterval);
58 clearTimeout(this.errorTimer);
59 this.webTorrentService.remove(this.video.magnetUri);
61 this.sub.unsubscribe();
67 // We are loading the video
70 console.log('Adding ' + this.video.magnetUri + '.');
72 // The callback might never return if there are network issues
73 // So we create a timer to inform the user the load is abnormally long
74 this.errorTimer = setTimeout(() => this.loadTooLong(), VideoWatchComponent.LOADTIME_TOO_LONG);
76 this.webTorrentService.add(this.video.magnetUri, (torrent) => {
77 // Clear the error timer
78 clearTimeout(this.errorTimer);
79 // Maybe the error was fired by the timer, so reset it
82 // We are not loading the video anymore
85 console.log('Added ' + this.video.magnetUri + '.');
86 torrent.files[0].appendTo(this.elementRef.nativeElement.querySelector('.embed-responsive'), (err) => {
88 alert('Cannot append the file.');
93 this.runInProgress(torrent);
97 showMagnetUriModal() {
98 this.magnetUriModal.show();
101 hideMagnetUriModal() {
102 this.magnetUriModal.hide();
105 private loadTooLong() {
107 console.error('The video load seems to be abnormally long.');
110 private setOpenGraphTags() {
111 this.metaService.setTag('og:type', 'video');
113 this.metaService.setTag('og:title', this.video.name);
114 this.metaService.setTag('name', this.video.name);
116 this.metaService.setTag('og:description', this.video.description);
117 this.metaService.setTag('description', this.video.description);
119 this.metaService.setTag('og:image', this.video.thumbnailPath);
121 this.metaService.setTag('og:duration', this.video.duration);
123 this.metaService.setTag('og:site_name', 'PeerTube');
125 this.metaService.setTag('og:url', window.location.href);
126 this.metaService.setTag('url', window.location.href);
129 private runInProgress(torrent: any) {
130 // Refresh each second
131 this.torrentInfosInterval = setInterval(() => {
132 this.ngZone.run(() => {
133 this.downloadSpeed = torrent.downloadSpeed;
134 this.numPeers = torrent.numPeers;
135 this.uploadSpeed = torrent.uploadSpeed;