]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/video-add/video-add.component.ts
Server: Add NSFW in user profile
[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';
7ddd02c9 6import { NotificationsService } from 'angular2-notifications';
8140a704 7
693b1aba 8import { AuthService } from '../../core';
6e07c3de
C
9import {
10 FormReactive,
11 VIDEO_NAME,
12 VIDEO_CATEGORY,
d07137b9 13 VIDEO_LICENCE,
6e07c3de
C
14 VIDEO_DESCRIPTION,
15 VIDEO_TAGS
16} from '../../shared';
17import { VideoService } from '../shared';
1553e15d 18
dc8bc31b
C
19@Component({
20 selector: 'my-videos-add',
ec8d8440
C
21 styleUrls: [ './video-add.component.scss' ],
22 templateUrl: './video-add.component.html'
dc8bc31b
C
23})
24
4b2f33f3
C
25export class VideoAddComponent extends FormReactive implements OnInit {
26 tags: string[] = [];
e822fdae 27 uploader: FileUploader;
6e07c3de 28 videoCategories = [];
d07137b9 29 videoLicences = [];
4b2f33f3
C
30
31 error: string = null;
32 form: FormGroup;
33 formErrors = {
e822fdae 34 name: '',
6e07c3de 35 category: '',
d07137b9 36 licence: '',
4b2f33f3
C
37 description: '',
38 currentTag: ''
39 };
40 validationMessages = {
41 name: VIDEO_NAME.MESSAGES,
6e07c3de 42 category: VIDEO_CATEGORY.MESSAGES,
d07137b9 43 licence: VIDEO_LICENCE.MESSAGES,
4b2f33f3
C
44 description: VIDEO_DESCRIPTION.MESSAGES,
45 currentTag: VIDEO_TAGS.MESSAGES
e822fdae 46 };
dc8bc31b 47
bf57d5ee
C
48 // Special error messages
49 tagsError = '';
50 fileError = '';
51
1553e15d 52 constructor(
9bfe96e1 53 private authService: AuthService,
4fd8aa32 54 private elementRef: ElementRef,
4b2f33f3 55 private formBuilder: FormBuilder,
7ddd02c9 56 private router: Router,
6e07c3de
C
57 private notificationsService: NotificationsService,
58 private videoService: VideoService
4b2f33f3
C
59 ) {
60 super();
61 }
dc8bc31b 62
e822fdae
C
63 get filename() {
64 if (this.uploader.queue.length === 0) {
65 return null;
66 }
67
68 return this.uploader.queue[0].file.name;
69 }
70
4b2f33f3
C
71 buildForm() {
72 this.form = this.formBuilder.group({
73 name: [ '', VIDEO_NAME.VALIDATORS ],
6e07c3de 74 category: [ '', VIDEO_CATEGORY.VALIDATORS ],
d07137b9 75 licence: [ '', VIDEO_LICENCE.VALIDATORS ],
4b2f33f3
C
76 description: [ '', VIDEO_DESCRIPTION.VALIDATORS ],
77 currentTag: [ '', VIDEO_TAGS.VALIDATORS ]
78 });
79
80 this.form.valueChanges.subscribe(data => this.onValueChanged(data));
e822fdae
C
81 }
82
dc8bc31b 83 ngOnInit() {
6e07c3de 84 this.videoCategories = this.videoService.videoCategories;
d07137b9 85 this.videoLicences = this.videoService.videoLicences;
6e07c3de 86
e822fdae
C
87 this.uploader = new FileUploader({
88 authToken: this.authService.getRequestHeaderValue(),
89 queueLimit: 1,
98b01bac 90 url: '/api/v1/videos',
e822fdae 91 removeAfterUpload: true
dc8bc31b 92 });
e822fdae
C
93
94 this.uploader.onBuildItemForm = (item, form) => {
4b2f33f3 95 const name = this.form.value['name'];
6e07c3de 96 const category = this.form.value['category'];
d07137b9 97 const licence = this.form.value['licence'];
4b2f33f3
C
98 const description = this.form.value['description'];
99
100 form.append('name', name);
6e07c3de 101 form.append('category', category);
d07137b9 102 form.append('licence', licence);
4b2f33f3 103 form.append('description', description);
e822fdae 104
4b2f33f3
C
105 for (let i = 0; i < this.tags.length; i++) {
106 form.append(`tags[${i}]`, this.tags[i]);
e822fdae
C
107 }
108 };
4b2f33f3
C
109
110 this.buildForm();
e822fdae
C
111 }
112
bf57d5ee
C
113 checkForm() {
114 this.forceCheck();
115
bf57d5ee
C
116 if (this.filename === null) {
117 this.fileError = 'You did not add a file.';
118 }
119
120 return this.form.valid === true && this.tagsError === '' && this.fileError === '';
121 }
122
123 fileChanged() {
124 this.fileError = '';
125 }
126
e822fdae
C
127 onTagKeyPress(event: KeyboardEvent) {
128 // Enter press
129 if (event.keyCode === 13) {
e54163c2 130 this.addTagIfPossible();
e822fdae
C
131 }
132 }
133
134 removeFile() {
135 this.uploader.clearQueue();
136 }
137
138 removeTag(tag: string) {
4b2f33f3 139 this.tags.splice(this.tags.indexOf(tag), 1);
46485303 140 this.form.get('currentTag').enable();
dc8bc31b
C
141 }
142
e822fdae 143 upload() {
e54163c2
C
144 // Maybe the user forgot to press "enter" when he filled the field
145 this.addTagIfPossible();
146
bf57d5ee
C
147 if (this.checkForm() === false) {
148 return;
149 }
150
e822fdae
C
151 const item = this.uploader.queue[0];
152 // TODO: wait for https://github.com/valor-software/ng2-file-upload/pull/242
153 item.alias = 'videofile';
154
1a005042
C
155 // FIXME: remove
156 // Run detection change for progress bar
157 const interval = setInterval(() => { ; }, 250);
158
e822fdae 159 item.onSuccess = () => {
1a005042
C
160 clearInterval(interval);
161
e822fdae 162 console.log('Video uploaded.');
7ddd02c9
C
163 this.notificationsService.success('Success', 'Video uploaded.');
164
e822fdae
C
165
166 // Print all the videos once it's finished
c6de16eb 167 this.router.navigate(['/videos/list']);
e822fdae
C
168 };
169
170 item.onError = (response: string, status: number) => {
1a005042
C
171 clearInterval(interval);
172
bd5c83a8
C
173 // We need to handle manually these cases beceause we use the FileUpload component
174 if (status === 400) {
175 this.error = response;
176 } else if (status === 401) {
177 this.error = 'Access token was expired, refreshing token...';
178 this.authService.refreshAccessToken().subscribe(
179 () => {
180 // Update the uploader request header
181 this.uploader.authToken = this.authService.getRequestHeaderValue();
182 this.error += ' access token refreshed. Please retry your request.';
183 }
184 );
185 } else {
186 this.error = 'Unknow error';
187 console.error(this.error);
188 }
e822fdae
C
189 };
190
e822fdae 191 this.uploader.uploadAll();
dc8bc31b 192 }
e54163c2
C
193
194 private addTagIfPossible() {
195 const currentTag = this.form.value['currentTag'];
196 if (currentTag === undefined) return;
197
198 // Check if the tag is valid and does not already exist
199 if (
200 currentTag.length >= 2 &&
201 this.form.controls['currentTag'].valid &&
202 this.tags.indexOf(currentTag) === -1
203 ) {
204 this.tags.push(currentTag);
205 this.form.patchValue({ currentTag: '' });
206
207 if (this.tags.length >= 3) {
208 this.form.get('currentTag').disable();
209 }
210
211 this.tagsError = '';
212 }
213 }
dc8bc31b 214}