PeerTube/client/src/app/videos/video-watch/video-watch.component.ts

73 lines
2.1 KiB
TypeScript
Raw Normal View History

import { Component, ElementRef, OnInit } from '@angular/core';
import { CanDeactivate, ComponentInstruction, RouteParams } from '@angular/router-deprecated';
2016-05-13 14:18:37 +02:00
import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe';
2016-03-14 13:50:19 +01:00
import { LoaderComponent, Video, VideoService } from '../shared';
2016-05-31 22:39:36 +02:00
import { WebTorrentService } from './webtorrent.service';
2016-03-14 13:50:19 +01:00
@Component({
selector: 'my-video-watch',
template: require('./video-watch.component.html'),
styles: [ require('./video-watch.component.scss') ],
2016-05-31 22:39:36 +02:00
providers: [ WebTorrentService ],
directives: [ LoaderComponent ],
pipes: [ BytesPipe ]
2016-03-14 13:50:19 +01:00
})
export class VideoWatchComponent implements OnInit, CanDeactivate {
downloadSpeed: number;
2016-04-29 14:18:14 +02:00
loading: boolean = false;
2016-05-27 17:49:18 +02:00
numPeers: number;
uploadSpeed: number;
video: Video;
2016-03-14 13:50:19 +01:00
2016-05-27 17:49:18 +02:00
private interval: NodeJS.Timer;
2016-03-14 13:50:19 +01:00
constructor(
2016-05-27 17:49:18 +02:00
private elementRef: ElementRef,
2016-05-27 17:25:52 +02:00
private routeParams: RouteParams,
2016-05-31 22:39:36 +02:00
private videoService: VideoService,
private webTorrentService: WebTorrentService
) {}
2016-03-14 13:50:19 +01:00
loadVideo(video: Video) {
2016-04-29 14:18:14 +02:00
this.loading = true;
2016-03-14 13:50:19 +01:00
this.video = video;
2016-03-15 13:16:54 +01:00
console.log('Adding ' + this.video.magnetUri + '.');
2016-05-31 22:39:36 +02:00
this.webTorrentService.add(this.video.magnetUri, (torrent) => {
2016-04-29 14:18:14 +02:00
this.loading = false;
2016-03-15 13:16:54 +01:00
console.log('Added ' + this.video.magnetUri + '.');
2016-05-27 17:25:52 +02:00
torrent.files[0].appendTo(this.elementRef.nativeElement.querySelector('.embed-responsive'), (err) => {
2016-03-14 13:50:19 +01:00
if (err) {
alert('Cannot append the file.');
console.error(err);
}
2016-04-08 20:58:07 +02:00
});
// Refresh each second
2016-05-27 17:25:52 +02:00
this.interval = setInterval(() => {
this.downloadSpeed = torrent.downloadSpeed;
this.numPeers = torrent.numPeers;
2016-05-27 17:49:18 +02:00
this.uploadSpeed = torrent.uploadSpeed;
}, 1000);
2016-04-08 20:58:07 +02:00
});
2016-03-14 13:50:19 +01:00
}
2016-03-14 22:16:43 +01:00
2016-05-27 17:49:18 +02:00
ngOnInit() {
let id = this.routeParams.get('id');
this.videoService.getVideo(id).subscribe(
video => this.loadVideo(video),
error => alert(error)
);
}
2016-05-27 17:25:52 +02:00
routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
2016-03-14 22:16:43 +01:00
console.log('Removing video from webtorrent.');
2016-05-27 17:25:52 +02:00
clearInterval(this.interval);
2016-05-31 22:39:36 +02:00
this.webTorrentService.remove(this.video.magnetUri);
2016-03-14 22:16:43 +01:00
return true;
}
2016-03-14 13:50:19 +01:00
}