2016-05-27 14:23:10 +00:00
|
|
|
import { Component, ElementRef, OnInit } from '@angular/core';
|
|
|
|
import { CanDeactivate, ComponentInstruction, RouteParams } from '@angular/router-deprecated';
|
2016-05-23 07:50:26 +00:00
|
|
|
|
2016-05-13 12:18:37 +00:00
|
|
|
import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe';
|
2016-03-14 12:50:19 +00:00
|
|
|
|
2016-06-03 20:08:03 +00:00
|
|
|
import { LoaderComponent, Video, VideoService } from '../shared';
|
2016-05-31 20:39:36 +00:00
|
|
|
import { WebTorrentService } from './webtorrent.service';
|
2016-03-14 12:50:19 +00:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-video-watch',
|
2016-06-03 20:08:03 +00:00
|
|
|
template: require('./video-watch.component.html'),
|
|
|
|
styles: [ require('./video-watch.component.scss') ],
|
2016-05-31 20:39:36 +00:00
|
|
|
providers: [ WebTorrentService ],
|
2016-05-26 18:40:03 +00:00
|
|
|
directives: [ LoaderComponent ],
|
2016-04-28 18:40:56 +00:00
|
|
|
pipes: [ BytesPipe ]
|
2016-03-14 12:50:19 +00:00
|
|
|
})
|
|
|
|
|
2016-05-27 14:23:10 +00:00
|
|
|
export class VideoWatchComponent implements OnInit, CanDeactivate {
|
2016-04-28 18:40:56 +00:00
|
|
|
downloadSpeed: number;
|
2016-04-29 12:18:14 +00:00
|
|
|
loading: boolean = false;
|
2016-05-27 15:49:18 +00:00
|
|
|
numPeers: number;
|
|
|
|
uploadSpeed: number;
|
|
|
|
video: Video;
|
2016-03-14 12:50:19 +00:00
|
|
|
|
2016-05-27 15:49:18 +00:00
|
|
|
private interval: NodeJS.Timer;
|
2016-03-14 12:50:19 +00:00
|
|
|
|
|
|
|
constructor(
|
2016-05-27 15:49:18 +00:00
|
|
|
private elementRef: ElementRef,
|
2016-05-27 15:25:52 +00:00
|
|
|
private routeParams: RouteParams,
|
2016-05-31 20:39:36 +00:00
|
|
|
private videoService: VideoService,
|
|
|
|
private webTorrentService: WebTorrentService
|
|
|
|
) {}
|
2016-03-14 12:50:19 +00:00
|
|
|
|
|
|
|
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-05-31 20:39:36 +00:00
|
|
|
|
|
|
|
this.webTorrentService.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-05-27 15:25:52 +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
|
|
|
});
|
2016-04-28 18:40:56 +00:00
|
|
|
|
|
|
|
// Refresh each second
|
2016-05-27 15:25:52 +00:00
|
|
|
this.interval = setInterval(() => {
|
2016-04-28 18:40:56 +00:00
|
|
|
this.downloadSpeed = torrent.downloadSpeed;
|
|
|
|
this.numPeers = torrent.numPeers;
|
2016-05-27 15:49:18 +00:00
|
|
|
this.uploadSpeed = torrent.uploadSpeed;
|
2016-04-28 18:40:56 +00:00
|
|
|
}, 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
|
|
|
|
2016-05-27 15:49:18 +00:00
|
|
|
ngOnInit() {
|
|
|
|
let id = this.routeParams.get('id');
|
|
|
|
this.videoService.getVideo(id).subscribe(
|
|
|
|
video => this.loadVideo(video),
|
|
|
|
error => alert(error)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-05-27 15:25:52 +00:00
|
|
|
routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
|
2016-03-14 21:16:43 +00:00
|
|
|
console.log('Removing video from webtorrent.');
|
2016-05-27 15:25:52 +00:00
|
|
|
clearInterval(this.interval);
|
2016-05-31 20:39:36 +00:00
|
|
|
this.webTorrentService.remove(this.video.magnetUri);
|
2016-03-14 21:16:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-03-14 12:50:19 +00:00
|
|
|
}
|