1
0
Fork 0
peertube/client/angular/videos/components/watch/videos-watch.component.ts

75 lines
2.2 KiB
TypeScript
Raw Normal View History

2016-05-13 08:18:37 -04:00
import { Component, OnInit, ElementRef } from '@angular/core';
import { RouteParams, CanDeactivate, ComponentInstruction } from '@angular/router-deprecated';
import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe';
2016-03-14 08:50:19 -04:00
2016-03-14 17:16:43 -04:00
// TODO import it with systemjs
2016-03-14 08:50:19 -04:00
declare var WebTorrent: any;
import { Video } from '../../models/video';
import { VideosService } from '../../services/videos.service';
@Component({
selector: 'my-video-watch',
templateUrl: 'app/angular/videos/components/watch/videos-watch.component.html',
styleUrls: [ 'app/angular/videos/components/watch/videos-watch.component.css' ],
pipes: [ BytesPipe ]
2016-03-14 08:50:19 -04:00
})
2016-03-14 17:16:43 -04:00
export class VideosWatchComponent implements OnInit, CanDeactivate {
2016-03-14 08:50:19 -04:00
video: Video;
downloadSpeed: number;
uploadSpeed: number;
numPeers: number;
2016-04-29 08:18:14 -04:00
loading: boolean = false;
2016-03-14 08:50:19 -04:00
private _interval: NodeJS.Timer;
2016-03-14 08:50:19 -04:00
private client: any;
constructor(
private _videosService: VideosService,
private _routeParams: RouteParams,
private _elementRef: ElementRef
) {
2016-03-14 17:16:43 -04:00
// TODO: use a service
2016-03-14 08:50:19 -04:00
this.client = new WebTorrent({ dht: false });
}
ngOnInit() {
let id = this._routeParams.get('id');
this._videosService.getVideo(id).subscribe(
video => this.loadVideo(video),
error => alert(error)
);
}
loadVideo(video: Video) {
2016-04-29 08:18:14 -04:00
this.loading = true;
2016-03-14 08:50:19 -04:00
this.video = video;
2016-03-15 08:16:54 -04:00
console.log('Adding ' + this.video.magnetUri + '.');
2016-03-14 08:50:19 -04:00
this.client.add(this.video.magnetUri, (torrent) => {
2016-04-29 08:18:14 -04:00
this.loading = false;
2016-03-15 08:16:54 -04:00
console.log('Added ' + this.video.magnetUri + '.');
2016-04-28 13:55:41 -04:00
torrent.files[0].appendTo(this._elementRef.nativeElement.querySelector('.embed-responsive'), (err) => {
2016-03-14 08:50:19 -04:00
if (err) {
alert('Cannot append the file.');
console.error(err);
}
2016-04-08 14:58:07 -04:00
});
// Refresh each second
this._interval = setInterval(() => {
this.downloadSpeed = torrent.downloadSpeed;
this.uploadSpeed = torrent.uploadSpeed;
this.numPeers = torrent.numPeers;
}, 1000);
2016-04-08 14:58:07 -04:00
});
2016-03-14 08:50:19 -04:00
}
2016-03-14 17:16:43 -04:00
routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) : any {
console.log('Removing video from webtorrent.');
2016-04-28 14:47:01 -04:00
clearInterval(this._interval);
2016-03-14 17:16:43 -04:00
this.client.remove(this.video.magnetUri);
return true;
}
2016-03-14 08:50:19 -04:00
}