]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
41a2aee3 1import { Component, ElementRef, OnInit } from '@angular/core';
00a44645 2import { CanDeactivate, RouteSegment } from '@angular/router';
8140a704 3
230809ef 4import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe';
dc8bc31b 5
4a6995be 6import { LoaderComponent, Video, VideoService } from '../shared';
d3ef341a 7import { 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 18export 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,
00a44645 33 private routeSegment: RouteSegment,
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 76 ngOnInit() {
00a44645 77 let id = this.routeSegment.getParam('id');
4fd8aa32 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
00a44645 87 routerCanDeactivate() {
98b01bac 88 console.log('Removing video from webtorrent.');
3ad109e4 89 clearInterval(this.torrentInfosInterval);
d3ef341a 90 this.webTorrentService.remove(this.video.magnetUri);
00a44645 91 return Promise.resolve(true);
98b01bac 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}