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 { VideoPrivacy } from '../../../../../shared/models/videos'
7 import { AuthService, ServerService } from '../../core'
8 import { FormReactive } from '../../shared'
9 import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message'
10 import { VideoEdit } from '../../shared/video/video-edit.model'
11 import { VideoService } from '../../shared/video/video.service'
14 selector: 'my-videos-add',
15 templateUrl: './video-add.component.html',
17 './shared/video-edit.component.scss',
18 './video-add.component.scss'
22 export class VideoAddComponent extends FormReactive implements OnInit {
23 @ViewChild('videofileInput') videofileInput
25 isUploadingVideo = false
27 videoUploadPercents = 0
32 formErrors: { [ id: string ]: string } = {}
33 validationMessages: ValidatorMessage = {}
35 userVideoChannels = []
37 firstStepPrivacyId = 0
38 firstStepChannelId = 0
41 private formBuilder: FormBuilder,
42 private router: Router,
43 private notificationsService: NotificationsService,
44 private authService: AuthService,
45 private serverService: ServerService,
46 private videoService: VideoService
52 this.form = this.formBuilder.group({})
53 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
59 this.serverService.videoPrivaciesLoaded
62 this.videoPrivacies = this.serverService.getVideoPrivacies()
65 this.firstStepPrivacyId = VideoPrivacy.PUBLIC
68 this.authService.userInformationLoaded
71 const user = this.authService.getUser()
74 const videoChannels = user.videoChannels
75 if (Array.isArray(videoChannels) === false) return
77 this.userVideoChannels = videoChannels.map(v => ({ id: v.id, label: v.name }))
78 this.firstStepChannelId = this.userVideoChannels[0].id
84 this.uploadFirstStep()
90 return this.form.valid
94 const videofile = this.videofileInput.nativeElement.files[0]
95 const name = videofile.name.replace(/\.[^/.]+$/, '')
96 const privacy = this.firstStepPrivacyId.toString()
98 const channelId = this.firstStepChannelId.toString()
100 const formData = new FormData()
101 formData.append('name', name)
102 // Put the video "private" -> we wait he validates the second step
103 formData.append('privacy', VideoPrivacy.PRIVATE.toString())
104 formData.append('nsfw', '' + nsfw)
105 formData.append('channelId', '' + channelId)
106 formData.append('videofile', videofile)
108 this.isUploadingVideo = true
109 this.form.patchValue({
116 this.videoService.uploadVideo(formData).subscribe(
118 if (event.type === HttpEventType.UploadProgress) {
119 this.videoUploadPercents = Math.round(100 * event.loaded / event.total)
120 } else if (event instanceof HttpResponse) {
121 console.log('Video uploaded.')
123 this.videoUploaded = true
125 this.videoUploadedId = event.body.video.id
131 this.videoUploadPercents = 0
132 this.error = err.message
137 updateSecondStep () {
138 if (this.checkForm() === false) {
142 const video = new VideoEdit()
143 video.patch(this.form.value)
144 video.channel = this.firstStepChannelId
145 video.id = this.videoUploadedId
147 this.videoService.updateVideo(video)
150 this.notificationsService.success('Success', 'Video published.')
151 this.router.navigate([ '/videos/watch', video.id ])
155 this.error = 'Cannot update the video.'