1 import { Validators } from '@angular/common';
2 import { Component, ElementRef, OnInit } from '@angular/core';
3 import { FormControl, FormGroup, REACTIVE_FORM_DIRECTIVES } from '@angular/forms';
4 import { Router } from '@angular/router';
6 import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe';
7 import { PROGRESSBAR_DIRECTIVES } from 'ng2-bootstrap/components/progressbar';
8 import { FileSelectDirective, FileUploader } from 'ng2-file-upload/ng2-file-upload';
10 import { AuthService } from '../../shared';
13 selector: 'my-videos-add',
14 styles: [ require('./video-add.component.scss') ],
15 template: require('./video-add.component.html'),
16 directives: [ FileSelectDirective, PROGRESSBAR_DIRECTIVES, REACTIVE_FORM_DIRECTIVES ],
20 export class VideoAddComponent implements OnInit {
21 currentTag: string; // Tag the user is writing in the input
24 uploader: FileUploader;
32 private authService: AuthService,
33 private elementRef: ElementRef,
34 private router: Router
38 if (this.uploader.queue.length === 0) {
42 return this.uploader.queue[0].file.name;
45 get isTagsInputDisabled () {
46 return this.video.tags.length >= 3;
49 getInvalidFieldsTitle() {
51 const nameControl = this.videoForm.controls['name'];
52 const descriptionControl = this.videoForm.controls['description'];
54 if (!nameControl.valid) {
55 title += 'A name is required\n';
58 if (this.video.tags.length === 0) {
59 title += 'At least one tag is required\n';
62 if (this.filename === null) {
63 title += 'A file is required\n';
66 if (!descriptionControl.valid) {
67 title += 'A description is required\n';
74 this.videoForm = new FormGroup({
75 name: new FormControl('', [ <any>Validators.required, <any>Validators.minLength(3), <any>Validators.maxLength(50) ]),
76 description: new FormControl('', [ <any>Validators.required, <any>Validators.minLength(3), <any>Validators.maxLength(250) ]),
77 tags: new FormControl('', <any>Validators.pattern('^[a-zA-Z0-9]{2,10}$'))
81 this.uploader = new FileUploader({
82 authToken: this.authService.getRequestHeaderValue(),
84 url: '/api/v1/videos',
85 removeAfterUpload: true
88 this.uploader.onBuildItemForm = (item, form) => {
89 form.append('name', this.video.name);
90 form.append('description', this.video.description);
92 for (let i = 0; i < this.video.tags.length; i++) {
93 form.append(`tags[${i}]`, this.video.tags[i]);
98 onTagKeyPress(event: KeyboardEvent) {
100 if (event.keyCode === 13) {
101 // Check if the tag is valid and does not already exist
103 this.currentTag !== '' &&
104 this.videoForm.controls['tags'].valid &&
105 this.video.tags.indexOf(this.currentTag) === -1
107 this.video.tags.push(this.currentTag);
108 this.currentTag = '';
114 this.uploader.clearQueue();
117 removeTag(tag: string) {
118 this.video.tags.splice(this.video.tags.indexOf(tag), 1);
122 const item = this.uploader.queue[0];
123 // TODO: wait for https://github.com/valor-software/ng2-file-upload/pull/242
124 item.alias = 'videofile';
126 item.onSuccess = () => {
127 console.log('Video uploaded.');
129 // Print all the videos once it's finished
130 this.router.navigate(['/videos/list']);
133 item.onError = (response: string, status: number) => {
134 // We need to handle manually these cases beceause we use the FileUpload component
135 if (status === 400) {
136 this.error = response;
137 } else if (status === 401) {
138 this.error = 'Access token was expired, refreshing token...';
139 this.authService.refreshAccessToken().subscribe(
141 // Update the uploader request header
142 this.uploader.authToken = this.authService.getRequestHeaderValue();
143 this.error += ' access token refreshed. Please retry your request.';
147 this.error = 'Unknow error';
148 console.error(this.error);
153 this.uploader.uploadAll();