1 import { HttpEventType, HttpResponse } from '@angular/common/http'
2 import { Component, OnDestroy, 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 { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
7 import { NotificationsService } from 'angular2-notifications'
8 import { BytesPipe } from 'ngx-pipes'
9 import { Subscription } from 'rxjs/Subscription'
10 import { VideoPrivacy } from '../../../../../shared/models/videos'
11 import { AuthService, ServerService } from '../../core'
12 import { FormReactive } from '../../shared'
13 import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message'
14 import { populateAsyncUserVideoChannels } from '../../shared/misc/utils'
15 import { VideoEdit } from '../../shared/video/video-edit.model'
16 import { VideoService } from '../../shared/video/video.service'
19 selector: 'my-videos-add',
20 templateUrl: './video-add.component.html',
22 './shared/video-edit.component.scss',
23 './video-add.component.scss'
27 export class VideoAddComponent extends FormReactive implements OnInit, OnDestroy, CanComponentDeactivate {
28 @ViewChild('videofileInput') videofileInput
30 isUploadingVideo = false
32 videoUploadObservable: Subscription = null
33 videoUploadPercents = 0
40 formErrors: { [ id: string ]: string } = {}
41 validationMessages: ValidatorMessage = {}
43 userVideoChannels = []
44 userVideoQuotaUsed = 0
46 firstStepPrivacyId = 0
47 firstStepChannelId = 0
50 private formBuilder: FormBuilder,
51 private router: Router,
52 private notificationsService: NotificationsService,
53 private authService: AuthService,
54 private userService: UserService,
55 private serverService: ServerService,
56 private videoService: VideoService
61 get videoExtensions () {
62 return this.serverService.getConfig().video.file.extensions.join(',')
66 this.form = this.formBuilder.group({})
67 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
73 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
74 .then(() => this.firstStepChannelId = this.userVideoChannels[0].id)
76 this.userService.getMyVideoQuotaUsed()
77 .subscribe(data => this.userVideoQuotaUsed = data.videoQuotaUsed)
79 this.serverService.videoPrivaciesLoaded
82 this.videoPrivacies = this.serverService.getVideoPrivacies()
85 this.firstStepPrivacyId = VideoPrivacy.PUBLIC
90 if (this.videoUploadObservable) {
91 this.videoUploadObservable.unsubscribe()
98 if (this.videoUploaded === true) {
99 text = 'Your video was uploaded in your account and is private.' +
100 ' But associated data (tags, description...) will be lost, are you sure you want to leave this page?'
102 text = 'Your video is not uploaded yet, are you sure you want to leave this page?'
106 canDeactivate: !this.isUploadingVideo,
112 this.uploadFirstStep()
118 return this.form.valid
122 if (this.videoUploadObservable !== null) {
123 this.videoUploadObservable.unsubscribe()
124 this.isUploadingVideo = false
125 this.videoUploadPercents = 0
126 this.videoUploadObservable = null
127 this.notificationsService.info('Info', 'Upload cancelled')
132 const videofile = this.videofileInput.nativeElement.files[0]
133 if (!videofile) return
135 const videoQuota = this.authService.getUser().videoQuota
136 if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) {
137 const bytePipes = new BytesPipe()
139 const msg = 'Your video quota is exceeded with this video ' +
140 `(video size: ${bytePipes.transform(videofile.size, 0)}, ` +
141 `used: ${bytePipes.transform(this.userVideoQuotaUsed, 0)}, ` +
142 `quota: ${bytePipes.transform(videoQuota, 0)})`
143 this.notificationsService.error('Error', msg)
147 const name = videofile.name.replace(/\.[^/.]+$/, '')
148 const privacy = this.firstStepPrivacyId.toString()
150 const commentsEnabled = true
151 const channelId = this.firstStepChannelId.toString()
153 const formData = new FormData()
154 formData.append('name', name)
155 // Put the video "private" -> we wait he validates the second step
156 formData.append('privacy', VideoPrivacy.PRIVATE.toString())
157 formData.append('nsfw', '' + nsfw)
158 formData.append('commentsEnabled', '' + commentsEnabled)
159 formData.append('channelId', '' + channelId)
160 formData.append('videofile', videofile)
162 this.isUploadingVideo = true
163 this.form.patchValue({
170 this.videoUploadObservable = this.videoService.uploadVideo(formData).subscribe(
172 if (event.type === HttpEventType.UploadProgress) {
173 this.videoUploadPercents = Math.round(100 * event.loaded / event.total)
174 } else if (event instanceof HttpResponse) {
175 console.log('Video uploaded.')
177 this.videoUploaded = true
179 this.videoUploadedIds = event.body.video
181 this.videoUploadObservable = null
187 this.isUploadingVideo = false
188 this.videoUploadPercents = 0
189 this.videoUploadObservable = null
190 this.notificationsService.error('Error', err.message)
195 updateSecondStep () {
196 if (this.checkForm() === false) {
200 const video = new VideoEdit()
201 video.patch(this.form.value)
202 video.channel = this.firstStepChannelId
203 video.id = this.videoUploadedIds.id
204 video.uuid = this.videoUploadedIds.uuid
206 this.videoService.updateVideo(video)
209 this.notificationsService.success('Success', 'Video published.')
210 this.router.navigate([ '/videos/watch', video.uuid ])
214 this.notificationsService.error('Error', err.message)