1 import { HttpEventType, HttpResponse } from '@angular/common/http'
2 import { Component, OnInit, ViewChild } from '@angular/core'
3 import { FormBuilder, FormGroup } from '@angular/forms'
4 import { Router } from '@angular/router'
5 import { NotificationsService } from 'angular2-notifications'
6 import { VideoService } from 'app/shared/video/video.service'
7 import { VideoCreate } from '../../../../../shared'
8 import { VideoPrivacy } from '../../../../../shared/models/videos'
9 import { AuthService, ServerService } from '../../core'
10 import { FormReactive } from '../../shared'
11 import { ValidatorMessage } from '../../shared/forms/form-validators'
12 import { VideoEdit } from '../../shared/video/video-edit.model'
15 selector: 'my-videos-add',
16 templateUrl: './video-add.component.html',
18 './shared/video-edit.component.scss',
19 './video-add.component.scss'
23 export class VideoAddComponent extends FormReactive implements OnInit {
24 @ViewChild('videofileInput') videofileInput
26 isUploadingVideo = false
28 videoUploadPercents = 0
33 formErrors: { [ id: string ]: string } = {}
34 validationMessages: ValidatorMessage = {}
36 userVideoChannels = []
38 firstStepPrivacyId = 0
39 firstStepChannelId = 0
42 private formBuilder: FormBuilder,
43 private router: Router,
44 private notificationsService: NotificationsService,
45 private authService: AuthService,
46 private serverService: ServerService,
47 private videoService: VideoService
53 this.form = this.formBuilder.group({})
54 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
60 this.serverService.videoPrivaciesLoaded
63 this.videoPrivacies = this.serverService.getVideoPrivacies()
66 this.firstStepPrivacyId = VideoPrivacy.PUBLIC
69 this.authService.userInformationLoaded
72 const user = this.authService.getUser()
75 const videoChannels = user.videoChannels
76 if (Array.isArray(videoChannels) === false) return
78 this.userVideoChannels = videoChannels.map(v => ({ id: v.id, label: v.name }))
79 this.firstStepChannelId = this.userVideoChannels[0].id
85 this.uploadFirstStep()
91 return this.form.valid
95 const videofile = this.videofileInput.nativeElement.files[0]
96 const name = videofile.name.replace(/\.[^/.]+$/, '')
97 const privacy = this.firstStepPrivacyId.toString()
99 const channelId = this.firstStepChannelId.toString()
101 const formData = new FormData()
102 formData.append('name', name)
103 // Put the video "private" -> we wait he validates the second step
104 formData.append('privacy', VideoPrivacy.PRIVATE.toString())
105 formData.append('nsfw', '' + nsfw)
106 formData.append('channelId', '' + channelId)
107 formData.append('videofile', videofile)
109 this.isUploadingVideo = true
110 this.form.patchValue({
117 this.videoService.uploadVideo(formData).subscribe(
119 if (event.type === HttpEventType.UploadProgress) {
120 this.videoUploadPercents = Math.round(100 * event.loaded / event.total)
121 } else if (event instanceof HttpResponse) {
122 console.log('Video uploaded.')
124 this.videoUploaded = true
126 this.videoUploadedId = event.body.video.id
132 this.videoUploadPercents = 0
133 this.error = err.message
138 updateSecondStep () {
139 if (this.checkForm() === false) {
143 const video = new VideoEdit()
144 video.patch(this.form.value)
145 video.channel = this.firstStepChannelId
146 video.id = this.videoUploadedId
148 this.videoService.updateVideo(video)
151 this.notificationsService.success('Success', 'Video published.')
152 this.router.navigate([ '/videos/watch', video.id ])
156 this.error = 'Cannot update the video.'