]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/app/videos/video-add/video-add.component.ts
Follow the angular styleguide for the directories structure
[github/Chocobozzz/PeerTube.git] / client / app / videos / video-add / video-add.component.ts
CommitLineData
230809ef
C
1import { Component, ElementRef, OnInit } from '@angular/core';
2import { Router } from '@angular/router-deprecated';
dc8bc31b 3
8140a704 4import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe';
41a2aee3 5import { PROGRESSBAR_DIRECTIVES } from 'ng2-bootstrap/components/progressbar';
8140a704 6
41a2aee3 7import { AuthService, User } from '../../users/index';
1553e15d 8
98b01bac 9// TODO: import it with systemjs
dc8bc31b
C
10declare var jQuery:any;
11
12@Component({
13 selector: 'my-videos-add',
41a2aee3
C
14 styleUrls: [ 'client/app/videos/video-add/video-add.component.css' ],
15 templateUrl: 'client/app/videos/video-add/video-add.component.html',
8140a704
C
16 directives: [ PROGRESSBAR_DIRECTIVES ],
17 pipes: [ BytesPipe ]
dc8bc31b
C
18})
19
41a2aee3 20export class VideoAddComponent implements OnInit {
1553e15d 21 user: User;
dc8bc31b
C
22 fileToUpload: any;
23 progressBar: { value: number; max: number; } = { value: 0, max: 0 };
24
25 private _form: any;
26
1553e15d
C
27 constructor(
28 private _router: Router, private _elementRef: ElementRef,
29 private _authService: AuthService
30 ) {}
dc8bc31b
C
31
32 ngOnInit() {
1553e15d 33 this.user = User.load();
8c9c1942 34 jQuery(this._elementRef.nativeElement).find('#videofile').fileupload({
98b01bac
C
35 url: '/api/v1/videos',
36 dataType: 'json',
dc8bc31b
C
37 singleFileUploads: true,
38 multipart: true,
dc8bc31b
C
39 autoupload: false,
40
41 add: (e, data) => {
42 this._form = data;
43 this.fileToUpload = data['files'][0];
44 },
45
46 progressall: (e, data) => {
47 this.progressBar.value = data.loaded;
86e054b2
C
48 // The server is a little bit slow to answer (has to seed the video)
49 // So we add more time to the progress bar (+10%)
50 this.progressBar.max = data.total + (0.1 * data.total);
dc8bc31b
C
51 },
52
53 done: (e, data) => {
86e054b2 54 this.progressBar.value = this.progressBar.max;
98b01bac
C
55 console.log('Video uploaded.');
56
dc8bc31b
C
57 // Print all the videos once it's finished
58 this._router.navigate(['VideosList']);
59 }
60 });
61 }
62
63 uploadFile() {
1553e15d 64 this._form.headers = this._authService.getRequestHeader().toJSON();
dc8bc31b
C
65 this._form.formData = jQuery(this._elementRef.nativeElement).find('form').serializeArray();
66 this._form.submit();
67 }
68}