1
0
Fork 0
peertube/client/src/app/videos/+video-edit/video-add.component.ts

197 lines
5.4 KiB
TypeScript
Raw Normal View History

2017-12-01 17:56:26 +00:00
import { HttpEventType, HttpResponse } from '@angular/common/http'
import { Component, OnInit, ViewChild } from '@angular/core'
import { FormBuilder, FormGroup } from '@angular/forms'
import { Router } from '@angular/router'
import { NotificationsService } from 'angular2-notifications'
2017-12-01 17:56:26 +00:00
import { VideoService } from 'app/shared/video/video.service'
import { VideoCreate } from '../../../../../shared'
import { AuthService, ServerService } from '../../core'
2017-03-22 20:15:55 +00:00
import {
FormReactive,
VIDEO_CATEGORY,
2017-10-25 15:31:11 +00:00
VIDEO_CHANNEL,
2017-12-01 17:56:26 +00:00
VIDEO_DESCRIPTION,
2017-10-31 10:52:52 +00:00
VIDEO_FILE,
2017-12-01 17:56:26 +00:00
VIDEO_LANGUAGE,
VIDEO_LICENCE,
VIDEO_NAME,
VIDEO_PRIVACY,
VIDEO_TAGS
} from '../../shared'
2016-03-14 12:50:19 +00:00
@Component({
selector: 'my-videos-add',
styleUrls: [ './shared/video-edit.component.scss' ],
templateUrl: './video-add.component.html'
2016-03-14 12:50:19 +00:00
})
2016-09-09 20:16:51 +00:00
export class VideoAddComponent extends FormReactive implements OnInit {
@ViewChild('videofileInput') videofileInput
progressPercent = 0
tags: string[] = []
videoCategories = []
videoLicences = []
videoLanguages = []
2017-10-31 10:52:52 +00:00
videoPrivacies = []
2017-10-25 15:31:11 +00:00
userVideoChannels = []
2016-09-09 20:16:51 +00:00
tagValidators = VIDEO_TAGS.VALIDATORS
tagValidatorsMessages = VIDEO_TAGS.MESSAGES
error: string
form: FormGroup
2016-09-09 20:16:51 +00:00
formErrors = {
name: '',
2017-10-31 10:52:52 +00:00
privacy: '',
2017-03-22 20:15:55 +00:00
category: '',
2017-03-27 19:11:37 +00:00
licence: '',
2017-04-07 12:57:05 +00:00
language: '',
2017-10-25 15:31:11 +00:00
channelId: '',
description: '',
videofile: ''
}
2016-09-09 20:16:51 +00:00
validationMessages = {
name: VIDEO_NAME.MESSAGES,
2017-10-31 10:52:52 +00:00
privacy: VIDEO_PRIVACY.MESSAGES,
2017-03-22 20:15:55 +00:00
category: VIDEO_CATEGORY.MESSAGES,
2017-03-27 19:11:37 +00:00
licence: VIDEO_LICENCE.MESSAGES,
2017-04-07 12:57:05 +00:00
language: VIDEO_LANGUAGE.MESSAGES,
2017-10-25 15:31:11 +00:00
channelId: VIDEO_CHANNEL.MESSAGES,
description: VIDEO_DESCRIPTION.MESSAGES,
videofile: VIDEO_FILE.MESSAGES
}
2016-03-14 12:50:19 +00:00
constructor (
2016-09-09 20:16:51 +00:00
private formBuilder: FormBuilder,
private router: Router,
2017-03-22 20:15:55 +00:00
private notificationsService: NotificationsService,
2017-10-25 15:31:11 +00:00
private authService: AuthService,
private serverService: ServerService,
2017-03-22 20:15:55 +00:00
private videoService: VideoService
2016-09-09 20:16:51 +00:00
) {
super()
2016-09-09 20:16:51 +00:00
}
2016-03-14 12:50:19 +00:00
get filename () {
return this.form.value['videofile']
}
buildForm () {
2016-09-09 20:16:51 +00:00
this.form = this.formBuilder.group({
name: [ '', VIDEO_NAME.VALIDATORS ],
2017-04-04 19:37:03 +00:00
nsfw: [ false ],
2017-10-31 10:52:52 +00:00
privacy: [ '', VIDEO_PRIVACY.VALIDATORS ],
2017-03-22 20:15:55 +00:00
category: [ '', VIDEO_CATEGORY.VALIDATORS ],
2017-03-27 19:11:37 +00:00
licence: [ '', VIDEO_LICENCE.VALIDATORS ],
2017-04-07 12:57:05 +00:00
language: [ '', VIDEO_LANGUAGE.VALIDATORS ],
channelId: [ '', VIDEO_CHANNEL.VALIDATORS ],
2016-09-09 20:16:51 +00:00
description: [ '', VIDEO_DESCRIPTION.VALIDATORS ],
videofile: [ '', VIDEO_FILE.VALIDATORS ],
tags: [ '' ]
})
2016-09-09 20:16:51 +00:00
this.form.valueChanges.subscribe(data => this.onValueChanged(data))
}
ngOnInit () {
this.videoCategories = this.serverService.getVideoCategories()
this.videoLicences = this.serverService.getVideoLicences()
this.videoLanguages = this.serverService.getVideoLanguages()
2017-10-31 10:52:52 +00:00
this.videoPrivacies = this.serverService.getVideoPrivacies()
2017-03-22 20:15:55 +00:00
this.buildForm()
this.authService.userInformationLoaded
.subscribe(
() => {
const user = this.authService.getUser()
if (!user) return
const videoChannels = user.videoChannels
if (Array.isArray(videoChannels) === false) return
this.userVideoChannels = videoChannels.map(v => ({ id: v.id, label: v.name }))
this.form.patchValue({ channelId: this.userVideoChannels[0].id })
}
)
}
// The goal is to keep reactive form validation (required field)
// https://stackoverflow.com/a/44238894
fileChange ($event) {
this.form.controls['videofile'].setValue($event.target.files[0].name)
}
removeFile () {
this.videofileInput.nativeElement.value = ''
this.form.controls['videofile'].setValue('')
}
checkForm () {
this.forceCheck()
return this.form.valid
}
upload () {
if (this.checkForm() === false) {
return
}
const formValue: VideoCreate = this.form.value
const name = formValue.name
2017-10-31 10:52:52 +00:00
const privacy = formValue.privacy
const nsfw = formValue.nsfw
const category = formValue.category
const licence = formValue.licence
const language = formValue.language
2017-10-25 15:31:11 +00:00
const channelId = formValue.channelId
const description = formValue.description
const tags = formValue.tags
const videofile = this.videofileInput.nativeElement.files[0]
const formData = new FormData()
formData.append('name', name)
2017-10-31 10:52:52 +00:00
formData.append('privacy', privacy.toString())
formData.append('category', '' + category)
formData.append('nsfw', '' + nsfw)
formData.append('licence', '' + licence)
2017-10-25 15:31:11 +00:00
formData.append('channelId', '' + channelId)
formData.append('videofile', videofile)
// Language is optional
if (language) {
formData.append('language', '' + language)
}
formData.append('description', description)
for (let i = 0; i < tags.length; i++) {
formData.append(`tags[${i}]`, tags[i])
}
this.videoService.uploadVideo(formData).subscribe(
event => {
if (event.type === HttpEventType.UploadProgress) {
this.progressPercent = Math.round(100 * event.loaded / event.total)
} else if (event instanceof HttpResponse) {
console.log('Video uploaded.')
this.notificationsService.success('Success', 'Video uploaded.')
// Display all the videos once it's finished
2017-12-01 13:46:22 +00:00
this.router.navigate([ '/videos/trending' ])
}
},
err => {
// Reset progress
this.progressPercent = 0
this.error = err.message
}
)
2016-03-14 12:50:19 +00:00
}
}