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'
15 import { CanComponentDeactivate } from '@app/shared/can-deactivate-guard.service'
18 selector: 'my-videos-add',
19 templateUrl: './video-add.component.html',
21 './shared/video-edit.component.scss',
22 './video-add.component.scss'
26 export class VideoAddComponent extends FormReactive implements OnInit, CanComponentDeactivate {
27 @ViewChild('videofileInput') videofileInput
29 isUploadingVideo = false
31 videoUploadObservable = null
32 videoUploadPercents = 0
39 formErrors: { [ id: string ]: string } = {}
40 validationMessages: ValidatorMessage = {}
42 userVideoChannels = []
43 userVideoQuotaUsed = 0
45 firstStepPrivacyId = 0
46 firstStepChannelId = 0
49 private formBuilder: FormBuilder,
50 private router: Router,
51 private notificationsService: NotificationsService,
52 private authService: AuthService,
53 private userService: UserService,
54 private serverService: ServerService,
55 private videoService: VideoService
60 get videoExtensions () {
61 return this.serverService.getConfig().video.file.extensions.join(',')
65 this.form = this.formBuilder.group({})
66 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
72 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
73 .then(() => this.firstStepChannelId = this.userVideoChannels[0].id)
75 this.userService.getMyVideoQuotaUsed()
76 .subscribe(data => this.userVideoQuotaUsed = data.videoQuotaUsed)
78 this.serverService.videoPrivaciesLoaded
81 this.videoPrivacies = this.serverService.getVideoPrivacies()
84 this.firstStepPrivacyId = VideoPrivacy.PUBLIC
89 return !this.isUploadingVideo
93 this.uploadFirstStep()
99 return this.form.valid
103 if (this.videoUploadObservable !== null) {
104 this.videoUploadObservable.unsubscribe()
105 this.isUploadingVideo = false
106 this.videoUploadPercents = 0
107 this.videoUploadObservable = null
108 this.notificationsService.info('Info', 'Upload cancelled')
113 const videofile = this.videofileInput.nativeElement.files[0]
114 if (!videofile) return
116 const videoQuota = this.authService.getUser().videoQuota
117 if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) {
118 const bytePipes = new BytesPipe()
120 const msg = 'Your video quota is exceeded with this video ' +
121 `(video size: ${bytePipes.transform(videofile.size, 0)}, ` +
122 `used: ${bytePipes.transform(this.userVideoQuotaUsed, 0)}, ` +
123 `quota: ${bytePipes.transform(videoQuota, 0)})`
124 this.notificationsService.error('Error', msg)
128 const name = videofile.name.replace(/\.[^/.]+$/, '')
129 const privacy = this.firstStepPrivacyId.toString()
131 const commentsEnabled = true
132 const channelId = this.firstStepChannelId.toString()
134 const formData = new FormData()
135 formData.append('name', name)
136 // Put the video "private" -> we wait he validates the second step
137 formData.append('privacy', VideoPrivacy.PRIVATE.toString())
138 formData.append('nsfw', '' + nsfw)
139 formData.append('commentsEnabled', '' + commentsEnabled)
140 formData.append('channelId', '' + channelId)
141 formData.append('videofile', videofile)
143 this.isUploadingVideo = true
144 this.form.patchValue({
151 this.videoUploadObservable = this.videoService.uploadVideo(formData).subscribe(
153 if (event.type === HttpEventType.UploadProgress) {
154 this.videoUploadPercents = Math.round(100 * event.loaded / event.total)
155 } else if (event instanceof HttpResponse) {
156 console.log('Video uploaded.')
158 this.videoUploaded = true
160 this.videoUploadedIds = event.body.video
162 this.videoUploadObservable = null
168 this.isUploadingVideo = false
169 this.videoUploadPercents = 0
170 this.videoUploadObservable = null
171 this.notificationsService.error('Error', err.message)
176 updateSecondStep () {
177 if (this.checkForm() === false) {
181 const video = new VideoEdit()
182 video.patch(this.form.value)
183 video.channel = this.firstStepChannelId
184 video.id = this.videoUploadedIds.id
185 video.uuid = this.videoUploadedIds.uuid
187 this.videoService.updateVideo(video)
190 this.notificationsService.success('Success', 'Video published.')
191 this.router.navigate([ '/videos/watch', video.uuid ])
195 this.notificationsService.error('Error', err.message)