]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/app/videos/video-watch/video-watch.component.ts
137db8f0b23987a064370ddf4ec0be68e6dc73b6
[github/Chocobozzz/PeerTube.git] / client / app / videos / video-watch / video-watch.component.ts
1 import { Component, ElementRef, OnInit } from '@angular/core';
2 import { CanDeactivate, ComponentInstruction, RouteParams } from '@angular/router-deprecated';
3
4 import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe';
5
6 import { LoaderComponent, Video, VideoService } from '../shared/index';
7
8 // TODO import it with systemjs
9 declare var WebTorrent: any;
10
11 @Component({
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 ],
16 pipes: [ BytesPipe ]
17 })
18
19 export class VideoWatchComponent implements OnInit, CanDeactivate {
20 downloadSpeed: number;
21 loading: boolean = false;
22 numPeers: number;
23 uploadSpeed: number;
24 video: Video;
25
26 private client: any;
27 private interval: NodeJS.Timer;
28
29 constructor(
30 private elementRef: ElementRef,
31 private routeParams: RouteParams,
32 private videoService: VideoService
33 ) {
34 // TODO: use a service
35 this.client = new WebTorrent({ dht: false });
36 }
37
38 loadVideo(video: Video) {
39 this.loading = true;
40 this.video = video;
41 console.log('Adding ' + this.video.magnetUri + '.');
42 this.client.add(this.video.magnetUri, (torrent) => {
43 this.loading = false;
44 console.log('Added ' + this.video.magnetUri + '.');
45 torrent.files[0].appendTo(this.elementRef.nativeElement.querySelector('.embed-responsive'), (err) => {
46 if (err) {
47 alert('Cannot append the file.');
48 console.error(err);
49 }
50 });
51
52 // Refresh each second
53 this.interval = setInterval(() => {
54 this.downloadSpeed = torrent.downloadSpeed;
55 this.numPeers = torrent.numPeers;
56 this.uploadSpeed = torrent.uploadSpeed;
57 }, 1000);
58 });
59 }
60
61 ngOnInit() {
62 let id = this.routeParams.get('id');
63 this.videoService.getVideo(id).subscribe(
64 video => this.loadVideo(video),
65 error => alert(error)
66 );
67 }
68
69 routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
70 console.log('Removing video from webtorrent.');
71 clearInterval(this.interval);
72 this.client.remove(this.video.magnetUri);
73 return true;
74 }
75 }