]>
Commit | Line | Data |
---|---|---|
dc8bc31b | 1 | import { Component, OnInit, ElementRef } from 'angular2/core'; |
98b01bac | 2 | import { RouteParams, CanDeactivate, ComponentInstruction } from 'angular2/router'; |
17c912d9 | 3 | import { BytesPipe } from 'angular-pipes/math'; |
dc8bc31b | 4 | |
98b01bac | 5 | // TODO import it with systemjs |
dc8bc31b C |
6 | declare var WebTorrent: any; |
7 | ||
8 | import { Video } from '../../models/video'; | |
9 | import { VideosService } from '../../services/videos.service'; | |
10 | ||
11 | @Component({ | |
12 | selector: 'my-video-watch', | |
13 | templateUrl: 'app/angular/videos/components/watch/videos-watch.component.html', | |
8cfecb2a C |
14 | styleUrls: [ 'app/angular/videos/components/watch/videos-watch.component.css' ], |
15 | pipes: [ BytesPipe ] | |
dc8bc31b C |
16 | }) |
17 | ||
98b01bac | 18 | export class VideosWatchComponent implements OnInit, CanDeactivate { |
dc8bc31b | 19 | video: Video; |
8cfecb2a C |
20 | downloadSpeed: number; |
21 | uploadSpeed: number; | |
22 | numPeers: number; | |
da932efc | 23 | loading: boolean = false; |
dc8bc31b | 24 | |
8cfecb2a | 25 | private _interval: number; |
dc8bc31b C |
26 | private client: any; |
27 | ||
28 | constructor( | |
29 | private _videosService: VideosService, | |
30 | private _routeParams: RouteParams, | |
31 | private _elementRef: ElementRef | |
32 | ) { | |
98b01bac | 33 | // TODO: use a service |
dc8bc31b C |
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) { | |
da932efc | 46 | this.loading = true; |
dc8bc31b | 47 | this.video = video; |
2c4a0b5d | 48 | console.log('Adding ' + this.video.magnetUri + '.'); |
dc8bc31b | 49 | this.client.add(this.video.magnetUri, (torrent) => { |
da932efc | 50 | this.loading = false; |
2c4a0b5d | 51 | console.log('Added ' + this.video.magnetUri + '.'); |
cb11e775 | 52 | torrent.files[0].appendTo(this._elementRef.nativeElement.querySelector('.embed-responsive'), (err) => { |
dc8bc31b C |
53 | if (err) { |
54 | alert('Cannot append the file.'); | |
55 | console.error(err); | |
56 | } | |
44124980 | 57 | }); |
8cfecb2a C |
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); | |
44124980 | 65 | }); |
dc8bc31b | 66 | } |
98b01bac C |
67 | |
68 | routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) : any { | |
69 | console.log('Removing video from webtorrent.'); | |
e9a2578e | 70 | clearInterval(this._interval); |
98b01bac C |
71 | this.client.remove(this.video.magnetUri); |
72 | return true; | |
73 | } | |
dc8bc31b | 74 | } |