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 { LoadingBarService } from '@ngx-loading-bar/core'
8 import { NotificationsService } from 'angular2-notifications'
9 import { BytesPipe } from 'ngx-pipes'
10 import { Subscription } from 'rxjs/Subscription'
11 import { VideoPrivacy } from '../../../../../shared/models/videos'
12 import { AuthService, ServerService } from '../../core'
13 import { FormReactive } from '../../shared'
14 import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message'
15 import { populateAsyncUserVideoChannels } from '../../shared/misc/utils'
16 import { VideoEdit } from '../../shared/video/video-edit.model'
17 import { VideoService } from '../../shared/video/video.service'
20 selector: 'my-videos-add',
21 templateUrl: './video-add.component.html',
23 './shared/video-edit.component.scss',
24 './video-add.component.scss'
28 export class VideoAddComponent extends FormReactive implements OnInit, OnDestroy, CanComponentDeactivate {
29 @ViewChild('videofileInput') videofileInput
31 isUploadingVideo = false
32 isUpdatingVideo = false
34 videoUploadObservable: Subscription = null
35 videoUploadPercents = 0
43 formErrors: { [ id: string ]: string } = {}
44 validationMessages: ValidatorMessage = {}
46 userVideoChannels = []
47 userVideoQuotaUsed = 0
49 firstStepPrivacyId = 0
50 firstStepChannelId = 0
53 private formBuilder: FormBuilder,
54 private router: Router,
55 private notificationsService: NotificationsService,
56 private authService: AuthService,
57 private userService: UserService,
58 private serverService: ServerService,
59 private videoService: VideoService,
60 private loadingBar: LoadingBarService
65 get videoExtensions () {
66 return this.serverService.getConfig().video.file.extensions.join(',')
70 this.form = this.formBuilder.group({})
71 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
77 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
78 .then(() => this.firstStepChannelId = this.userVideoChannels[0].id)
80 this.userService.getMyVideoQuotaUsed()
81 .subscribe(data => this.userVideoQuotaUsed = data.videoQuotaUsed)
83 this.serverService.videoPrivaciesLoaded
86 this.videoPrivacies = this.serverService.getVideoPrivacies()
89 this.firstStepPrivacyId = VideoPrivacy.PUBLIC
94 if (this.videoUploadObservable) {
95 this.videoUploadObservable.unsubscribe()
102 if (this.videoUploaded === true) {
103 text = 'Your video was uploaded in your account and is private.' +
104 ' But associated data (tags, description...) will be lost, are you sure you want to leave this page?'
106 text = 'Your video is not uploaded yet, are you sure you want to leave this page?'
110 canDeactivate: !this.isUploadingVideo,
116 this.uploadFirstStep()
122 return this.form.valid
126 if (this.videoUploadObservable !== null) {
127 this.videoUploadObservable.unsubscribe()
128 this.isUploadingVideo = false
129 this.videoUploadPercents = 0
130 this.videoUploadObservable = null
131 this.notificationsService.info('Info', 'Upload cancelled')
136 const videofile = this.videofileInput.nativeElement.files[0] as File
137 if (!videofile) return
139 // Cannot upload videos > 4GB for now
140 if (videofile.size > 4 * 1024 * 1024 * 1024) {
141 this.notificationsService.error('Error', 'We are sorry but PeerTube cannot handle videos > 4GB')
145 const videoQuota = this.authService.getUser().videoQuota
146 if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) {
147 const bytePipes = new BytesPipe()
149 const msg = 'Your video quota is exceeded with this video ' +
150 `(video size: ${bytePipes.transform(videofile.size, 0)}, ` +
151 `used: ${bytePipes.transform(this.userVideoQuotaUsed, 0)}, ` +
152 `quota: ${bytePipes.transform(videoQuota, 0)})`
153 this.notificationsService.error('Error', msg)
157 this.videoFileName = videofile.name
159 const nameWithoutExtension = videofile.name.replace(/\.[^/.]+$/, '')
162 // If the name of the file is very small, keep the extension
163 if (nameWithoutExtension.length < 3) {
164 name = videofile.name
166 name = nameWithoutExtension
169 const privacy = this.firstStepPrivacyId.toString()
171 const commentsEnabled = true
172 const channelId = this.firstStepChannelId.toString()
174 const formData = new FormData()
175 formData.append('name', name)
176 // Put the video "private" -> we are waiting the user validation of the second step
177 formData.append('privacy', VideoPrivacy.PRIVATE.toString())
178 formData.append('nsfw', '' + nsfw)
179 formData.append('commentsEnabled', '' + commentsEnabled)
180 formData.append('channelId', '' + channelId)
181 formData.append('videofile', videofile)
183 this.isUploadingVideo = true
184 this.form.patchValue({
191 this.videoUploadObservable = this.videoService.uploadVideo(formData).subscribe(
193 if (event.type === HttpEventType.UploadProgress) {
194 this.videoUploadPercents = Math.round(100 * event.loaded / event.total)
195 } else if (event instanceof HttpResponse) {
196 console.log('Video uploaded.')
198 this.videoUploaded = true
200 this.videoUploadedIds = event.body.video
202 this.videoUploadObservable = null
208 this.isUploadingVideo = false
209 this.videoUploadPercents = 0
210 this.videoUploadObservable = null
211 this.notificationsService.error('Error', err.message)
216 updateSecondStep () {
217 if (this.checkForm() === false) {
221 const video = new VideoEdit()
222 video.patch(this.form.value)
223 video.channelId = this.firstStepChannelId
224 video.id = this.videoUploadedIds.id
225 video.uuid = this.videoUploadedIds.uuid
227 this.isUpdatingVideo = true
228 this.loadingBar.start()
229 this.videoService.updateVideo(video)
232 this.isUpdatingVideo = false
233 this.isUploadingVideo = false
234 this.loadingBar.complete()
236 this.notificationsService.success('Success', 'Video published.')
237 this.router.navigate([ '/videos/watch', video.uuid ])
241 this.isUpdatingVideo = false
242 this.notificationsService.error('Error', err.message)