1 import { Component, ElementRef, OnInit } from '@angular/core';
2 import { CanDeactivate, ComponentInstruction, RouteParams } from '@angular/router-deprecated';
4 import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe';
6 import { LoaderComponent, Video, VideoService } from '../shared/index';
8 // TODO import it with systemjs
9 declare var WebTorrent: any;
12 selector: 'my-video-watch',
13 templateUrl: 'client/app/videos/video-watch/video-watch.component.html',
14 styleUrls: [ 'client/app/videos/video-watch/video-watch.component.css' ],
15 directives: [ LoaderComponent ],
19 export class VideoWatchComponent implements OnInit, CanDeactivate {
20 downloadSpeed: number;
21 loading: boolean = false;
27 private interval: NodeJS.Timer;
30 private elementRef: ElementRef,
31 private routeParams: RouteParams,
32 private videoService: VideoService
34 // TODO: use a service
35 this.client = new WebTorrent({ dht: false });
38 loadVideo(video: Video) {
41 console.log('Adding ' + this.video.magnetUri + '.');
42 this.client.add(this.video.magnetUri, (torrent) => {
44 console.log('Added ' + this.video.magnetUri + '.');
45 torrent.files[0].appendTo(this.elementRef.nativeElement.querySelector('.embed-responsive'), (err) => {
47 alert('Cannot append the file.');
52 // Refresh each second
53 this.interval = setInterval(() => {
54 this.downloadSpeed = torrent.downloadSpeed;
55 this.numPeers = torrent.numPeers;
56 this.uploadSpeed = torrent.uploadSpeed;
62 let id = this.routeParams.get('id');
63 this.videoService.getVideo(id).subscribe(
64 video => this.loadVideo(video),
69 routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
70 console.log('Removing video from webtorrent.');
71 clearInterval(this.interval);
72 this.client.remove(this.video.magnetUri);