1 import { HttpEventType, HttpResponse } from '@angular/common/http'
2 import { Component, OnInit, ViewChild } from '@angular/core'
3 import { FormBuilder, FormGroup } from '@angular/forms'
4 import { Router } from '@angular/router'
5 import { NotificationsService } from 'angular2-notifications'
6 import { VideoService } from 'app/shared/video/video.service'
7 import { VideoCreate } from '../../../../../shared'
8 import { AuthService, ServerService } from '../../core'
23 selector: 'my-videos-add',
24 styleUrls: [ './shared/video-edit.component.scss' ],
25 templateUrl: './video-add.component.html'
28 export class VideoAddComponent extends FormReactive implements OnInit {
29 @ViewChild('videofileInput') videofileInput
37 userVideoChannels = []
39 tagValidators = VIDEO_TAGS.VALIDATORS
40 tagValidatorsMessages = VIDEO_TAGS.MESSAGES
54 validationMessages = {
55 name: VIDEO_NAME.MESSAGES,
56 privacy: VIDEO_PRIVACY.MESSAGES,
57 category: VIDEO_CATEGORY.MESSAGES,
58 licence: VIDEO_LICENCE.MESSAGES,
59 language: VIDEO_LANGUAGE.MESSAGES,
60 channelId: VIDEO_CHANNEL.MESSAGES,
61 description: VIDEO_DESCRIPTION.MESSAGES,
62 videofile: VIDEO_FILE.MESSAGES
66 private formBuilder: FormBuilder,
67 private router: Router,
68 private notificationsService: NotificationsService,
69 private authService: AuthService,
70 private serverService: ServerService,
71 private videoService: VideoService
77 return this.form.value['videofile']
81 this.form = this.formBuilder.group({
82 name: [ '', VIDEO_NAME.VALIDATORS ],
84 privacy: [ '', VIDEO_PRIVACY.VALIDATORS ],
85 category: [ '', VIDEO_CATEGORY.VALIDATORS ],
86 licence: [ '', VIDEO_LICENCE.VALIDATORS ],
87 language: [ '', VIDEO_LANGUAGE.VALIDATORS ],
88 channelId: [ '', VIDEO_CHANNEL.VALIDATORS ],
89 description: [ '', VIDEO_DESCRIPTION.VALIDATORS ],
90 videofile: [ '', VIDEO_FILE.VALIDATORS ],
94 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
98 this.videoCategories = this.serverService.getVideoCategories()
99 this.videoLicences = this.serverService.getVideoLicences()
100 this.videoLanguages = this.serverService.getVideoLanguages()
101 this.videoPrivacies = this.serverService.getVideoPrivacies()
105 this.authService.userInformationLoaded
108 const user = this.authService.getUser()
111 const videoChannels = user.videoChannels
112 if (Array.isArray(videoChannels) === false) return
114 this.userVideoChannels = videoChannels.map(v => ({ id: v.id, label: v.name }))
116 this.form.patchValue({ channelId: this.userVideoChannels[0].id })
121 // The goal is to keep reactive form validation (required field)
122 // https://stackoverflow.com/a/44238894
123 fileChange ($event) {
124 this.form.controls['videofile'].setValue($event.target.files[0].name)
128 this.videofileInput.nativeElement.value = ''
129 this.form.controls['videofile'].setValue('')
135 return this.form.valid
139 if (this.checkForm() === false) {
143 const formValue: VideoCreate = this.form.value
145 const name = formValue.name
146 const privacy = formValue.privacy
147 const nsfw = formValue.nsfw
148 const category = formValue.category
149 const licence = formValue.licence
150 const language = formValue.language
151 const channelId = formValue.channelId
152 const description = formValue.description
153 const tags = formValue.tags
154 const videofile = this.videofileInput.nativeElement.files[0]
156 const formData = new FormData()
157 formData.append('name', name)
158 formData.append('privacy', privacy.toString())
159 formData.append('category', '' + category)
160 formData.append('nsfw', '' + nsfw)
161 formData.append('licence', '' + licence)
162 formData.append('channelId', '' + channelId)
163 formData.append('videofile', videofile)
165 // Language is optional
167 formData.append('language', '' + language)
170 formData.append('description', description)
172 for (let i = 0; i < tags.length; i++) {
173 formData.append(`tags[${i}]`, tags[i])
176 this.videoService.uploadVideo(formData).subscribe(
178 if (event.type === HttpEventType.UploadProgress) {
179 this.progressPercent = Math.round(100 * event.loaded / event.total)
180 } else if (event instanceof HttpResponse) {
181 console.log('Video uploaded.')
182 this.notificationsService.success('Success', 'Video uploaded.')
184 // Display all the videos once it's finished
185 this.router.navigate([ '/videos/trending' ])
191 this.progressPercent = 0
192 this.error = err.message