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 { populateAsyncUserVideoChannels } from '../../shared/misc/utils'
11 import { VideoEdit } from '../../shared/video/video-edit.model'
12 import { VideoService } from '../../shared/video/video.service'
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
36 formErrors: { [ id: string ]: string } = {}
37 validationMessages: ValidatorMessage = {}
39 userVideoChannels = []
41 firstStepPrivacyId = 0
42 firstStepChannelId = 0
45 private formBuilder: FormBuilder,
46 private router: Router,
47 private notificationsService: NotificationsService,
48 private authService: AuthService,
49 private serverService: ServerService,
50 private videoService: VideoService
56 this.form = this.formBuilder.group({})
57 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
63 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
64 .then(() => this.firstStepChannelId = this.userVideoChannels[0].id)
66 this.serverService.videoPrivaciesLoaded
69 this.videoPrivacies = this.serverService.getVideoPrivacies()
72 this.firstStepPrivacyId = VideoPrivacy.PUBLIC
77 this.uploadFirstStep()
83 return this.form.valid
87 const videofile = this.videofileInput.nativeElement.files[0]
88 const name = videofile.name.replace(/\.[^/.]+$/, '')
89 const privacy = this.firstStepPrivacyId.toString()
91 const channelId = this.firstStepChannelId.toString()
93 const formData = new FormData()
94 formData.append('name', name)
95 // Put the video "private" -> we wait he validates the second step
96 formData.append('privacy', VideoPrivacy.PRIVATE.toString())
97 formData.append('nsfw', '' + nsfw)
98 formData.append('channelId', '' + channelId)
99 formData.append('videofile', videofile)
101 this.isUploadingVideo = true
102 this.form.patchValue({
109 this.videoService.uploadVideo(formData).subscribe(
111 if (event.type === HttpEventType.UploadProgress) {
112 this.videoUploadPercents = Math.round(100 * event.loaded / event.total)
113 } else if (event instanceof HttpResponse) {
114 console.log('Video uploaded.')
116 this.videoUploaded = true
118 this.videoUploadedIds = event.body.video
124 this.videoUploadPercents = 0
125 this.error = err.message
130 updateSecondStep () {
131 if (this.checkForm() === false) {
135 const video = new VideoEdit()
136 video.patch(this.form.value)
137 video.channel = this.firstStepChannelId
138 video.id = this.videoUploadedIds.id
139 video.uuid = this.videoUploadedIds.uuid
141 this.videoService.updateVideo(video)
144 this.notificationsService.success('Success', 'Video published.')
145 this.router.navigate([ '/videos/watch', video.uuid ])
149 this.error = 'Cannot update the video.'