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 { UserService } from '@app/shared'
6 import { NotificationsService } from 'angular2-notifications'
7 import { BytesPipe } from 'ngx-pipes'
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/validator-message'
12 import { populateAsyncUserVideoChannels } from '../../shared/misc/utils'
13 import { VideoEdit } from '../../shared/video/video-edit.model'
14 import { VideoService } from '../../shared/video/video.service'
17 selector: 'my-videos-add',
18 templateUrl: './video-add.component.html',
20 './shared/video-edit.component.scss',
21 './video-add.component.scss'
25 export class VideoAddComponent extends FormReactive implements OnInit {
26 @ViewChild('videofileInput') videofileInput
28 isUploadingVideo = false
30 videoUploadPercents = 0
37 formErrors: { [ id: string ]: string } = {}
38 validationMessages: ValidatorMessage = {}
40 userVideoChannels = []
41 userVideoQuotaUsed = 0
43 firstStepPrivacyId = 0
44 firstStepChannelId = 0
47 private formBuilder: FormBuilder,
48 private router: Router,
49 private notificationsService: NotificationsService,
50 private authService: AuthService,
51 private userService: UserService,
52 private serverService: ServerService,
53 private videoService: VideoService
58 get videoExtensions () {
59 return this.serverService.getConfig().video.file.extensions.join(',')
63 this.form = this.formBuilder.group({})
64 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
70 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
71 .then(() => this.firstStepChannelId = this.userVideoChannels[0].id)
73 this.userService.getMyVideoQuotaUsed()
74 .subscribe(data => this.userVideoQuotaUsed = data.videoQuotaUsed)
76 this.serverService.videoPrivaciesLoaded
79 this.videoPrivacies = this.serverService.getVideoPrivacies()
82 this.firstStepPrivacyId = VideoPrivacy.PUBLIC
87 this.uploadFirstStep()
93 return this.form.valid
97 const videofile = this.videofileInput.nativeElement.files[0]
98 const videoQuota = this.authService.getUser().videoQuota
99 if ((this.userVideoQuotaUsed + videofile.size) > videoQuota) {
100 const bytePipes = new BytesPipe()
102 const msg = 'Your video quota is exceeded with this video ' +
103 `(video size: ${bytePipes.transform(videofile.size, 0)}, ` +
104 `used: ${bytePipes.transform(this.userVideoQuotaUsed, 0)}, ` +
105 `quota: ${bytePipes.transform(videoQuota, 0)})`
106 this.notificationsService.error('Error', msg)
110 const name = videofile.name.replace(/\.[^/.]+$/, '')
111 const privacy = this.firstStepPrivacyId.toString()
113 const commentsEnabled = true
114 const channelId = this.firstStepChannelId.toString()
116 const formData = new FormData()
117 formData.append('name', name)
118 // Put the video "private" -> we wait he validates the second step
119 formData.append('privacy', VideoPrivacy.PRIVATE.toString())
120 formData.append('nsfw', '' + nsfw)
121 formData.append('commentsEnabled', '' + commentsEnabled)
122 formData.append('channelId', '' + channelId)
123 formData.append('videofile', videofile)
125 this.isUploadingVideo = true
126 this.form.patchValue({
133 this.videoService.uploadVideo(formData).subscribe(
135 if (event.type === HttpEventType.UploadProgress) {
136 this.videoUploadPercents = Math.round(100 * event.loaded / event.total)
137 } else if (event instanceof HttpResponse) {
138 console.log('Video uploaded.')
140 this.videoUploaded = true
142 this.videoUploadedIds = event.body.video
148 this.isUploadingVideo = false
149 this.videoUploadPercents = 0
150 this.notificationsService.error('Error', err.message)
155 updateSecondStep () {
156 if (this.checkForm() === false) {
160 const video = new VideoEdit()
161 video.patch(this.form.value)
162 video.channel = this.firstStepChannelId
163 video.id = this.videoUploadedIds.id
164 video.uuid = this.videoUploadedIds.uuid
166 this.videoService.updateVideo(video)
169 this.notificationsService.success('Success', 'Video published.')
170 this.router.navigate([ '/videos/watch', video.uuid ])
174 this.notificationsService.error('Error', err.message)