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