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 videoUploadObservable = null
31 videoUploadPercents = 0
38 formErrors: { [ id: string ]: string } = {}
39 validationMessages: ValidatorMessage = {}
41 userVideoChannels = []
42 userVideoQuotaUsed = 0
44 firstStepPrivacyId = 0
45 firstStepChannelId = 0
48 private formBuilder: FormBuilder,
49 private router: Router,
50 private notificationsService: NotificationsService,
51 private authService: AuthService,
52 private userService: UserService,
53 private serverService: ServerService,
54 private videoService: VideoService
59 get videoExtensions () {
60 return this.serverService.getConfig().video.file.extensions.join(',')
64 this.form = this.formBuilder.group({})
65 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
71 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
72 .then(() => this.firstStepChannelId = this.userVideoChannels[0].id)
74 this.userService.getMyVideoQuotaUsed()
75 .subscribe(data => this.userVideoQuotaUsed = data.videoQuotaUsed)
77 this.serverService.videoPrivaciesLoaded
80 this.videoPrivacies = this.serverService.getVideoPrivacies()
83 this.firstStepPrivacyId = VideoPrivacy.PUBLIC
88 this.uploadFirstStep()
94 return this.form.valid
98 if (this.videoUploadObservable !== null) {
99 this.videoUploadObservable.unsubscribe()
100 this.isUploadingVideo = false
101 this.videoUploadPercents = 0
102 this.videoUploadObservable = null
103 this.notificationsService.info('Info', 'Upload cancelled')
108 const videofile = this.videofileInput.nativeElement.files[0]
109 if (!videofile) return
111 const videoQuota = this.authService.getUser().videoQuota
112 if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) {
113 const bytePipes = new BytesPipe()
115 const msg = 'Your video quota is exceeded with this video ' +
116 `(video size: ${bytePipes.transform(videofile.size, 0)}, ` +
117 `used: ${bytePipes.transform(this.userVideoQuotaUsed, 0)}, ` +
118 `quota: ${bytePipes.transform(videoQuota, 0)})`
119 this.notificationsService.error('Error', msg)
123 const name = videofile.name.replace(/\.[^/.]+$/, '')
124 const privacy = this.firstStepPrivacyId.toString()
126 const commentsEnabled = true
127 const channelId = this.firstStepChannelId.toString()
129 const formData = new FormData()
130 formData.append('name', name)
131 // Put the video "private" -> we wait he validates the second step
132 formData.append('privacy', VideoPrivacy.PRIVATE.toString())
133 formData.append('nsfw', '' + nsfw)
134 formData.append('commentsEnabled', '' + commentsEnabled)
135 formData.append('channelId', '' + channelId)
136 formData.append('videofile', videofile)
138 this.isUploadingVideo = true
139 this.form.patchValue({
146 this.videoUploadObservable = this.videoService.uploadVideo(formData).subscribe(
148 if (event.type === HttpEventType.UploadProgress) {
149 this.videoUploadPercents = Math.round(100 * event.loaded / event.total)
150 } else if (event instanceof HttpResponse) {
151 console.log('Video uploaded.')
153 this.videoUploaded = true
155 this.videoUploadedIds = event.body.video
157 this.videoUploadObservable = null
163 this.isUploadingVideo = false
164 this.videoUploadPercents = 0
165 this.videoUploadObservable = null
166 this.notificationsService.error('Error', err.message)
171 updateSecondStep () {
172 if (this.checkForm() === false) {
176 const video = new VideoEdit()
177 video.patch(this.form.value)
178 video.channel = this.firstStepChannelId
179 video.id = this.videoUploadedIds.id
180 video.uuid = this.videoUploadedIds.uuid
182 this.videoService.updateVideo(video)
185 this.notificationsService.success('Success', 'Video published.')
186 this.router.navigate([ '/videos/watch', video.uuid ])
190 this.notificationsService.error('Error', err.message)