2017-12-01 17:56:26 +00:00
|
|
|
import { HttpEventType, HttpResponse } from '@angular/common/http'
|
2017-09-14 15:06:31 +00:00
|
|
|
import { Component, OnInit, ViewChild } from '@angular/core'
|
2017-06-16 12:32:15 +00:00
|
|
|
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'
|
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'
|
|
|
|
import { ValidatorMessage } from '../../shared/forms/form-validators'
|
|
|
|
import { VideoEdit } from '../../shared/video/video-edit.model'
|
2016-04-14 20:07:46 +00:00
|
|
|
|
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 {
|
2017-09-14 15:06:31 +00:00
|
|
|
@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-04-16 12:06:48 +00:00
|
|
|
|
2017-12-07 15:32:06 +00:00
|
|
|
error: string = null
|
2017-06-16 12:32:15 +00:00
|
|
|
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
|
|
|
|
2017-06-16 12:32:15 +00:00
|
|
|
constructor (
|
2016-09-09 20:16:51 +00:00
|
|
|
private formBuilder: FormBuilder,
|
2017-01-27 15:14:11 +00:00
|
|
|
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,
|
2017-10-09 17:12:40 +00:00
|
|
|
private serverService: ServerService,
|
2017-03-22 20:15:55 +00:00
|
|
|
private videoService: VideoService
|
2016-09-09 20:16:51 +00:00
|
|
|
) {
|
2017-06-16 12:32:15 +00:00
|
|
|
super()
|
2016-09-09 20:16:51 +00:00
|
|
|
}
|
2016-03-14 12:50:19 +00:00
|
|
|
|
2017-06-16 12:32:15 +00:00
|
|
|
buildForm () {
|
2017-12-07 15:32:06 +00:00
|
|
|
this.form = this.formBuilder.group({})
|
2017-06-16 12:32:15 +00:00
|
|
|
this.form.valueChanges.subscribe(data => this.onValueChanged(data))
|
2016-06-07 20:34:02 +00:00
|
|
|
}
|
|
|
|
|
2017-06-16 12:32:15 +00:00
|
|
|
ngOnInit () {
|
|
|
|
this.buildForm()
|
2017-10-30 19:26:06 +00:00
|
|
|
|
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
|
|
|
|
2017-10-30 19:26: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-10-30 19:26:06 +00:00
|
|
|
}
|
|
|
|
)
|
2016-06-07 20:34:02 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 16:22:44 +00:00
|
|
|
fileChange () {
|
|
|
|
this.uploadFirstStep()
|
2016-11-27 17:10:26 +00:00
|
|
|
}
|
|
|
|
|
2017-09-14 15:06:31 +00:00
|
|
|
checkForm () {
|
|
|
|
this.forceCheck()
|
|
|
|
|
|
|
|
return this.form.valid
|
2016-06-07 20:34:02 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 15:32:06 +00:00
|
|
|
uploadFirstStep () {
|
2017-09-14 15:06:31 +00:00
|
|
|
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()
|
2017-09-14 15:06:31 +00:00
|
|
|
|
|
|
|
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())
|
2017-09-14 15:06:31 +00:00
|
|
|
formData.append('nsfw', '' + nsfw)
|
2017-10-25 15:31:11 +00:00
|
|
|
formData.append('channelId', '' + channelId)
|
2017-09-14 15:06:31 +00:00
|
|
|
formData.append('videofile', videofile)
|
|
|
|
|
2017-12-07 16:22:44 +00:00
|
|
|
this.isUploadingVideo = true
|
|
|
|
this.form.patchValue({
|
|
|
|
name,
|
|
|
|
privacy,
|
|
|
|
nsfw,
|
|
|
|
channelId
|
|
|
|
})
|
2016-06-07 20:34:02 +00:00
|
|
|
|
2017-09-14 15:06:31 +00:00
|
|
|
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)
|
2017-09-14 15:06:31 +00:00
|
|
|
} 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
|
2017-09-14 15:06:31 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-09-14 21:03:43 +00:00
|
|
|
err => {
|
|
|
|
// Reset progress
|
2017-12-07 16:56:59 +00:00
|
|
|
this.videoUploadPercents = 0
|
2017-09-14 21:03:43 +00:00
|
|
|
this.error = err.message
|
|
|
|
}
|
2017-09-14 15:06:31 +00:00
|
|
|
)
|
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
|
|
|
}
|