]>
Commit | Line | Data |
---|---|---|
bfb3a98f | 1 | import { Component, OnInit, ViewChild } from '@angular/core' |
df98563e C |
2 | import { FormBuilder, FormGroup } from '@angular/forms' |
3 | import { Router } from '@angular/router' | |
dc8bc31b | 4 | |
df98563e | 5 | import { NotificationsService } from 'angular2-notifications' |
8140a704 | 6 | |
6e07c3de C |
7 | import { |
8 | FormReactive, | |
9 | VIDEO_NAME, | |
10 | VIDEO_CATEGORY, | |
d07137b9 | 11 | VIDEO_LICENCE, |
db216afd | 12 | VIDEO_LANGUAGE, |
6e07c3de | 13 | VIDEO_DESCRIPTION, |
db7af09b | 14 | VIDEO_TAGS, |
bcd9f81e | 15 | VIDEO_CHANNEL, |
db7af09b | 16 | VIDEO_FILE |
df98563e | 17 | } from '../../shared' |
bcd9f81e | 18 | import { AuthService, ServerService } from '../../core' |
df98563e | 19 | import { VideoService } from '../shared' |
4771e000 | 20 | import { VideoCreate } from '../../../../../shared' |
bfb3a98f | 21 | import { HttpEventType, HttpResponse } from '@angular/common/http' |
1553e15d | 22 | |
dc8bc31b C |
23 | @Component({ |
24 | selector: 'my-videos-add', | |
d8e689b8 | 25 | styleUrls: [ './video-edit.component.scss' ], |
ec8d8440 | 26 | templateUrl: './video-add.component.html' |
dc8bc31b C |
27 | }) |
28 | ||
4b2f33f3 | 29 | export class VideoAddComponent extends FormReactive implements OnInit { |
bfb3a98f C |
30 | @ViewChild('videofileInput') videofileInput |
31 | ||
32 | progressPercent = 0 | |
df98563e | 33 | tags: string[] = [] |
df98563e C |
34 | videoCategories = [] |
35 | videoLicences = [] | |
36 | videoLanguages = [] | |
bcd9f81e | 37 | userVideoChannels = [] |
4b2f33f3 | 38 | |
df98563e C |
39 | tagValidators = VIDEO_TAGS.VALIDATORS |
40 | tagValidatorsMessages = VIDEO_TAGS.MESSAGES | |
3758da94 | 41 | |
bfb3a98f | 42 | error: string |
df98563e | 43 | form: FormGroup |
4b2f33f3 | 44 | formErrors = { |
e822fdae | 45 | name: '', |
6e07c3de | 46 | category: '', |
d07137b9 | 47 | licence: '', |
db216afd | 48 | language: '', |
bcd9f81e | 49 | channelId: '', |
bfb3a98f C |
50 | description: '', |
51 | videofile: '' | |
df98563e | 52 | } |
4b2f33f3 C |
53 | validationMessages = { |
54 | name: VIDEO_NAME.MESSAGES, | |
6e07c3de | 55 | category: VIDEO_CATEGORY.MESSAGES, |
d07137b9 | 56 | licence: VIDEO_LICENCE.MESSAGES, |
db216afd | 57 | language: VIDEO_LANGUAGE.MESSAGES, |
bcd9f81e | 58 | channelId: VIDEO_CHANNEL.MESSAGES, |
bfb3a98f C |
59 | description: VIDEO_DESCRIPTION.MESSAGES, |
60 | videofile: VIDEO_FILE.MESSAGES | |
df98563e | 61 | } |
dc8bc31b | 62 | |
df98563e | 63 | constructor ( |
4b2f33f3 | 64 | private formBuilder: FormBuilder, |
7ddd02c9 | 65 | private router: Router, |
6e07c3de | 66 | private notificationsService: NotificationsService, |
bcd9f81e | 67 | private authService: AuthService, |
db7af09b | 68 | private serverService: ServerService, |
6e07c3de | 69 | private videoService: VideoService |
4b2f33f3 | 70 | ) { |
df98563e | 71 | super() |
4b2f33f3 | 72 | } |
dc8bc31b | 73 | |
df98563e | 74 | get filename () { |
bfb3a98f | 75 | return this.form.value['videofile'] |
e822fdae C |
76 | } |
77 | ||
df98563e | 78 | buildForm () { |
4b2f33f3 C |
79 | this.form = this.formBuilder.group({ |
80 | name: [ '', VIDEO_NAME.VALIDATORS ], | |
92fb909c | 81 | nsfw: [ false ], |
6e07c3de | 82 | category: [ '', VIDEO_CATEGORY.VALIDATORS ], |
d07137b9 | 83 | licence: [ '', VIDEO_LICENCE.VALIDATORS ], |
db216afd | 84 | language: [ '', VIDEO_LANGUAGE.VALIDATORS ], |
2de96f4d | 85 | channelId: [ '', VIDEO_CHANNEL.VALIDATORS ], |
4b2f33f3 | 86 | description: [ '', VIDEO_DESCRIPTION.VALIDATORS ], |
bfb3a98f C |
87 | videofile: [ '', VIDEO_FILE.VALIDATORS ], |
88 | tags: [ '' ] | |
df98563e | 89 | }) |
4b2f33f3 | 90 | |
df98563e | 91 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) |
e822fdae C |
92 | } |
93 | ||
df98563e | 94 | ngOnInit () { |
db7af09b C |
95 | this.videoCategories = this.serverService.getVideoCategories() |
96 | this.videoLicences = this.serverService.getVideoLicences() | |
97 | this.videoLanguages = this.serverService.getVideoLanguages() | |
6e07c3de | 98 | |
df98563e | 99 | this.buildForm() |
2de96f4d C |
100 | |
101 | this.authService.userInformationLoaded | |
102 | .subscribe( | |
103 | () => { | |
104 | const user = this.authService.getUser() | |
105 | if (!user) return | |
106 | ||
107 | const videoChannels = user.videoChannels | |
108 | if (Array.isArray(videoChannels) === false) return | |
109 | ||
110 | this.userVideoChannels = videoChannels.map(v => ({ id: v.id, label: v.name })) | |
111 | ||
112 | this.form.patchValue({ channelId: this.userVideoChannels[0].id }) | |
113 | } | |
114 | ) | |
e822fdae C |
115 | } |
116 | ||
bfb3a98f C |
117 | // The goal is to keep reactive form validation (required field) |
118 | // https://stackoverflow.com/a/44238894 | |
119 | fileChange ($event) { | |
120 | this.form.controls['videofile'].setValue($event.target.files[0].name) | |
bf57d5ee C |
121 | } |
122 | ||
bfb3a98f C |
123 | removeFile () { |
124 | this.videofileInput.nativeElement.value = '' | |
125 | this.form.controls['videofile'].setValue('') | |
bf57d5ee C |
126 | } |
127 | ||
bfb3a98f C |
128 | checkForm () { |
129 | this.forceCheck() | |
130 | ||
131 | return this.form.valid | |
e822fdae C |
132 | } |
133 | ||
df98563e | 134 | upload () { |
bf57d5ee | 135 | if (this.checkForm() === false) { |
df98563e | 136 | return |
bf57d5ee C |
137 | } |
138 | ||
bfb3a98f C |
139 | const formValue: VideoCreate = this.form.value |
140 | ||
141 | const name = formValue.name | |
142 | const nsfw = formValue.nsfw | |
143 | const category = formValue.category | |
144 | const licence = formValue.licence | |
145 | const language = formValue.language | |
bcd9f81e | 146 | const channelId = formValue.channelId |
bfb3a98f C |
147 | const description = formValue.description |
148 | const tags = formValue.tags | |
149 | const videofile = this.videofileInput.nativeElement.files[0] | |
150 | ||
151 | const formData = new FormData() | |
152 | formData.append('name', name) | |
153 | formData.append('category', '' + category) | |
154 | formData.append('nsfw', '' + nsfw) | |
155 | formData.append('licence', '' + licence) | |
bcd9f81e | 156 | formData.append('channelId', '' + channelId) |
bfb3a98f C |
157 | formData.append('videofile', videofile) |
158 | ||
159 | // Language is optional | |
160 | if (language) { | |
161 | formData.append('language', '' + language) | |
df98563e | 162 | } |
e822fdae | 163 | |
bfb3a98f C |
164 | formData.append('description', description) |
165 | ||
166 | for (let i = 0; i < tags.length; i++) { | |
167 | formData.append(`tags[${i}]`, tags[i]) | |
df98563e | 168 | } |
e822fdae | 169 | |
bfb3a98f C |
170 | this.videoService.uploadVideo(formData).subscribe( |
171 | event => { | |
172 | if (event.type === HttpEventType.UploadProgress) { | |
173 | this.progressPercent = Math.round(100 * event.loaded / event.total) | |
174 | } else if (event instanceof HttpResponse) { | |
175 | console.log('Video uploaded.') | |
176 | this.notificationsService.success('Success', 'Video uploaded.') | |
177 | ||
178 | // Display all the videos once it's finished | |
315cc0cc | 179 | this.router.navigate([ '/videos/list' ]) |
bfb3a98f C |
180 | } |
181 | }, | |
182 | ||
315cc0cc C |
183 | err => { |
184 | // Reset progress | |
185 | this.progressPercent = 0 | |
186 | this.error = err.message | |
187 | } | |
bfb3a98f | 188 | ) |
dc8bc31b C |
189 | } |
190 | } |