]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-edit/video-add.component.ts
Design video update
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-add.component.ts
CommitLineData
202f6b6c 1import { HttpEventType, HttpResponse } from '@angular/common/http'
bfb3a98f 2import { Component, OnInit, ViewChild } from '@angular/core'
df98563e
C
3import { FormBuilder, FormGroup } from '@angular/forms'
4import { Router } from '@angular/router'
df98563e 5import { NotificationsService } from 'angular2-notifications'
202f6b6c
C
6import { VideoService } from 'app/shared/video/video.service'
7import { VideoCreate } from '../../../../../shared'
8import { AuthService, ServerService } from '../../core'
6e07c3de
C
9import {
10 FormReactive,
6e07c3de 11 VIDEO_CATEGORY,
bcd9f81e 12 VIDEO_CHANNEL,
202f6b6c 13 VIDEO_DESCRIPTION,
fd45e8f4 14 VIDEO_FILE,
202f6b6c
C
15 VIDEO_LANGUAGE,
16 VIDEO_LICENCE,
17 VIDEO_NAME,
18 VIDEO_PRIVACY,
19 VIDEO_TAGS
df98563e 20} from '../../shared'
1553e15d 21
dc8bc31b
C
22@Component({
23 selector: 'my-videos-add',
80958c78 24 styleUrls: [ './shared/video-edit.component.scss' ],
ec8d8440 25 templateUrl: './video-add.component.html'
dc8bc31b
C
26})
27
4b2f33f3 28export class VideoAddComponent extends FormReactive implements OnInit {
bfb3a98f
C
29 @ViewChild('videofileInput') videofileInput
30
31 progressPercent = 0
df98563e 32 tags: string[] = []
df98563e
C
33 videoCategories = []
34 videoLicences = []
35 videoLanguages = []
fd45e8f4 36 videoPrivacies = []
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: '',
fd45e8f4 46 privacy: '',
6e07c3de 47 category: '',
d07137b9 48 licence: '',
db216afd 49 language: '',
bcd9f81e 50 channelId: '',
bfb3a98f
C
51 description: '',
52 videofile: ''
df98563e 53 }
4b2f33f3
C
54 validationMessages = {
55 name: VIDEO_NAME.MESSAGES,
fd45e8f4 56 privacy: VIDEO_PRIVACY.MESSAGES,
6e07c3de 57 category: VIDEO_CATEGORY.MESSAGES,
d07137b9 58 licence: VIDEO_LICENCE.MESSAGES,
db216afd 59 language: VIDEO_LANGUAGE.MESSAGES,
bcd9f81e 60 channelId: VIDEO_CHANNEL.MESSAGES,
bfb3a98f
C
61 description: VIDEO_DESCRIPTION.MESSAGES,
62 videofile: VIDEO_FILE.MESSAGES
df98563e 63 }
dc8bc31b 64
df98563e 65 constructor (
4b2f33f3 66 private formBuilder: FormBuilder,
7ddd02c9 67 private router: Router,
6e07c3de 68 private notificationsService: NotificationsService,
bcd9f81e 69 private authService: AuthService,
db7af09b 70 private serverService: ServerService,
6e07c3de 71 private videoService: VideoService
4b2f33f3 72 ) {
df98563e 73 super()
4b2f33f3 74 }
dc8bc31b 75
df98563e 76 get filename () {
bfb3a98f 77 return this.form.value['videofile']
e822fdae
C
78 }
79
df98563e 80 buildForm () {
4b2f33f3
C
81 this.form = this.formBuilder.group({
82 name: [ '', VIDEO_NAME.VALIDATORS ],
92fb909c 83 nsfw: [ false ],
fd45e8f4 84 privacy: [ '', VIDEO_PRIVACY.VALIDATORS ],
6e07c3de 85 category: [ '', VIDEO_CATEGORY.VALIDATORS ],
d07137b9 86 licence: [ '', VIDEO_LICENCE.VALIDATORS ],
db216afd 87 language: [ '', VIDEO_LANGUAGE.VALIDATORS ],
2de96f4d 88 channelId: [ '', VIDEO_CHANNEL.VALIDATORS ],
4b2f33f3 89 description: [ '', VIDEO_DESCRIPTION.VALIDATORS ],
bfb3a98f
C
90 videofile: [ '', VIDEO_FILE.VALIDATORS ],
91 tags: [ '' ]
df98563e 92 })
4b2f33f3 93
df98563e 94 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
e822fdae
C
95 }
96
df98563e 97 ngOnInit () {
db7af09b
C
98 this.videoCategories = this.serverService.getVideoCategories()
99 this.videoLicences = this.serverService.getVideoLicences()
100 this.videoLanguages = this.serverService.getVideoLanguages()
fd45e8f4 101 this.videoPrivacies = this.serverService.getVideoPrivacies()
6e07c3de 102
df98563e 103 this.buildForm()
2de96f4d
C
104
105 this.authService.userInformationLoaded
106 .subscribe(
107 () => {
108 const user = this.authService.getUser()
109 if (!user) return
110
111 const videoChannels = user.videoChannels
112 if (Array.isArray(videoChannels) === false) return
113
114 this.userVideoChannels = videoChannels.map(v => ({ id: v.id, label: v.name }))
115
116 this.form.patchValue({ channelId: this.userVideoChannels[0].id })
117 }
118 )
e822fdae
C
119 }
120
bfb3a98f
C
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)
bf57d5ee
C
125 }
126
bfb3a98f
C
127 removeFile () {
128 this.videofileInput.nativeElement.value = ''
129 this.form.controls['videofile'].setValue('')
bf57d5ee
C
130 }
131
bfb3a98f
C
132 checkForm () {
133 this.forceCheck()
134
135 return this.form.valid
e822fdae
C
136 }
137
df98563e 138 upload () {
bf57d5ee 139 if (this.checkForm() === false) {
df98563e 140 return
bf57d5ee
C
141 }
142
bfb3a98f
C
143 const formValue: VideoCreate = this.form.value
144
145 const name = formValue.name
fd45e8f4 146 const privacy = formValue.privacy
bfb3a98f
C
147 const nsfw = formValue.nsfw
148 const category = formValue.category
149 const licence = formValue.licence
150 const language = formValue.language
bcd9f81e 151 const channelId = formValue.channelId
bfb3a98f
C
152 const description = formValue.description
153 const tags = formValue.tags
154 const videofile = this.videofileInput.nativeElement.files[0]
155
156 const formData = new FormData()
157 formData.append('name', name)
fd45e8f4 158 formData.append('privacy', privacy.toString())
bfb3a98f
C
159 formData.append('category', '' + category)
160 formData.append('nsfw', '' + nsfw)
161 formData.append('licence', '' + licence)
bcd9f81e 162 formData.append('channelId', '' + channelId)
bfb3a98f
C
163 formData.append('videofile', videofile)
164
165 // Language is optional
166 if (language) {
167 formData.append('language', '' + language)
df98563e 168 }
e822fdae 169
bfb3a98f
C
170 formData.append('description', description)
171
172 for (let i = 0; i < tags.length; i++) {
173 formData.append(`tags[${i}]`, tags[i])
df98563e 174 }
e822fdae 175
bfb3a98f
C
176 this.videoService.uploadVideo(formData).subscribe(
177 event => {
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.')
183
184 // Display all the videos once it's finished
9bf9d2a5 185 this.router.navigate([ '/videos/trending' ])
bfb3a98f
C
186 }
187 },
188
315cc0cc
C
189 err => {
190 // Reset progress
191 this.progressPercent = 0
192 this.error = err.message
193 }
bfb3a98f 194 )
dc8bc31b
C
195 }
196}