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-03-14 12:50:19 +00:00
import { Component, OnInit, ElementRef } from 'angular2/core';
2016-03-14 21:16:43 +00:00
import { RouteParams, CanDeactivate, ComponentInstruction } from 'angular2/router';
2016-05-03 18:23:23 +00:00
import { BytesPipe } from 'angular-pipes/math';
2016-03-14 12:50:19 +00:00
2016-03-14 21:16:43 +00:00
// TODO import it with systemjs
2016-03-14 12:50:19 +00: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 12:50:19 +00:00
})
2016-03-14 21:16:43 +00:00
export class VideosWatchComponent implements OnInit, CanDeactivate {
2016-03-14 12:50:19 +00:00
video: Video;
downloadSpeed: number;
uploadSpeed: number;
numPeers: number;
2016-04-29 12:18:14 +00:00
loading: boolean = false;
2016-03-14 12:50:19 +00:00
private _interval: number;
2016-03-14 12:50:19 +00:00
private client: any;
constructor(
private _videosService: VideosService,
private _routeParams: RouteParams,
private _elementRef: ElementRef
) {
2016-03-14 21:16:43 +00:00
// TODO: use a service
2016-03-14 12:50:19 +00: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 12:18:14 +00:00
this.loading = true;
2016-03-14 12:50:19 +00:00
this.video = video;
2016-03-15 12:16:54 +00:00
console.log('Adding ' + this.video.magnetUri + '.');
2016-03-14 12:50:19 +00:00
this.client.add(this.video.magnetUri, (torrent) => {
2016-04-29 12:18:14 +00:00
this.loading = false;
2016-03-15 12:16:54 +00:00
console.log('Added ' + this.video.magnetUri + '.');
2016-04-28 17:55:41 +00:00
torrent.files[0].appendTo(this._elementRef.nativeElement.querySelector('.embed-responsive'), (err) => {
2016-03-14 12:50:19 +00:00
if (err) {
alert('Cannot append the file.');
console.error(err);
}
2016-04-08 18:58:07 +00:00
});
// Refresh each second
this._interval = setInterval(() => {
this.downloadSpeed = torrent.downloadSpeed;
this.uploadSpeed = torrent.uploadSpeed;
this.numPeers = torrent.numPeers;
}, 1000);
2016-04-08 18:58:07 +00:00
});
2016-03-14 12:50:19 +00:00
}
2016-03-14 21:16:43 +00:00
routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) : any {
console.log('Removing video from webtorrent.');
2016-04-28 18:47:01 +00:00
clearInterval(this._interval);
2016-03-14 21:16:43 +00:00
this.client.remove(this.video.magnetUri);
return true;
}
2016-03-14 12:50:19 +00:00
}