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