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