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

162 lines
4.3 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-08 07:39:15 +00:00
import { VideoPrivacy } from '../../../../../shared/models/videos'
2017-12-01 17:56:26 +00:00
import { AuthService, ServerService } from '../../core'
2017-12-07 15:32:06 +00:00
import { FormReactive } from '../../shared'
2017-12-11 16:36:46 +00:00
import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message'
2017-12-07 15:32:06 +00:00
import { VideoEdit } from '../../shared/video/video-edit.model'
2017-12-11 16:36:46 +00:00
import { VideoService } from '../../shared/video/video.service'
2016-03-14 12:50:19 +00:00
@Component({
selector: 'my-videos-add',
2017-12-07 15:32:06 +00:00
templateUrl: './video-add.component.html',
styleUrls: [
'./shared/video-edit.component.scss',
'./video-add.component.scss'
]
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
2017-12-07 15:32:06 +00:00
isUploadingVideo = false
2017-12-07 16:22:44 +00:00
videoUploaded = false
2017-12-07 16:56:59 +00:00
videoUploadPercents = 0
2017-12-08 07:39:15 +00:00
videoUploadedId = 0
2017-12-07 15:32:06 +00:00
error: string = null
form: FormGroup
2017-12-07 15:32:06 +00:00
formErrors: { [ id: string ]: string } = {}
validationMessages: ValidatorMessage = {}
2017-12-07 16:22:44 +00:00
2017-12-07 15:32:06 +00:00
userVideoChannels = []
videoPrivacies = []
2017-12-08 07:39:15 +00:00
firstStepPrivacyId = 0
firstStepChannelId = 0
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
buildForm () {
2017-12-07 15:32:06 +00:00
this.form = this.formBuilder.group({})
this.form.valueChanges.subscribe(data => this.onValueChanged(data))
}
ngOnInit () {
this.buildForm()
2017-12-07 16:56:59 +00:00
this.serverService.videoPrivaciesLoaded
2017-12-07 16:22:44 +00:00
.subscribe(
() => {
this.videoPrivacies = this.serverService.getVideoPrivacies()
2017-12-08 07:39:15 +00:00
// Public by default
this.firstStepPrivacyId = VideoPrivacy.PUBLIC
2017-12-07 16:22:44 +00:00
})
2017-12-07 15:32:06 +00:00
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 }))
2017-12-08 07:39:15 +00:00
this.firstStepChannelId = this.userVideoChannels[0].id
}
)
}
2017-12-07 16:22:44 +00:00
fileChange () {
this.uploadFirstStep()
}
checkForm () {
this.forceCheck()
return this.form.valid
}
2017-12-07 15:32:06 +00:00
uploadFirstStep () {
const videofile = this.videofileInput.nativeElement.files[0]
2017-12-08 07:39:15 +00:00
const name = videofile.name.replace(/\.[^/.]+$/, '')
const privacy = this.firstStepPrivacyId.toString()
2017-12-07 16:22:44 +00:00
const nsfw = false
2017-12-08 07:39:15 +00:00
const channelId = this.firstStepChannelId.toString()
const formData = new FormData()
formData.append('name', name)
2017-12-08 07:39:15 +00:00
// Put the video "private" -> we wait he validates the second step
formData.append('privacy', VideoPrivacy.PRIVATE.toString())
formData.append('nsfw', '' + nsfw)
2017-10-25 15:31:11 +00:00
formData.append('channelId', '' + channelId)
formData.append('videofile', videofile)
2017-12-07 16:22:44 +00:00
this.isUploadingVideo = true
this.form.patchValue({
name,
privacy,
nsfw,
channelId
})
this.videoService.uploadVideo(formData).subscribe(
event => {
if (event.type === HttpEventType.UploadProgress) {
2017-12-07 16:56:59 +00:00
this.videoUploadPercents = Math.round(100 * event.loaded / event.total)
} else if (event instanceof HttpResponse) {
console.log('Video uploaded.')
2017-12-07 16:22:44 +00:00
this.videoUploaded = true
2017-12-08 07:39:15 +00:00
this.videoUploadedId = event.body.video.id
}
},
err => {
// Reset progress
2017-12-07 16:56:59 +00:00
this.videoUploadPercents = 0
this.error = err.message
}
)
2016-03-14 12:50:19 +00:00
}
2017-12-07 15:32:06 +00:00
updateSecondStep () {
if (this.checkForm() === false) {
return
}
2017-12-08 07:39:15 +00:00
const video = new VideoEdit()
video.patch(this.form.value)
video.channel = this.firstStepChannelId
video.id = this.videoUploadedId
2017-12-07 15:32:06 +00:00
this.videoService.updateVideo(video)
.subscribe(
() => {
this.notificationsService.success('Success', 'Video published.')
2017-12-08 07:39:15 +00:00
this.router.navigate([ '/videos/watch', video.id ])
2017-12-07 15:32:06 +00:00
},
err => {
this.error = 'Cannot update the video.'
console.error(err)
}
)
}
2016-03-14 12:50:19 +00:00
}