]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/video-add/video-add.component.ts
Client: improve host regex
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / video-add / video-add.component.ts
CommitLineData
230809ef 1import { Component, ElementRef, OnInit } from '@angular/core';
4b2f33f3 2import { FormBuilder, FormGroup } from '@angular/forms';
00a44645 3import { Router } from '@angular/router';
dc8bc31b 4
ab32b0fc 5import { FileUploader } from 'ng2-file-upload/ng2-file-upload';
8140a704 6
693b1aba
C
7import { AuthService } from '../../core';
8import { FormReactive, VIDEO_NAME, VIDEO_DESCRIPTION, VIDEO_TAGS } from '../../shared';
1553e15d 9
dc8bc31b
C
10@Component({
11 selector: 'my-videos-add',
ec8d8440
C
12 styleUrls: [ './video-add.component.scss' ],
13 templateUrl: './video-add.component.html'
dc8bc31b
C
14})
15
4b2f33f3
C
16export class VideoAddComponent extends FormReactive implements OnInit {
17 tags: string[] = [];
e822fdae 18 uploader: FileUploader;
4b2f33f3
C
19
20 error: string = null;
21 form: FormGroup;
22 formErrors = {
e822fdae 23 name: '',
4b2f33f3
C
24 description: '',
25 currentTag: ''
26 };
27 validationMessages = {
28 name: VIDEO_NAME.MESSAGES,
29 description: VIDEO_DESCRIPTION.MESSAGES,
30 currentTag: VIDEO_TAGS.MESSAGES
e822fdae 31 };
dc8bc31b 32
1553e15d 33 constructor(
9bfe96e1 34 private authService: AuthService,
4fd8aa32 35 private elementRef: ElementRef,
4b2f33f3 36 private formBuilder: FormBuilder,
9bfe96e1 37 private router: Router
4b2f33f3
C
38 ) {
39 super();
40 }
dc8bc31b 41
e822fdae
C
42 get filename() {
43 if (this.uploader.queue.length === 0) {
44 return null;
45 }
46
47 return this.uploader.queue[0].file.name;
48 }
49
4b2f33f3
C
50 buildForm() {
51 this.form = this.formBuilder.group({
52 name: [ '', VIDEO_NAME.VALIDATORS ],
53 description: [ '', VIDEO_DESCRIPTION.VALIDATORS ],
54 currentTag: [ '', VIDEO_TAGS.VALIDATORS ]
55 });
56
57 this.form.valueChanges.subscribe(data => this.onValueChanged(data));
e822fdae
C
58 }
59
60 getInvalidFieldsTitle() {
61 let title = '';
4b2f33f3
C
62 const nameControl = this.form.controls['name'];
63 const descriptionControl = this.form.controls['description'];
e822fdae
C
64
65 if (!nameControl.valid) {
66 title += 'A name is required\n';
67 }
68
4b2f33f3 69 if (this.tags.length === 0) {
e822fdae
C
70 title += 'At least one tag is required\n';
71 }
72
73 if (this.filename === null) {
74 title += 'A file is required\n';
75 }
76
77 if (!descriptionControl.valid) {
78 title += 'A description is required\n';
79 }
80
81 return title;
82 }
83
dc8bc31b 84 ngOnInit() {
e822fdae
C
85 this.uploader = new FileUploader({
86 authToken: this.authService.getRequestHeaderValue(),
87 queueLimit: 1,
98b01bac 88 url: '/api/v1/videos',
e822fdae 89 removeAfterUpload: true
dc8bc31b 90 });
e822fdae
C
91
92 this.uploader.onBuildItemForm = (item, form) => {
4b2f33f3
C
93 const name = this.form.value['name'];
94 const description = this.form.value['description'];
95
96 form.append('name', name);
97 form.append('description', description);
e822fdae 98
4b2f33f3
C
99 for (let i = 0; i < this.tags.length; i++) {
100 form.append(`tags[${i}]`, this.tags[i]);
e822fdae
C
101 }
102 };
4b2f33f3
C
103
104 this.buildForm();
e822fdae
C
105 }
106
107 onTagKeyPress(event: KeyboardEvent) {
4b2f33f3
C
108 const currentTag = this.form.value['currentTag'];
109
e822fdae
C
110 // Enter press
111 if (event.keyCode === 13) {
112 // Check if the tag is valid and does not already exist
113 if (
1a005042 114 currentTag.length >= 2 &&
4b2f33f3
C
115 this.form.controls['currentTag'].valid &&
116 this.tags.indexOf(currentTag) === -1
e822fdae 117 ) {
4b2f33f3
C
118 this.tags.push(currentTag);
119 this.form.patchValue({ currentTag: '' });
120
121 if (this.tags.length >= 3) {
122 this.form.get('currentTag').disable();
123 }
e822fdae
C
124 }
125 }
126 }
127
128 removeFile() {
129 this.uploader.clearQueue();
130 }
131
132 removeTag(tag: string) {
4b2f33f3 133 this.tags.splice(this.tags.indexOf(tag), 1);
46485303 134 this.form.get('currentTag').enable();
dc8bc31b
C
135 }
136
e822fdae
C
137 upload() {
138 const item = this.uploader.queue[0];
139 // TODO: wait for https://github.com/valor-software/ng2-file-upload/pull/242
140 item.alias = 'videofile';
141
1a005042
C
142 // FIXME: remove
143 // Run detection change for progress bar
144 const interval = setInterval(() => { ; }, 250);
145
e822fdae 146 item.onSuccess = () => {
1a005042
C
147 clearInterval(interval);
148
e822fdae
C
149 console.log('Video uploaded.');
150
151 // Print all the videos once it's finished
c6de16eb 152 this.router.navigate(['/videos/list']);
e822fdae
C
153 };
154
155 item.onError = (response: string, status: number) => {
1a005042
C
156 clearInterval(interval);
157
bd5c83a8
C
158 // We need to handle manually these cases beceause we use the FileUpload component
159 if (status === 400) {
160 this.error = response;
161 } else if (status === 401) {
162 this.error = 'Access token was expired, refreshing token...';
163 this.authService.refreshAccessToken().subscribe(
164 () => {
165 // Update the uploader request header
166 this.uploader.authToken = this.authService.getRequestHeaderValue();
167 this.error += ' access token refreshed. Please retry your request.';
168 }
169 );
170 } else {
171 this.error = 'Unknow error';
172 console.error(this.error);
173 }
e822fdae
C
174 };
175
e822fdae 176 this.uploader.uploadAll();
dc8bc31b
C
177 }
178}