]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/video-add/video-add.component.ts
Client: Handle NSFW video
[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 ],
92fb909c 74 nsfw: [ false ],
6e07c3de 75 category: [ '', VIDEO_CATEGORY.VALIDATORS ],
d07137b9 76 licence: [ '', VIDEO_LICENCE.VALIDATORS ],
4b2f33f3
C
77 description: [ '', VIDEO_DESCRIPTION.VALIDATORS ],
78 currentTag: [ '', VIDEO_TAGS.VALIDATORS ]
79 });
80
81 this.form.valueChanges.subscribe(data => this.onValueChanged(data));
e822fdae
C
82 }
83
dc8bc31b 84 ngOnInit() {
6e07c3de 85 this.videoCategories = this.videoService.videoCategories;
d07137b9 86 this.videoLicences = this.videoService.videoLicences;
6e07c3de 87
e822fdae
C
88 this.uploader = new FileUploader({
89 authToken: this.authService.getRequestHeaderValue(),
90 queueLimit: 1,
98b01bac 91 url: '/api/v1/videos',
e822fdae 92 removeAfterUpload: true
dc8bc31b 93 });
e822fdae
C
94
95 this.uploader.onBuildItemForm = (item, form) => {
4b2f33f3 96 const name = this.form.value['name'];
92fb909c 97 const nsfw = this.form.value['nsfw'];
6e07c3de 98 const category = this.form.value['category'];
d07137b9 99 const licence = this.form.value['licence'];
4b2f33f3
C
100 const description = this.form.value['description'];
101
102 form.append('name', name);
6e07c3de 103 form.append('category', category);
92fb909c 104 form.append('nsfw', nsfw);
d07137b9 105 form.append('licence', licence);
4b2f33f3 106 form.append('description', description);
e822fdae 107
4b2f33f3
C
108 for (let i = 0; i < this.tags.length; i++) {
109 form.append(`tags[${i}]`, this.tags[i]);
e822fdae
C
110 }
111 };
4b2f33f3
C
112
113 this.buildForm();
e822fdae
C
114 }
115
bf57d5ee
C
116 checkForm() {
117 this.forceCheck();
118
bf57d5ee
C
119 if (this.filename === null) {
120 this.fileError = 'You did not add a file.';
121 }
122
123 return this.form.valid === true && this.tagsError === '' && this.fileError === '';
124 }
125
126 fileChanged() {
127 this.fileError = '';
128 }
129
e822fdae
C
130 onTagKeyPress(event: KeyboardEvent) {
131 // Enter press
132 if (event.keyCode === 13) {
e54163c2 133 this.addTagIfPossible();
e822fdae
C
134 }
135 }
136
137 removeFile() {
138 this.uploader.clearQueue();
139 }
140
141 removeTag(tag: string) {
4b2f33f3 142 this.tags.splice(this.tags.indexOf(tag), 1);
46485303 143 this.form.get('currentTag').enable();
dc8bc31b
C
144 }
145
e822fdae 146 upload() {
e54163c2
C
147 // Maybe the user forgot to press "enter" when he filled the field
148 this.addTagIfPossible();
149
bf57d5ee
C
150 if (this.checkForm() === false) {
151 return;
152 }
153
e822fdae
C
154 const item = this.uploader.queue[0];
155 // TODO: wait for https://github.com/valor-software/ng2-file-upload/pull/242
156 item.alias = 'videofile';
157
1a005042
C
158 // FIXME: remove
159 // Run detection change for progress bar
160 const interval = setInterval(() => { ; }, 250);
161
e822fdae 162 item.onSuccess = () => {
1a005042
C
163 clearInterval(interval);
164
e822fdae 165 console.log('Video uploaded.');
7ddd02c9
C
166 this.notificationsService.success('Success', 'Video uploaded.');
167
e822fdae
C
168
169 // Print all the videos once it's finished
c6de16eb 170 this.router.navigate(['/videos/list']);
e822fdae
C
171 };
172
173 item.onError = (response: string, status: number) => {
1a005042
C
174 clearInterval(interval);
175
bd5c83a8
C
176 // We need to handle manually these cases beceause we use the FileUpload component
177 if (status === 400) {
178 this.error = response;
179 } else if (status === 401) {
180 this.error = 'Access token was expired, refreshing token...';
181 this.authService.refreshAccessToken().subscribe(
182 () => {
183 // Update the uploader request header
184 this.uploader.authToken = this.authService.getRequestHeaderValue();
185 this.error += ' access token refreshed. Please retry your request.';
186 }
187 );
188 } else {
189 this.error = 'Unknow error';
190 console.error(this.error);
191 }
e822fdae
C
192 };
193
e822fdae 194 this.uploader.uploadAll();
dc8bc31b 195 }
e54163c2
C
196
197 private addTagIfPossible() {
198 const currentTag = this.form.value['currentTag'];
199 if (currentTag === undefined) return;
200
201 // Check if the tag is valid and does not already exist
202 if (
203 currentTag.length >= 2 &&
204 this.form.controls['currentTag'].valid &&
205 this.tags.indexOf(currentTag) === -1
206 ) {
207 this.tags.push(currentTag);
208 this.form.patchValue({ currentTag: '' });
209
210 if (this.tags.length >= 3) {
211 this.form.get('currentTag').disable();
212 }
213
214 this.tagsError = '';
215 }
216 }
dc8bc31b 217}