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';
7 import { WebTorrentService } from './webtorrent.service';
10 selector: 'my-video-watch',
11 templateUrl: 'client/app/videos/video-watch/video-watch.component.html',
12 styleUrls: [ 'client/app/videos/video-watch/video-watch.component.css' ],
13 providers: [ WebTorrentService ],
14 directives: [ LoaderComponent ],
18 export class VideoWatchComponent implements OnInit, CanDeactivate {
19 downloadSpeed: number;
20 loading: boolean = false;
25 private interval: NodeJS.Timer;
28 private elementRef: ElementRef,
29 private routeParams: RouteParams,
30 private videoService: VideoService,
31 private webTorrentService: WebTorrentService
34 loadVideo(video: Video) {
37 console.log('Adding ' + this.video.magnetUri + '.');
39 this.webTorrentService.add(this.video.magnetUri, (torrent) => {
41 console.log('Added ' + this.video.magnetUri + '.');
42 torrent.files[0].appendTo(this.elementRef.nativeElement.querySelector('.embed-responsive'), (err) => {
44 alert('Cannot append the file.');
49 // Refresh each second
50 this.interval = setInterval(() => {
51 this.downloadSpeed = torrent.downloadSpeed;
52 this.numPeers = torrent.numPeers;
53 this.uploadSpeed = torrent.uploadSpeed;
59 let id = this.routeParams.get('id');
60 this.videoService.getVideo(id).subscribe(
61 video => this.loadVideo(video),
66 routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
67 console.log('Removing video from webtorrent.');
68 clearInterval(this.interval);
69 this.webTorrentService.remove(this.video.magnetUri);