1 import { Component, OnInit, ViewChild } from '@angular/core'
2 import { FormBuilder, FormGroup } from '@angular/forms'
3 import { Router } from '@angular/router'
5 import { NotificationsService } from 'angular2-notifications'
19 import { AuthService, ServerService } from '../../core'
20 import { VideoService } from '../shared'
21 import { VideoCreate } from '../../../../../shared'
22 import { HttpEventType, HttpResponse } from '@angular/common/http'
25 selector: 'my-videos-add',
26 styleUrls: [ './video-edit.component.scss' ],
27 templateUrl: './video-add.component.html'
30 export class VideoAddComponent extends FormReactive implements OnInit {
31 @ViewChild('videofileInput') videofileInput
39 userVideoChannels = []
41 tagValidators = VIDEO_TAGS.VALIDATORS
42 tagValidatorsMessages = VIDEO_TAGS.MESSAGES
56 validationMessages = {
57 name: VIDEO_NAME.MESSAGES,
58 privacy: VIDEO_PRIVACY.MESSAGES,
59 category: VIDEO_CATEGORY.MESSAGES,
60 licence: VIDEO_LICENCE.MESSAGES,
61 language: VIDEO_LANGUAGE.MESSAGES,
62 channelId: VIDEO_CHANNEL.MESSAGES,
63 description: VIDEO_DESCRIPTION.MESSAGES,
64 videofile: VIDEO_FILE.MESSAGES
68 private formBuilder: FormBuilder,
69 private router: Router,
70 private notificationsService: NotificationsService,
71 private authService: AuthService,
72 private serverService: ServerService,
73 private videoService: VideoService
79 return this.form.value['videofile']
83 this.form = this.formBuilder.group({
84 name: [ '', VIDEO_NAME.VALIDATORS ],
86 privacy: [ '', VIDEO_PRIVACY.VALIDATORS ],
87 category: [ '', VIDEO_CATEGORY.VALIDATORS ],
88 licence: [ '', VIDEO_LICENCE.VALIDATORS ],
89 language: [ '', VIDEO_LANGUAGE.VALIDATORS ],
90 channelId: [ '', VIDEO_CHANNEL.VALIDATORS ],
91 description: [ '', VIDEO_DESCRIPTION.VALIDATORS ],
92 videofile: [ '', VIDEO_FILE.VALIDATORS ],
96 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
100 this.videoCategories = this.serverService.getVideoCategories()
101 this.videoLicences = this.serverService.getVideoLicences()
102 this.videoLanguages = this.serverService.getVideoLanguages()
103 this.videoPrivacies = this.serverService.getVideoPrivacies()
107 this.authService.userInformationLoaded
110 const user = this.authService.getUser()
113 const videoChannels = user.videoChannels
114 if (Array.isArray(videoChannels) === false) return
116 this.userVideoChannels = videoChannels.map(v => ({ id: v.id, label: v.name }))
118 this.form.patchValue({ channelId: this.userVideoChannels[0].id })
123 // The goal is to keep reactive form validation (required field)
124 // https://stackoverflow.com/a/44238894
125 fileChange ($event) {
126 this.form.controls['videofile'].setValue($event.target.files[0].name)
130 this.videofileInput.nativeElement.value = ''
131 this.form.controls['videofile'].setValue('')
137 return this.form.valid
141 if (this.checkForm() === false) {
145 const formValue: VideoCreate = this.form.value
147 const name = formValue.name
148 const privacy = formValue.privacy
149 const nsfw = formValue.nsfw
150 const category = formValue.category
151 const licence = formValue.licence
152 const language = formValue.language
153 const channelId = formValue.channelId
154 const description = formValue.description
155 const tags = formValue.tags
156 const videofile = this.videofileInput.nativeElement.files[0]
158 const formData = new FormData()
159 formData.append('name', name)
160 formData.append('privacy', privacy.toString())
161 formData.append('category', '' + category)
162 formData.append('nsfw', '' + nsfw)
163 formData.append('licence', '' + licence)
164 formData.append('channelId', '' + channelId)
165 formData.append('videofile', videofile)
167 // Language is optional
169 formData.append('language', '' + language)
172 formData.append('description', description)
174 for (let i = 0; i < tags.length; i++) {
175 formData.append(`tags[${i}]`, tags[i])
178 this.videoService.uploadVideo(formData).subscribe(
180 if (event.type === HttpEventType.UploadProgress) {
181 this.progressPercent = Math.round(100 * event.loaded / event.total)
182 } else if (event instanceof HttpResponse) {
183 console.log('Video uploaded.')
184 this.notificationsService.success('Success', 'Video uploaded.')
186 // Display all the videos once it's finished
187 this.router.navigate([ '/videos/list' ])
193 this.progressPercent = 0
194 this.error = err.message