]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/video-watch/video-watch.component.ts
Add tags support to the video list
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / video-watch / video-watch.component.ts
1 import { Component, ElementRef, OnInit } from '@angular/core';
2 import { CanDeactivate, RouteSegment } from '@angular/router';
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 private static LOADTIME_TOO_LONG: number = 30000;
20
21 downloadSpeed: number;
22 error: boolean = false;
23 loading: boolean = false;
24 numPeers: number;
25 uploadSpeed: number;
26 video: Video;
27
28 private errorTimer: NodeJS.Timer;
29 private torrentInfosInterval: NodeJS.Timer;
30
31 constructor(
32 private elementRef: ElementRef,
33 private routeSegment: RouteSegment,
34 private videoService: VideoService,
35 private webTorrentService: WebTorrentService
36 ) {}
37
38 loadVideo() {
39 // Reset the error
40 this.error = false;
41 // We are loading the video
42 this.loading = true;
43
44 console.log('Adding ' + this.video.magnetUri + '.');
45
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
50 this.webTorrentService.add(this.video.magnetUri, (torrent) => {
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
57 this.loading = false;
58
59 console.log('Added ' + this.video.magnetUri + '.');
60 torrent.files[0].appendTo(this.elementRef.nativeElement.querySelector('.embed-responsive'), (err) => {
61 if (err) {
62 alert('Cannot append the file.');
63 console.error(err);
64 }
65 });
66
67 // Refresh each second
68 this.torrentInfosInterval = setInterval(() => {
69 this.downloadSpeed = torrent.downloadSpeed;
70 this.numPeers = torrent.numPeers;
71 this.uploadSpeed = torrent.uploadSpeed;
72 }, 1000);
73 });
74 }
75
76 ngOnInit() {
77 let id = this.routeSegment.getParam('id');
78 this.videoService.getVideo(id).subscribe(
79 video => {
80 this.video = video;
81 this.loadVideo();
82 },
83 error => alert(error)
84 );
85 }
86
87 routerCanDeactivate() {
88 console.log('Removing video from webtorrent.');
89 clearInterval(this.torrentInfosInterval);
90 this.webTorrentService.remove(this.video.magnetUri);
91 return Promise.resolve(true);
92 }
93
94 private loadTooLong() {
95 this.error = true;
96 console.error('The video load seems to be abnormally long.');
97 }
98 }