1 import { HttpEventType, HttpResponse } from '@angular/common/http'
2 import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
3 import { Router } from '@angular/router'
4 import { UserService } from '@app/shared'
5 import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
6 import { LoadingBarService } from '@ngx-loading-bar/core'
7 import { NotificationsService } from 'angular2-notifications'
8 import { BytesPipe } from 'ngx-pipes'
9 import { Subscription } from 'rxjs'
10 import { VideoPrivacy } from '../../../../../shared/models/videos'
11 import { AuthService, ServerService } from '../../core'
12 import { FormReactive } from '../../shared'
13 import { populateAsyncUserVideoChannels } from '../../shared/misc/utils'
14 import { VideoEdit } from '../../shared/video/video-edit.model'
15 import { VideoService } from '../../shared/video/video.service'
16 import { I18n } from '@ngx-translate/i18n-polyfill'
17 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
20 selector: 'my-videos-add',
21 templateUrl: './video-add.component.html',
23 './shared/video-edit.component.scss',
24 './video-add.component.scss'
27 export class VideoAddComponent extends FormReactive implements OnInit, OnDestroy, CanComponentDeactivate {
28 @ViewChild('videofileInput') videofileInput
30 isUploadingVideo = false
31 isUpdatingVideo = false
33 videoUploadObservable: Subscription = null
34 videoUploadPercents = 0
41 userVideoChannels: { id: number, label: string, support: string }[] = []
42 userVideoQuotaUsed = 0
44 firstStepPrivacyId = 0
45 firstStepChannelId = 0
48 protected formValidatorService: FormValidatorService,
49 private router: Router,
50 private notificationsService: NotificationsService,
51 private authService: AuthService,
52 private userService: UserService,
53 private serverService: ServerService,
54 private videoService: VideoService,
55 private loadingBar: LoadingBarService,
61 get videoExtensions () {
62 return this.serverService.getConfig().video.file.extensions.join(',')
68 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
69 .then(() => this.firstStepChannelId = this.userVideoChannels[0].id)
71 this.userService.getMyVideoQuotaUsed()
72 .subscribe(data => this.userVideoQuotaUsed = data.videoQuotaUsed)
74 this.serverService.videoPrivaciesLoaded
77 this.videoPrivacies = this.serverService.getVideoPrivacies()
80 this.firstStepPrivacyId = VideoPrivacy.PUBLIC
85 if (this.videoUploadObservable) {
86 this.videoUploadObservable.unsubscribe()
93 if (this.videoUploaded === true) {
94 // FIXME: cannot concatenate strings inside i18n service :/
95 text = this.i18n('Your video was uploaded in your account and is private.') +
96 this.i18n('But associated data (tags, description...) will be lost, are you sure you want to leave this page?')
98 text = this.i18n('Your video is not uploaded yet, are you sure you want to leave this page?')
102 canDeactivate: !this.isUploadingVideo,
108 this.uploadFirstStep()
114 return this.form.valid
118 if (this.videoUploadObservable !== null) {
119 this.videoUploadObservable.unsubscribe()
120 this.isUploadingVideo = false
121 this.videoUploadPercents = 0
122 this.videoUploadObservable = null
123 this.notificationsService.info(this.i18n('Info'), this.i18n('Upload cancelled'))
128 const videofile = this.videofileInput.nativeElement.files[0] as File
129 if (!videofile) return
131 // Cannot upload videos > 4GB for now
132 if (videofile.size > 4 * 1024 * 1024 * 1024) {
133 this.notificationsService.error(this.i18n('Error'), this.i18n('We are sorry but PeerTube cannot handle videos > 4GB'))
137 const videoQuota = this.authService.getUser().videoQuota
138 if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) {
139 const bytePipes = new BytesPipe()
141 const msg = this.i18n(
142 'Your video quota is exceeded with this video (video size: {{ videoSize }}, used: {{ videoQuotaUsed }}, quota: {{ videoQuota }})',
144 videoSize: bytePipes.transform(videofile.size, 0),
145 videoQuotaUsed: bytePipes.transform(this.userVideoQuotaUsed, 0),
146 videoQuota: bytePipes.transform(videoQuota, 0)
149 this.notificationsService.error(this.i18n('Error'), msg)
153 this.videoFileName = videofile.name
155 const nameWithoutExtension = videofile.name.replace(/\.[^/.]+$/, '')
158 // If the name of the file is very small, keep the extension
159 if (nameWithoutExtension.length < 3) {
160 name = videofile.name
162 name = nameWithoutExtension
165 const privacy = this.firstStepPrivacyId.toString()
167 const waitTranscoding = true
168 const commentsEnabled = true
169 const channelId = this.firstStepChannelId.toString()
171 const formData = new FormData()
172 formData.append('name', name)
173 // Put the video "private" -> we are waiting the user validation of the second step
174 formData.append('privacy', VideoPrivacy.PRIVATE.toString())
175 formData.append('nsfw', '' + nsfw)
176 formData.append('commentsEnabled', '' + commentsEnabled)
177 formData.append('waitTranscoding', '' + waitTranscoding)
178 formData.append('channelId', '' + channelId)
179 formData.append('videofile', videofile)
181 this.isUploadingVideo = true
182 this.form.patchValue({
189 this.videoUploadObservable = this.videoService.uploadVideo(formData).subscribe(
191 if (event.type === HttpEventType.UploadProgress) {
192 this.videoUploadPercents = Math.round(100 * event.loaded / event.total)
193 } else if (event instanceof HttpResponse) {
194 this.videoUploaded = true
196 this.videoUploadedIds = event.body.video
198 this.videoUploadObservable = null
204 this.isUploadingVideo = false
205 this.videoUploadPercents = 0
206 this.videoUploadObservable = null
207 this.notificationsService.error(this.i18n('Error'), err.message)
212 updateSecondStep () {
213 if (this.checkForm() === false) {
217 const video = new VideoEdit()
218 video.patch(this.form.value)
219 video.id = this.videoUploadedIds.id
220 video.uuid = this.videoUploadedIds.uuid
222 this.isUpdatingVideo = true
223 this.loadingBar.start()
224 this.videoService.updateVideo(video)
227 this.isUpdatingVideo = false
228 this.isUploadingVideo = false
229 this.loadingBar.complete()
231 this.notificationsService.success(this.i18n('Success'), this.i18n('Video published.'))
232 this.router.navigate([ '/videos/watch', video.uuid ])
236 this.isUpdatingVideo = false
237 this.notificationsService.error(this.i18n('Error'), err.message)