1 import { HttpEventType, HttpResponse } from '@angular/common/http'
2 import { Component, EventEmitter, OnDestroy, OnInit, Output, ViewChild } from '@angular/core'
3 import { Router } from '@angular/router'
4 import { LoadingBarService } from '@ngx-loading-bar/core'
5 import { NotificationsService } from 'angular2-notifications'
6 import { BytesPipe } from 'ngx-pipes'
7 import { Subscription } from 'rxjs'
8 import { VideoPrivacy } from '../../../../../../shared/models/videos'
9 import { AuthService, ServerService } from '../../../core'
10 import { VideoEdit } from '../../../shared/video/video-edit.model'
11 import { VideoService } from '../../../shared/video/video.service'
12 import { I18n } from '@ngx-translate/i18n-polyfill'
13 import { VideoSend } from '@app/videos/+video-edit/video-add-components/video-send'
14 import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
15 import { FormValidatorService, UserService } from '@app/shared'
16 import { VideoCaptionService } from '@app/shared/video-caption'
19 selector: 'my-video-upload',
20 templateUrl: './video-upload.component.html',
22 '../shared/video-edit.component.scss',
23 './video-upload.component.scss'
26 export class VideoUploadComponent extends VideoSend implements OnInit, OnDestroy, CanComponentDeactivate {
27 @Output() firstStepDone = new EventEmitter<string>()
28 @ViewChild('videofileInput') videofileInput
30 // So that it can be accessed in the template
31 readonly SPECIAL_SCHEDULED_PRIVACY = VideoEdit.SPECIAL_SCHEDULED_PRIVACY
33 userVideoQuotaUsed = 0
34 userVideoQuotaUsedDaily = 0
36 isUploadingVideo = false
37 isUpdatingVideo = false
39 videoUploadObservable: Subscription = null
40 videoUploadPercents = 0
46 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
49 protected formValidatorService: FormValidatorService,
50 protected loadingBar: LoadingBarService,
51 protected notificationsService: NotificationsService,
52 protected authService: AuthService,
53 protected serverService: ServerService,
54 protected videoService: VideoService,
55 protected videoCaptionService: VideoCaptionService,
56 private userService: UserService,
57 private router: Router,
63 get videoExtensions () {
64 return this.serverService.getConfig().video.file.extensions.join(',')
70 this.userService.getMyVideoQuotaUsed()
72 this.userVideoQuotaUsed = data.videoQuotaUsed
73 this.userVideoQuotaUsedDaily = data.videoQuotaUsedDaily
78 if (this.videoUploadObservable) this.videoUploadObservable.unsubscribe()
84 if (this.videoUploaded === true) {
85 // FIXME: cannot concatenate strings inside i18n service :/
86 text = this.i18n('Your video was uploaded to your account and is private.') +
87 this.i18n('But associated data (tags, description...) will be lost, are you sure you want to leave this page?')
89 text = this.i18n('Your video is not uploaded yet, are you sure you want to leave this page?')
93 canDeactivate: !this.isUploadingVideo,
99 this.uploadFirstStep()
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(this.i18n('Info'), this.i18n('Upload cancelled'))
113 const videofile = this.videofileInput.nativeElement.files[0] as File
114 if (!videofile) return
116 // Cannot upload videos > 8GB for now
117 if (videofile.size > 8 * 1024 * 1024 * 1024) {
118 this.notificationsService.error(this.i18n('Error'), this.i18n('We are sorry but PeerTube cannot handle videos > 8GB'))
122 const bytePipes = new BytesPipe()
123 const videoQuota = this.authService.getUser().videoQuota
124 if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) {
125 const msg = this.i18n(
126 'Your video quota is exceeded with this video (video size: {{ videoSize }}, used: {{ videoQuotaUsed }}, quota: {{ videoQuota }})',
128 videoSize: bytePipes.transform(videofile.size, 0),
129 videoQuotaUsed: bytePipes.transform(this.userVideoQuotaUsed, 0),
130 videoQuota: bytePipes.transform(videoQuota, 0)
133 this.notificationsService.error(this.i18n('Error'), msg)
137 const videoQuotaDaily = this.authService.getUser().videoQuotaDaily
138 if (videoQuotaDaily !== -1 && (this.userVideoQuotaUsedDaily + videofile.size) > videoQuotaDaily) {
139 const msg = this.i18n(
140 'Your daily video quota is exceeded with this video (video size: {{ videoSize }}, ' +
141 'used: {{ videoQuotaUsedDaily }}, quota: {{ videoQuotaDaily }})',
143 videoSize: bytePipes.transform(videofile.size, 0),
144 videoQuotaUsedDaily: bytePipes.transform(this.userVideoQuotaUsedDaily, 0),
145 videoQuotaDaily: bytePipes.transform(videoQuotaDaily, 0)
148 this.notificationsService.error(this.i18n('Error'), msg)
152 const nameWithoutExtension = videofile.name.replace(/\.[^/.]+$/, '')
155 // If the name of the file is very small, keep the extension
156 if (nameWithoutExtension.length < 3) name = videofile.name
157 else name = nameWithoutExtension
159 const privacy = this.firstStepPrivacyId.toString()
161 const waitTranscoding = true
162 const commentsEnabled = true
163 const channelId = this.firstStepChannelId.toString()
165 const formData = new FormData()
166 formData.append('name', name)
167 // Put the video "private" -> we are waiting the user validation of the second step
168 formData.append('privacy', VideoPrivacy.PRIVATE.toString())
169 formData.append('nsfw', '' + nsfw)
170 formData.append('commentsEnabled', '' + commentsEnabled)
171 formData.append('waitTranscoding', '' + waitTranscoding)
172 formData.append('channelId', '' + channelId)
173 formData.append('videofile', videofile)
175 this.isUploadingVideo = true
176 this.firstStepDone.emit(name)
178 this.form.patchValue({
185 this.videoPrivacies = this.videoService.explainedPrivacyLabels(this.videoPrivacies)
187 this.videoUploadObservable = this.videoService.uploadVideo(formData).subscribe(
189 if (event.type === HttpEventType.UploadProgress) {
190 this.videoUploadPercents = Math.round(100 * event.loaded / event.total)
191 } else if (event instanceof HttpResponse) {
192 this.videoUploaded = true
194 this.videoUploadedIds = event.body.video
196 this.videoUploadObservable = null
202 this.isUploadingVideo = false
203 this.videoUploadPercents = 0
204 this.videoUploadObservable = null
205 this.notificationsService.error(this.i18n('Error'), err.message)
210 updateSecondStep () {
211 if (this.checkForm() === false) {
215 const video = new VideoEdit()
216 video.patch(this.form.value)
217 video.id = this.videoUploadedIds.id
218 video.uuid = this.videoUploadedIds.uuid
220 this.isUpdatingVideo = true
222 this.updateVideoAndCaptions(video)
225 this.isUpdatingVideo = false
226 this.isUploadingVideo = false
228 this.notificationsService.success(this.i18n('Success'), this.i18n('Video published.'))
229 this.router.navigate([ '/videos/watch', video.uuid ])
233 this.isUpdatingVideo = false
234 this.notificationsService.error(this.i18n('Error'), err.message)