]>
Commit | Line | Data |
---|---|---|
230809ef | 1 | import { Component, ElementRef, OnInit } from '@angular/core'; |
4b2f33f3 | 2 | import { FormBuilder, FormGroup } from '@angular/forms'; |
00a44645 | 3 | import { Router } from '@angular/router'; |
dc8bc31b | 4 | |
ab32b0fc | 5 | import { FileUploader } from 'ng2-file-upload/ng2-file-upload'; |
7ddd02c9 | 6 | import { NotificationsService } from 'angular2-notifications'; |
8140a704 | 7 | |
693b1aba | 8 | import { AuthService } from '../../core'; |
6e07c3de C |
9 | import { |
10 | FormReactive, | |
11 | VIDEO_NAME, | |
12 | VIDEO_CATEGORY, | |
d07137b9 | 13 | VIDEO_LICENCE, |
db216afd | 14 | VIDEO_LANGUAGE, |
6e07c3de C |
15 | VIDEO_DESCRIPTION, |
16 | VIDEO_TAGS | |
17 | } from '../../shared'; | |
18 | import { VideoService } from '../shared'; | |
1553e15d | 19 | |
dc8bc31b C |
20 | @Component({ |
21 | selector: 'my-videos-add', | |
d8e689b8 | 22 | styleUrls: [ './video-edit.component.scss' ], |
ec8d8440 | 23 | templateUrl: './video-add.component.html' |
dc8bc31b C |
24 | }) |
25 | ||
4b2f33f3 C |
26 | export class VideoAddComponent extends FormReactive implements OnInit { |
27 | tags: string[] = []; | |
e822fdae | 28 | uploader: FileUploader; |
6e07c3de | 29 | videoCategories = []; |
d07137b9 | 30 | videoLicences = []; |
db216afd | 31 | videoLanguages = []; |
4b2f33f3 | 32 | |
3758da94 C |
33 | tagValidators = VIDEO_TAGS.VALIDATORS; |
34 | tagValidatorsMessages = VIDEO_TAGS.MESSAGES; | |
35 | ||
4b2f33f3 C |
36 | error: string = null; |
37 | form: FormGroup; | |
38 | formErrors = { | |
e822fdae | 39 | name: '', |
6e07c3de | 40 | category: '', |
d07137b9 | 41 | licence: '', |
db216afd | 42 | language: '', |
3758da94 | 43 | description: '' |
4b2f33f3 C |
44 | }; |
45 | validationMessages = { | |
46 | name: VIDEO_NAME.MESSAGES, | |
6e07c3de | 47 | category: VIDEO_CATEGORY.MESSAGES, |
d07137b9 | 48 | licence: VIDEO_LICENCE.MESSAGES, |
db216afd | 49 | language: VIDEO_LANGUAGE.MESSAGES, |
3758da94 | 50 | description: VIDEO_DESCRIPTION.MESSAGES |
e822fdae | 51 | }; |
dc8bc31b | 52 | |
bf57d5ee | 53 | // Special error messages |
bf57d5ee C |
54 | fileError = ''; |
55 | ||
1553e15d | 56 | constructor( |
9bfe96e1 | 57 | private authService: AuthService, |
4fd8aa32 | 58 | private elementRef: ElementRef, |
4b2f33f3 | 59 | private formBuilder: FormBuilder, |
7ddd02c9 | 60 | private router: Router, |
6e07c3de C |
61 | private notificationsService: NotificationsService, |
62 | private videoService: VideoService | |
4b2f33f3 C |
63 | ) { |
64 | super(); | |
65 | } | |
dc8bc31b | 66 | |
e822fdae C |
67 | get filename() { |
68 | if (this.uploader.queue.length === 0) { | |
69 | return null; | |
70 | } | |
71 | ||
72 | return this.uploader.queue[0].file.name; | |
73 | } | |
74 | ||
4b2f33f3 C |
75 | buildForm() { |
76 | this.form = this.formBuilder.group({ | |
77 | name: [ '', VIDEO_NAME.VALIDATORS ], | |
92fb909c | 78 | nsfw: [ false ], |
6e07c3de | 79 | category: [ '', VIDEO_CATEGORY.VALIDATORS ], |
d07137b9 | 80 | licence: [ '', VIDEO_LICENCE.VALIDATORS ], |
db216afd | 81 | language: [ '', VIDEO_LANGUAGE.VALIDATORS ], |
4b2f33f3 | 82 | description: [ '', VIDEO_DESCRIPTION.VALIDATORS ], |
3758da94 | 83 | tags: [ ''] |
4b2f33f3 C |
84 | }); |
85 | ||
86 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)); | |
e822fdae C |
87 | } |
88 | ||
dc8bc31b | 89 | ngOnInit() { |
6e07c3de | 90 | this.videoCategories = this.videoService.videoCategories; |
d07137b9 | 91 | this.videoLicences = this.videoService.videoLicences; |
db216afd | 92 | this.videoLanguages = this.videoService.videoLanguages; |
6e07c3de | 93 | |
e822fdae C |
94 | this.uploader = new FileUploader({ |
95 | authToken: this.authService.getRequestHeaderValue(), | |
96 | queueLimit: 1, | |
98b01bac | 97 | url: '/api/v1/videos', |
e822fdae | 98 | removeAfterUpload: true |
dc8bc31b | 99 | }); |
e822fdae C |
100 | |
101 | this.uploader.onBuildItemForm = (item, form) => { | |
4b2f33f3 | 102 | const name = this.form.value['name']; |
92fb909c | 103 | const nsfw = this.form.value['nsfw']; |
6e07c3de | 104 | const category = this.form.value['category']; |
d07137b9 | 105 | const licence = this.form.value['licence']; |
db216afd | 106 | const language = this.form.value['language']; |
4b2f33f3 | 107 | const description = this.form.value['description']; |
3758da94 | 108 | const tags = this.form.value['tags']; |
4b2f33f3 C |
109 | |
110 | form.append('name', name); | |
6e07c3de | 111 | form.append('category', category); |
92fb909c | 112 | form.append('nsfw', nsfw); |
d07137b9 | 113 | form.append('licence', licence); |
db216afd C |
114 | |
115 | // Language is optional | |
116 | if (language) { | |
117 | form.append('language', language); | |
118 | } | |
119 | ||
4b2f33f3 | 120 | form.append('description', description); |
e822fdae | 121 | |
3758da94 C |
122 | for (let i = 0; i < tags.length; i++) { |
123 | form.append(`tags[${i}]`, tags[i]); | |
e822fdae C |
124 | } |
125 | }; | |
4b2f33f3 C |
126 | |
127 | this.buildForm(); | |
e822fdae C |
128 | } |
129 | ||
bf57d5ee C |
130 | checkForm() { |
131 | this.forceCheck(); | |
132 | ||
bf57d5ee C |
133 | if (this.filename === null) { |
134 | this.fileError = 'You did not add a file.'; | |
135 | } | |
136 | ||
3758da94 | 137 | return this.form.valid === true && this.fileError === ''; |
bf57d5ee C |
138 | } |
139 | ||
140 | fileChanged() { | |
141 | this.fileError = ''; | |
142 | } | |
143 | ||
e822fdae C |
144 | removeFile() { |
145 | this.uploader.clearQueue(); | |
146 | } | |
147 | ||
e822fdae | 148 | upload() { |
bf57d5ee C |
149 | if (this.checkForm() === false) { |
150 | return; | |
151 | } | |
152 | ||
e822fdae C |
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'; | |
156 | ||
1a005042 C |
157 | // FIXME: remove |
158 | // Run detection change for progress bar | |
159 | const interval = setInterval(() => { ; }, 250); | |
160 | ||
e822fdae | 161 | item.onSuccess = () => { |
1a005042 C |
162 | clearInterval(interval); |
163 | ||
e822fdae | 164 | console.log('Video uploaded.'); |
7ddd02c9 C |
165 | this.notificationsService.success('Success', 'Video uploaded.'); |
166 | ||
e822fdae C |
167 | |
168 | // Print all the videos once it's finished | |
c6de16eb | 169 | this.router.navigate(['/videos/list']); |
e822fdae C |
170 | }; |
171 | ||
172 | item.onError = (response: string, status: number) => { | |
1a005042 C |
173 | clearInterval(interval); |
174 | ||
bd5c83a8 C |
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( | |
181 | () => { | |
182 | // Update the uploader request header | |
183 | this.uploader.authToken = this.authService.getRequestHeaderValue(); | |
184 | this.error += ' access token refreshed. Please retry your request.'; | |
185 | } | |
186 | ); | |
187 | } else { | |
188 | this.error = 'Unknow error'; | |
189 | console.error(this.error); | |
190 | } | |
e822fdae C |
191 | }; |
192 | ||
e822fdae | 193 | this.uploader.uploadAll(); |
dc8bc31b C |
194 | } |
195 | } |