]>
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 { |
3ad109e4 C |
19 | private static LOADTIME_TOO_LONG: number = 30000; |
20 | ||
8cfecb2a | 21 | downloadSpeed: number; |
3ad109e4 | 22 | error: boolean = false; |
da932efc | 23 | loading: boolean = false; |
4fd8aa32 C |
24 | numPeers: number; |
25 | uploadSpeed: number; | |
26 | video: Video; | |
dc8bc31b | 27 | |
3ad109e4 C |
28 | private errorTimer: NodeJS.Timer; |
29 | private torrentInfosInterval: NodeJS.Timer; | |
dc8bc31b C |
30 | |
31 | constructor( | |
4fd8aa32 | 32 | private elementRef: ElementRef, |
ccf6ed16 | 33 | private routeParams: RouteParams, |
d3ef341a C |
34 | private videoService: VideoService, |
35 | private webTorrentService: WebTorrentService | |
36 | ) {} | |
dc8bc31b | 37 | |
3ad109e4 C |
38 | loadVideo() { |
39 | // Reset the error | |
40 | this.error = false; | |
41 | // We are loading the video | |
da932efc | 42 | this.loading = true; |
3ad109e4 | 43 | |
2c4a0b5d | 44 | console.log('Adding ' + this.video.magnetUri + '.'); |
d3ef341a | 45 | |
3ad109e4 C |
46 | // The callback might never return if there are network issues |
47 | // So we create a timer to inform the user the load is abnormally long | |
48 | this.errorTimer = setTimeout(() => this.loadTooLong(), VideoWatchComponent.LOADTIME_TOO_LONG); | |
49 | ||
d3ef341a | 50 | this.webTorrentService.add(this.video.magnetUri, (torrent) => { |
3ad109e4 C |
51 | // Clear the error timer |
52 | clearTimeout(this.errorTimer); | |
53 | // Maybe the error was fired by the timer, so reset it | |
54 | this.error = false; | |
55 | ||
56 | // We are not loading the video anymore | |
da932efc | 57 | this.loading = false; |
3ad109e4 | 58 | |
2c4a0b5d | 59 | console.log('Added ' + this.video.magnetUri + '.'); |
ccf6ed16 | 60 | torrent.files[0].appendTo(this.elementRef.nativeElement.querySelector('.embed-responsive'), (err) => { |
dc8bc31b C |
61 | if (err) { |
62 | alert('Cannot append the file.'); | |
63 | console.error(err); | |
64 | } | |
44124980 | 65 | }); |
8cfecb2a C |
66 | |
67 | // Refresh each second | |
3ad109e4 | 68 | this.torrentInfosInterval = setInterval(() => { |
8cfecb2a | 69 | this.downloadSpeed = torrent.downloadSpeed; |
8cfecb2a | 70 | this.numPeers = torrent.numPeers; |
4fd8aa32 | 71 | this.uploadSpeed = torrent.uploadSpeed; |
8cfecb2a | 72 | }, 1000); |
44124980 | 73 | }); |
dc8bc31b | 74 | } |
98b01bac | 75 | |
4fd8aa32 C |
76 | ngOnInit() { |
77 | let id = this.routeParams.get('id'); | |
78 | this.videoService.getVideo(id).subscribe( | |
3ad109e4 C |
79 | video => { |
80 | this.video = video; | |
81 | this.loadVideo(); | |
82 | }, | |
4fd8aa32 C |
83 | error => alert(error) |
84 | ); | |
85 | } | |
86 | ||
ccf6ed16 | 87 | routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) { |
98b01bac | 88 | console.log('Removing video from webtorrent.'); |
3ad109e4 | 89 | clearInterval(this.torrentInfosInterval); |
d3ef341a | 90 | this.webTorrentService.remove(this.video.magnetUri); |
98b01bac C |
91 | return true; |
92 | } | |
3ad109e4 C |
93 | |
94 | private loadTooLong() { | |
95 | this.error = true; | |
96 | console.error('The video load seems to be abnormally long.'); | |
97 | } | |
dc8bc31b | 98 | } |