1 import { HttpEventType, HttpResponse } from '@angular/common/http'
2 import { Component, ElementRef, EventEmitter, OnDestroy, OnInit, Output, ViewChild } from '@angular/core'
3 import { Router } from '@angular/router'
4 import { LoadingBarService } from '@ngx-loading-bar/core'
5 import { BytesPipe } from 'ngx-pipes'
6 import { Subscription } from 'rxjs'
7 import { VideoPrivacy } from '../../../../../../shared/models/videos'
8 import { AuthService, Notifier, ServerService } from '../../../core'
9 import { VideoEdit } from '../../../shared/video/video-edit.model'
10 import { VideoService } from '../../../shared/video/video.service'
11 import { I18n } from '@ngx-translate/i18n-polyfill'
12 import { VideoSend } from '@app/videos/+video-edit/video-add-components/video-send'
13 import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
14 import { FormValidatorService, UserService } from '@app/shared'
15 import { VideoCaptionService } from '@app/shared/video-caption'
16 import { scrollToTop } from '@app/shared/misc/utils'
19 selector: 'my-video-upload',
20 templateUrl: './video-upload.component.html',
22 '../shared/video-edit.component.scss',
23 './video-upload.component.scss',
27 export class VideoUploadComponent extends VideoSend implements OnInit, OnDestroy, CanComponentDeactivate {
28 @Output() firstStepDone = new EventEmitter<string>()
29 @Output() firstStepError = new EventEmitter<void>()
30 @ViewChild('videofileInput') videofileInput: ElementRef<HTMLInputElement>
32 // So that it can be accessed in the template
33 readonly SPECIAL_SCHEDULED_PRIVACY = VideoEdit.SPECIAL_SCHEDULED_PRIVACY
35 userVideoQuotaUsed = 0
36 userVideoQuotaUsedDaily = 0
38 isUploadingAudioFile = false
39 isUploadingVideo = false
40 isUpdatingVideo = false
43 videoUploadObservable: Subscription = null
44 videoUploadPercents = 0
50 waitTranscodingEnabled = true
51 previewfileUpload: File
55 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
58 protected formValidatorService: FormValidatorService,
59 protected loadingBar: LoadingBarService,
60 protected notifier: Notifier,
61 protected authService: AuthService,
62 protected serverService: ServerService,
63 protected videoService: VideoService,
64 protected videoCaptionService: VideoCaptionService,
65 private userService: UserService,
66 private router: Router,
72 get videoExtensions () {
73 return this.serverService.getConfig().video.file.extensions.join(',')
79 this.userService.getMyVideoQuotaUsed()
81 this.userVideoQuotaUsed = data.videoQuotaUsed
82 this.userVideoQuotaUsedDaily = data.videoQuotaUsedDaily
87 if (this.videoUploadObservable) this.videoUploadObservable.unsubscribe()
93 if (this.videoUploaded === true) {
94 // FIXME: cannot concatenate strings inside i18n service :/
95 text = this.i18n('Your video was uploaded to 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 return this.videofileInput.nativeElement.files[0]
111 getAudioUploadLabel () {
112 const videofile = this.getVideoFile()
113 if (!videofile) return this.i18n('Upload')
115 return this.i18n('Upload {{videofileName}}', { videofileName: videofile.name })
119 this.uploadFirstStep()
123 if (this.videoUploadObservable !== null) {
124 this.videoUploadObservable.unsubscribe()
125 this.isUploadingVideo = false
126 this.videoUploadPercents = 0
127 this.videoUploadObservable = null
128 this.notifier.info(this.i18n('Upload cancelled'))
132 uploadFirstStep (clickedOnButton = false) {
133 const videofile = this.getVideoFile()
134 if (!videofile) return
136 if (!this.checkGlobalUserQuota(videofile)) return
137 if (!this.checkDailyUserQuota(videofile)) return
139 if (clickedOnButton === false && this.isAudioFile(videofile.name)) {
140 this.isUploadingAudioFile = true
145 const nameWithoutExtension = videofile.name.replace(/\.[^/.]+$/, '')
148 // If the name of the file is very small, keep the extension
149 if (nameWithoutExtension.length < 3) name = videofile.name
150 else name = nameWithoutExtension
152 // Force user to wait transcoding for unsupported video types in web browsers
153 if (!videofile.name.endsWith('.mp4') && !videofile.name.endsWith('.webm') && !videofile.name.endsWith('.ogv')) {
154 this.waitTranscodingEnabled = false
157 const privacy = this.firstStepPrivacyId.toString()
158 const nsfw = this.serverService.getConfig().instance.isNSFW
159 const waitTranscoding = true
160 const commentsEnabled = true
161 const downloadEnabled = true
162 const channelId = this.firstStepChannelId.toString()
164 const formData = new FormData()
165 formData.append('name', name)
166 // Put the video "private" -> we are waiting the user validation of the second step
167 formData.append('privacy', VideoPrivacy.PRIVATE.toString())
168 formData.append('nsfw', '' + nsfw)
169 formData.append('commentsEnabled', '' + commentsEnabled)
170 formData.append('downloadEnabled', '' + downloadEnabled)
171 formData.append('waitTranscoding', '' + waitTranscoding)
172 formData.append('channelId', '' + channelId)
173 formData.append('videofile', videofile)
175 if (this.previewfileUpload) {
176 formData.append('previewfile', this.previewfileUpload)
177 formData.append('thumbnailfile', this.previewfileUpload)
180 this.isUploadingVideo = true
181 this.firstStepDone.emit(name)
183 this.form.patchValue({
188 previewfile: this.previewfileUpload
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 this.videoUploaded = true
198 this.videoUploadedIds = event.body.video
200 this.videoUploadObservable = null
206 this.isUploadingVideo = false
207 this.videoUploadPercents = 0
208 this.videoUploadObservable = null
209 this.firstStepError.emit()
210 this.notifier.error(err.message)
215 isPublishingButtonDisabled () {
216 return !this.form.valid ||
217 this.isUpdatingVideo === true ||
218 this.videoUploaded !== true
221 updateSecondStep () {
222 if (this.checkForm() === false) {
226 const video = new VideoEdit()
227 video.patch(this.form.value)
228 video.id = this.videoUploadedIds.id
229 video.uuid = this.videoUploadedIds.uuid
231 this.isUpdatingVideo = true
233 this.updateVideoAndCaptions(video)
236 this.isUpdatingVideo = false
237 this.isUploadingVideo = false
239 this.notifier.success(this.i18n('Video published.'))
240 this.router.navigate([ '/videos/watch', video.uuid ])
244 this.error = err.message
251 private checkGlobalUserQuota (videofile: File) {
252 const bytePipes = new BytesPipe()
254 // Check global user quota
255 const videoQuota = this.authService.getUser().videoQuota
256 if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) {
257 const msg = this.i18n(
258 'Your video quota is exceeded with this video (video size: {{videoSize}}, used: {{videoQuotaUsed}}, quota: {{videoQuota}})',
260 videoSize: bytePipes.transform(videofile.size, 0),
261 videoQuotaUsed: bytePipes.transform(this.userVideoQuotaUsed, 0),
262 videoQuota: bytePipes.transform(videoQuota, 0)
265 this.notifier.error(msg)
273 private checkDailyUserQuota (videofile: File) {
274 const bytePipes = new BytesPipe()
276 // Check daily user quota
277 const videoQuotaDaily = this.authService.getUser().videoQuotaDaily
278 if (videoQuotaDaily !== -1 && (this.userVideoQuotaUsedDaily + videofile.size) > videoQuotaDaily) {
279 const msg = this.i18n(
280 'Your daily video quota is exceeded with this video (video size: {{videoSize}}, used: {{quotaUsedDaily}}, quota: {{quotaDaily}})',
282 videoSize: bytePipes.transform(videofile.size, 0),
283 quotaUsedDaily: bytePipes.transform(this.userVideoQuotaUsedDaily, 0),
284 quotaDaily: bytePipes.transform(videoQuotaDaily, 0)
287 this.notifier.error(msg)
295 private isAudioFile (filename: string) {
296 return filename.endsWith('.mp3') || filename.endsWith('.flac') || filename.endsWith('.ogg')