1
0
Fork 0
peertube/client/app/videos/video-add/video-add.component.ts

70 lines
2.0 KiB
TypeScript
Raw Normal View History

2016-05-13 12:18:37 +00:00
import { Component, ElementRef, OnInit } from '@angular/core';
import { Router } from '@angular/router-deprecated';
2016-03-14 12:50:19 +00:00
import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe';
import { PROGRESSBAR_DIRECTIVES } from 'ng2-bootstrap/components/progressbar';
import { AuthService, User } from '../../users/index';
2016-03-14 21:16:43 +00:00
// TODO: import it with systemjs
2016-05-27 15:25:52 +00:00
declare var jQuery: any;
2016-03-14 12:50:19 +00:00
@Component({
selector: 'my-videos-add',
styleUrls: [ 'client/app/videos/video-add/video-add.component.css' ],
templateUrl: 'client/app/videos/video-add/video-add.component.html',
directives: [ PROGRESSBAR_DIRECTIVES ],
pipes: [ BytesPipe ]
2016-03-14 12:50:19 +00:00
})
export class VideoAddComponent implements OnInit {
2016-03-14 12:50:19 +00:00
fileToUpload: any;
progressBar: { value: number; max: number; } = { value: 0, max: 0 };
2016-05-27 15:49:18 +00:00
user: User;
2016-03-14 12:50:19 +00:00
2016-05-27 15:49:18 +00:00
private form: any;
2016-03-14 12:50:19 +00:00
constructor(
2016-05-27 15:49:18 +00:00
private router: Router,
private elementRef: ElementRef,
private authService: AuthService
) {}
2016-03-14 12:50:19 +00:00
ngOnInit() {
this.user = User.load();
2016-05-27 15:49:18 +00:00
jQuery(this.elementRef.nativeElement).find('#videofile').fileupload({
2016-03-14 21:16:43 +00:00
url: '/api/v1/videos',
dataType: 'json',
2016-03-14 12:50:19 +00:00
singleFileUploads: true,
multipart: true,
autoupload: false,
add: (e, data) => {
2016-05-27 15:49:18 +00:00
this.form = data;
2016-03-14 12:50:19 +00:00
this.fileToUpload = data['files'][0];
},
progressall: (e, data) => {
this.progressBar.value = data.loaded;
2016-03-16 20:22:21 +00:00
// The server is a little bit slow to answer (has to seed the video)
// So we add more time to the progress bar (+10%)
this.progressBar.max = data.total + (0.1 * data.total);
2016-03-14 12:50:19 +00:00
},
done: (e, data) => {
2016-03-16 20:22:21 +00:00
this.progressBar.value = this.progressBar.max;
2016-03-14 21:16:43 +00:00
console.log('Video uploaded.');
2016-03-14 12:50:19 +00:00
// Print all the videos once it's finished
2016-05-27 15:49:18 +00:00
this.router.navigate(['VideosList']);
2016-03-14 12:50:19 +00:00
}
});
}
uploadFile() {
2016-05-27 15:49:18 +00:00
this.form.headers = this.authService.getRequestHeader().toJSON();
this.form.formData = jQuery(this.elementRef.nativeElement).find('form').serializeArray();
this.form.submit();
2016-03-14 12:50:19 +00:00
}
}