]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/video-watch/video-watch.component.ts
Client: fix prod build process
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / video-watch / video-watch.component.ts
CommitLineData
c323efb9 1import { Component, ElementRef, NgZone, OnDestroy, OnInit } from '@angular/core';
0629423c 2import { ActivatedRoute } from '@angular/router';
8140a704 3
ab32b0fc 4import { Video, VideoService } from '../shared';
d3ef341a 5import { WebTorrentService } from './webtorrent.service';
dc8bc31b 6
dc8bc31b
C
7@Component({
8 selector: 'my-video-watch',
4a6995be 9 template: require('./video-watch.component.html'),
ab32b0fc 10 styles: [ require('./video-watch.component.scss') ]
dc8bc31b
C
11})
12
0629423c 13export class VideoWatchComponent implements OnInit, OnDestroy {
3ad109e4
C
14 private static LOADTIME_TOO_LONG: number = 30000;
15
8cfecb2a 16 downloadSpeed: number;
3ad109e4 17 error: boolean = false;
da932efc 18 loading: boolean = false;
4fd8aa32
C
19 numPeers: number;
20 uploadSpeed: number;
21 video: Video;
dc8bc31b 22
3ad109e4 23 private errorTimer: NodeJS.Timer;
0629423c 24 private sub: any;
3ad109e4 25 private torrentInfosInterval: NodeJS.Timer;
dc8bc31b
C
26
27 constructor(
4fd8aa32 28 private elementRef: ElementRef,
c323efb9 29 private ngZone: NgZone,
0629423c 30 private route: ActivatedRoute,
d3ef341a
C
31 private videoService: VideoService,
32 private webTorrentService: WebTorrentService
33 ) {}
dc8bc31b 34
3ad109e4
C
35 loadVideo() {
36 // Reset the error
37 this.error = false;
38 // We are loading the video
da932efc 39 this.loading = true;
3ad109e4 40
2c4a0b5d 41 console.log('Adding ' + this.video.magnetUri + '.');
d3ef341a 42
3ad109e4
C
43 // The callback might never return if there are network issues
44 // So we create a timer to inform the user the load is abnormally long
45 this.errorTimer = setTimeout(() => this.loadTooLong(), VideoWatchComponent.LOADTIME_TOO_LONG);
46
d3ef341a 47 this.webTorrentService.add(this.video.magnetUri, (torrent) => {
3ad109e4
C
48 // Clear the error timer
49 clearTimeout(this.errorTimer);
50 // Maybe the error was fired by the timer, so reset it
51 this.error = false;
52
53 // We are not loading the video anymore
da932efc 54 this.loading = false;
3ad109e4 55
2c4a0b5d 56 console.log('Added ' + this.video.magnetUri + '.');
ccf6ed16 57 torrent.files[0].appendTo(this.elementRef.nativeElement.querySelector('.embed-responsive'), (err) => {
dc8bc31b
C
58 if (err) {
59 alert('Cannot append the file.');
60 console.error(err);
61 }
44124980 62 });
8cfecb2a 63
c323efb9 64 this.runInProgress(torrent);
44124980 65 });
dc8bc31b 66 }
98b01bac 67
0629423c 68 ngOnDestroy() {
98b01bac 69 console.log('Removing video from webtorrent.');
3ad109e4 70 clearInterval(this.torrentInfosInterval);
d3ef341a 71 this.webTorrentService.remove(this.video.magnetUri);
0629423c
C
72
73 this.sub.unsubscribe();
74 }
75
76 ngOnInit() {
77 this.sub = this.route.params.subscribe(routeParams => {
78 let id = routeParams['id'];
79 this.videoService.getVideo(id).subscribe(
80 video => {
81 this.video = video;
82 this.loadVideo();
83 },
bf68dd75 84 error => alert(error.text)
0629423c
C
85 );
86 });
98b01bac 87 }
3ad109e4
C
88
89 private loadTooLong() {
90 this.error = true;
91 console.error('The video load seems to be abnormally long.');
92 }
c323efb9
C
93
94 private runInProgress(torrent: any) {
95 // Refresh each second
96 this.torrentInfosInterval = setInterval(() => {
97 this.ngZone.run(() => {
98 this.downloadSpeed = torrent.downloadSpeed;
99 this.numPeers = torrent.numPeers;
100 this.uploadSpeed = torrent.uploadSpeed;
101 });
102 }, 1000);
103 }
dc8bc31b 104}