]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-edit/video-add.component.ts
Add other video description tests
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-add.component.ts
CommitLineData
bfb3a98f 1import { Component, OnInit, ViewChild } from '@angular/core'
df98563e
C
2import { FormBuilder, FormGroup } from '@angular/forms'
3import { Router } from '@angular/router'
dc8bc31b 4
df98563e 5import { NotificationsService } from 'angular2-notifications'
8140a704 6
6e07c3de
C
7import {
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 18import { AuthService, ServerService } from '../../core'
df98563e 19import { VideoService } from '../shared'
4771e000 20import { VideoCreate } from '../../../../../shared'
bfb3a98f 21import { 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 29export 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 ],
bcd9f81e 85 channelId: [ this.userVideoChannels[0].id, 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
bcd9f81e
C
99 const user = this.authService.getUser()
100 this.userVideoChannels = user.videoChannels.map(v => ({ id: v.id, label: v.name }))
101
df98563e 102 this.buildForm()
e822fdae
C
103 }
104
bfb3a98f
C
105 // The goal is to keep reactive form validation (required field)
106 // https://stackoverflow.com/a/44238894
107 fileChange ($event) {
108 this.form.controls['videofile'].setValue($event.target.files[0].name)
bf57d5ee
C
109 }
110
bfb3a98f
C
111 removeFile () {
112 this.videofileInput.nativeElement.value = ''
113 this.form.controls['videofile'].setValue('')
bf57d5ee
C
114 }
115
bfb3a98f
C
116 checkForm () {
117 this.forceCheck()
118
119 return this.form.valid
e822fdae
C
120 }
121
df98563e 122 upload () {
bf57d5ee 123 if (this.checkForm() === false) {
df98563e 124 return
bf57d5ee
C
125 }
126
bfb3a98f
C
127 const formValue: VideoCreate = this.form.value
128
129 const name = formValue.name
130 const nsfw = formValue.nsfw
131 const category = formValue.category
132 const licence = formValue.licence
133 const language = formValue.language
bcd9f81e 134 const channelId = formValue.channelId
bfb3a98f
C
135 const description = formValue.description
136 const tags = formValue.tags
137 const videofile = this.videofileInput.nativeElement.files[0]
138
139 const formData = new FormData()
140 formData.append('name', name)
141 formData.append('category', '' + category)
142 formData.append('nsfw', '' + nsfw)
143 formData.append('licence', '' + licence)
bcd9f81e 144 formData.append('channelId', '' + channelId)
bfb3a98f
C
145 formData.append('videofile', videofile)
146
147 // Language is optional
148 if (language) {
149 formData.append('language', '' + language)
df98563e 150 }
e822fdae 151
bfb3a98f
C
152 formData.append('description', description)
153
154 for (let i = 0; i < tags.length; i++) {
155 formData.append(`tags[${i}]`, tags[i])
df98563e 156 }
e822fdae 157
bfb3a98f
C
158 this.videoService.uploadVideo(formData).subscribe(
159 event => {
160 if (event.type === HttpEventType.UploadProgress) {
161 this.progressPercent = Math.round(100 * event.loaded / event.total)
162 } else if (event instanceof HttpResponse) {
163 console.log('Video uploaded.')
164 this.notificationsService.success('Success', 'Video uploaded.')
165
166 // Display all the videos once it's finished
315cc0cc 167 this.router.navigate([ '/videos/list' ])
bfb3a98f
C
168 }
169 },
170
315cc0cc
C
171 err => {
172 // Reset progress
173 this.progressPercent = 0
174 this.error = err.message
175 }
bfb3a98f 176 )
dc8bc31b
C
177 }
178}