]>
Commit | Line | Data |
---|---|---|
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'; | |
7 | import { WebTorrentService } from './webtorrent.service'; | |
8 | ||
9 | @Component({ | |
10 | selector: 'my-video-watch', | |
11 | template: require('./video-watch.component.html'), | |
12 | styles: [ require('./video-watch.component.scss') ], | |
13 | providers: [ WebTorrentService ], | |
14 | directives: [ LoaderComponent ], | |
15 | pipes: [ BytesPipe ] | |
16 | }) | |
17 | ||
18 | export class VideoWatchComponent implements OnInit, CanDeactivate { | |
19 | downloadSpeed: number; | |
20 | loading: boolean = false; | |
21 | numPeers: number; | |
22 | uploadSpeed: number; | |
23 | video: Video; | |
24 | ||
25 | private interval: NodeJS.Timer; | |
26 | ||
27 | constructor( | |
28 | private elementRef: ElementRef, | |
29 | private routeParams: RouteParams, | |
30 | private videoService: VideoService, | |
31 | private webTorrentService: WebTorrentService | |
32 | ) {} | |
33 | ||
34 | loadVideo(video: Video) { | |
35 | this.loading = true; | |
36 | this.video = video; | |
37 | console.log('Adding ' + this.video.magnetUri + '.'); | |
38 | ||
39 | this.webTorrentService.add(this.video.magnetUri, (torrent) => { | |
40 | this.loading = false; | |
41 | console.log('Added ' + this.video.magnetUri + '.'); | |
42 | torrent.files[0].appendTo(this.elementRef.nativeElement.querySelector('.embed-responsive'), (err) => { | |
43 | if (err) { | |
44 | alert('Cannot append the file.'); | |
45 | console.error(err); | |
46 | } | |
47 | }); | |
48 | ||
49 | // Refresh each second | |
50 | this.interval = setInterval(() => { | |
51 | this.downloadSpeed = torrent.downloadSpeed; | |
52 | this.numPeers = torrent.numPeers; | |
53 | this.uploadSpeed = torrent.uploadSpeed; | |
54 | }, 1000); | |
55 | }); | |
56 | } | |
57 | ||
58 | ngOnInit() { | |
59 | let id = this.routeParams.get('id'); | |
60 | this.videoService.getVideo(id).subscribe( | |
61 | video => this.loadVideo(video), | |
62 | error => alert(error) | |
63 | ); | |
64 | } | |
65 | ||
66 | routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) { | |
67 | console.log('Removing video from webtorrent.'); | |
68 | clearInterval(this.interval); | |
69 | this.webTorrentService.remove(this.video.magnetUri); | |
70 | return true; | |
71 | } | |
72 | } |