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
35 formErrors: { [ id: string ]: string } = {}
36 validationMessages: ValidatorMessage = {}
38 userVideoChannels = []
40 firstStepPrivacyId = 0
41 firstStepChannelId = 0
44 private formBuilder: FormBuilder,
45 private router: Router,
46 private notificationsService: NotificationsService,
47 private authService: AuthService,
48 private serverService: ServerService,
49 private videoService: VideoService
55 this.form = this.formBuilder.group({})
56 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
62 this.serverService.videoPrivaciesLoaded
65 this.videoPrivacies = this.serverService.getVideoPrivacies()
68 this.firstStepPrivacyId = VideoPrivacy.PUBLIC
71 this.authService.userInformationLoaded
74 const user = this.authService.getUser()
77 const videoChannels = user.videoChannels
78 if (Array.isArray(videoChannels) === false) return
80 this.userVideoChannels = videoChannels.map(v => ({ id: v.id, label: v.name }))
81 this.firstStepChannelId = this.userVideoChannels[0].id
87 this.uploadFirstStep()
93 return this.form.valid
97 const videofile = this.videofileInput.nativeElement.files[0]
98 const name = videofile.name.replace(/\.[^/.]+$/, '')
99 const privacy = this.firstStepPrivacyId.toString()
101 const channelId = this.firstStepChannelId.toString()
103 const formData = new FormData()
104 formData.append('name', name)
105 // Put the video "private" -> we wait he validates the second step
106 formData.append('privacy', VideoPrivacy.PRIVATE.toString())
107 formData.append('nsfw', '' + nsfw)
108 formData.append('channelId', '' + channelId)
109 formData.append('videofile', videofile)
111 this.isUploadingVideo = true
112 this.form.patchValue({
119 this.videoService.uploadVideo(formData).subscribe(
121 if (event.type === HttpEventType.UploadProgress) {
122 this.videoUploadPercents = Math.round(100 * event.loaded / event.total)
123 } else if (event instanceof HttpResponse) {
124 console.log('Video uploaded.')
126 this.videoUploaded = true
128 this.videoUploadedIds = event.body.video
134 this.videoUploadPercents = 0
135 this.error = err.message
140 updateSecondStep () {
141 if (this.checkForm() === false) {
145 const video = new VideoEdit()
146 video.patch(this.form.value)
147 video.channel = this.firstStepChannelId
148 video.id = this.videoUploadedIds.id
149 video.uuid = this.videoUploadedIds.uuid
151 this.videoService.updateVideo(video)
154 this.notificationsService.success('Success', 'Video published.')
155 this.router.navigate([ '/videos/watch', video.uuid ])
159 this.error = 'Cannot update the video.'