1 import { Component, ElementRef, OnInit } from '@angular/core';
2 import { FormBuilder, FormGroup } from '@angular/forms';
3 import { Router } from '@angular/router';
5 import { FileUploader } from 'ng2-file-upload/ng2-file-upload';
6 import { NotificationsService } from 'angular2-notifications';
8 import { AuthService } from '../../core';
17 } from '../../shared';
18 import { VideoService } from '../shared';
21 selector: 'my-videos-add',
22 styleUrls: [ './video-edit.component.scss' ],
23 templateUrl: './video-add.component.html'
26 export class VideoAddComponent extends FormReactive implements OnInit {
28 uploader: FileUploader;
33 tagValidators = VIDEO_TAGS.VALIDATORS;
34 tagValidatorsMessages = VIDEO_TAGS.MESSAGES;
45 validationMessages = {
46 name: VIDEO_NAME.MESSAGES,
47 category: VIDEO_CATEGORY.MESSAGES,
48 licence: VIDEO_LICENCE.MESSAGES,
49 language: VIDEO_LANGUAGE.MESSAGES,
50 description: VIDEO_DESCRIPTION.MESSAGES
53 // Special error messages
57 private authService: AuthService,
58 private elementRef: ElementRef,
59 private formBuilder: FormBuilder,
60 private router: Router,
61 private notificationsService: NotificationsService,
62 private videoService: VideoService
68 if (this.uploader.queue.length === 0) {
72 return this.uploader.queue[0].file.name;
76 this.form = this.formBuilder.group({
77 name: [ '', VIDEO_NAME.VALIDATORS ],
79 category: [ '', VIDEO_CATEGORY.VALIDATORS ],
80 licence: [ '', VIDEO_LICENCE.VALIDATORS ],
81 language: [ '', VIDEO_LANGUAGE.VALIDATORS ],
82 description: [ '', VIDEO_DESCRIPTION.VALIDATORS ],
86 this.form.valueChanges.subscribe(data => this.onValueChanged(data));
90 this.videoCategories = this.videoService.videoCategories;
91 this.videoLicences = this.videoService.videoLicences;
92 this.videoLanguages = this.videoService.videoLanguages;
94 this.uploader = new FileUploader({
95 authToken: this.authService.getRequestHeaderValue(),
97 url: '/api/v1/videos',
98 removeAfterUpload: true
101 this.uploader.onBuildItemForm = (item, form) => {
102 const name = this.form.value['name'];
103 const nsfw = this.form.value['nsfw'];
104 const category = this.form.value['category'];
105 const licence = this.form.value['licence'];
106 const language = this.form.value['language'];
107 const description = this.form.value['description'];
108 const tags = this.form.value['tags'];
110 form.append('name', name);
111 form.append('category', category);
112 form.append('nsfw', nsfw);
113 form.append('licence', licence);
115 // Language is optional
117 form.append('language', language);
120 form.append('description', description);
122 for (let i = 0; i < tags.length; i++) {
123 form.append(`tags[${i}]`, tags[i]);
133 if (this.filename === null) {
134 this.fileError = 'You did not add a file.';
137 return this.form.valid === true && this.fileError === '';
145 this.uploader.clearQueue();
149 if (this.checkForm() === false) {
153 const item = this.uploader.queue[0];
154 // TODO: wait for https://github.com/valor-software/ng2-file-upload/pull/242
155 item.alias = 'videofile';
158 // Run detection change for progress bar
159 const interval = setInterval(() => { ; }, 250);
161 item.onSuccess = () => {
162 clearInterval(interval);
164 console.log('Video uploaded.');
165 this.notificationsService.success('Success', 'Video uploaded.');
168 // Print all the videos once it's finished
169 this.router.navigate(['/videos/list']);
172 item.onError = (response: string, status: number) => {
173 clearInterval(interval);
175 // We need to handle manually these cases beceause we use the FileUpload component
176 if (status === 400) {
177 this.error = response;
178 } else if (status === 401) {
179 this.error = 'Access token was expired, refreshing token...';
180 this.authService.refreshAccessToken().subscribe(
182 // Update the uploader request header
183 this.uploader.authToken = this.authService.getRequestHeaderValue();
184 this.error += ' access token refreshed. Please retry your request.';
188 this.error = 'Unknow error';
189 console.error(this.error);
193 this.uploader.uploadAll();