aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/videos/video-add/video-add.component.ts
blob: 8df4f951be603322bc516e3ebc105cf8cd309976 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/// <reference path="../../../../typings/globals/jquery/index.d.ts" />
/// <reference path="../../../../typings/globals/jquery.fileupload/index.d.ts" />

import { Component, ElementRef, OnInit } from '@angular/core';
import { Router } from '@angular/router-deprecated';

import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe';
import { PROGRESSBAR_DIRECTIVES } from 'ng2-bootstrap/components/progressbar';

import { AuthService, User } from '../../shared';

@Component({
  selector: 'my-videos-add',
  styles: [ require('./video-add.component.scss') ],
  template: require('./video-add.component.html'),
  directives: [ PROGRESSBAR_DIRECTIVES ],
  pipes: [ BytesPipe ]
})

export class VideoAddComponent implements OnInit {
  fileToUpload: any;
  progressBar: { value: number; max: number; } = { value: 0, max: 0 };
  user: User;

  private form: any;

  constructor(
    private authService: AuthService,
    private elementRef: ElementRef,
    private router: Router
  ) {}

  ngOnInit() {
    this.user = User.load();
    jQuery(this.elementRef.nativeElement).find('#videofile').fileupload({
      url: '/api/v1/videos',
      dataType: 'json',
      singleFileUploads: true,
      multipart: true,
      autoUpload: false,

      add: (e, data) => {
        this.form = data;
        this.fileToUpload = data['files'][0];
      },

      progressall: (e, data) => {
        this.progressBar.value = data.loaded;
        // 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);
      },

      done: (e, data) => {
        this.progressBar.value = this.progressBar.max;
        console.log('Video uploaded.');

        // Print all the videos once it's finished
        this.router.navigate(['VideosList']);
      }
    });
  }

  uploadFile() {
    this.form.formData = jQuery(this.elementRef.nativeElement).find('form').serializeArray();
    this.form.headers = this.authService.getRequestHeader().toJSON();
    this.form.submit();
  }
}