]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-edit/video-add-components/video-upload.component.ts
Merge remote-tracking branch 'weblate/develop' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-edit / video-add-components / video-upload.component.ts
CommitLineData
2e257e36 1import { AfterViewInit, Component, ElementRef, EventEmitter, OnDestroy, OnInit, Output, ViewChild } from '@angular/core'
fbad87b0 2import { Router } from '@angular/router'
f6d6e7f8 3import { UploadxOptions, UploadState, UploadxService } from 'ngx-uploadx'
4import { UploaderXFormData } from './uploaderx-form-data'
2e257e36 5import { AuthService, CanComponentDeactivate, HooksService, Notifier, ServerService, UserService } from '@app/core'
f6d6e7f8 6import { scrollToTop, genericUploadErrorHandler } from '@app/helpers'
67ed6552 7import { FormValidatorService } from '@app/shared/shared-forms'
94676e63 8import { BytesPipe, VideoCaptionService, VideoEdit, VideoService } from '@app/shared/shared-main'
fbad87b0 9import { LoadingBarService } from '@ngx-loading-bar/core'
2e257e36 10import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
67ed6552 11import { VideoPrivacy } from '@shared/models'
1942f11d 12import { VideoSend } from './video-send'
f6d6e7f8 13import { HttpErrorResponse, HttpEventType, HttpHeaders } from '@angular/common/http'
fbad87b0
C
14
15@Component({
16 selector: 'my-video-upload',
17 templateUrl: './video-upload.component.html',
18 styleUrls: [
78848714 19 '../shared/video-edit.component.scss',
457bb213
C
20 './video-upload.component.scss',
21 './video-send.scss'
fbad87b0
C
22 ]
23})
f6d6e7f8 24export class VideoUploadComponent extends VideoSend implements OnInit, OnDestroy, AfterViewInit, CanComponentDeactivate {
fbad87b0 25 @Output() firstStepDone = new EventEmitter<string>()
7373507f 26 @Output() firstStepError = new EventEmitter<void>()
2f5d2ec5 27 @ViewChild('videofileInput') videofileInput: ElementRef<HTMLInputElement>
fbad87b0 28
43620009 29 userVideoQuotaUsed = 0
bee0abff 30 userVideoQuotaUsedDaily = 0
43620009 31
7b992a86 32 isUploadingAudioFile = false
fbad87b0 33 isUploadingVideo = false
7b992a86 34
fbad87b0 35 videoUploaded = false
fbad87b0
C
36 videoUploadPercents = 0
37 videoUploadedIds = {
38 id: 0,
39 uuid: ''
40 }
d4132d3f 41 formData: FormData
7b992a86 42
7b992a86 43 previewfileUpload: File
fbad87b0 44
7373507f 45 error: string
d4132d3f 46 enableRetryAfterError: boolean
fbad87b0 47
f6d6e7f8 48 // So that it can be accessed in the template
43620009 49 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
f6d6e7f8 50 protected readonly BASE_VIDEO_UPLOAD_URL = VideoService.BASE_VIDEO_URL + 'upload-resumable'
51
52 private uploadxOptions: UploadxOptions
53 private isUpdatingVideo = false
54 private fileToUpload: File
fbad87b0
C
55
56 constructor (
57 protected formValidatorService: FormValidatorService,
43620009 58 protected loadingBar: LoadingBarService,
f8b2c1b4 59 protected notifier: Notifier,
43620009
C
60 protected authService: AuthService,
61 protected serverService: ServerService,
62 protected videoService: VideoService,
63 protected videoCaptionService: VideoCaptionService,
fbad87b0 64 private userService: UserService,
2e257e36 65 private router: Router,
f6d6e7f8 66 private hooks: HooksService,
67 private resumableUploadService: UploadxService
68 ) {
fbad87b0 69 super()
f6d6e7f8 70
71 this.uploadxOptions = {
72 endpoint: this.BASE_VIDEO_UPLOAD_URL,
73 multiple: false,
74 token: this.authService.getAccessToken(),
75 uploaderClass: UploaderXFormData,
76 retryConfig: {
77 maxAttempts: 6,
78 shouldRetry: (code: number) => {
79 return code < 400 || code >= 501
80 }
81 }
82 }
fbad87b0
C
83 }
84
85 get videoExtensions () {
758f0d19 86 return this.serverConfig.video.file.extensions.join(', ')
fbad87b0
C
87 }
88
f6d6e7f8 89 onUploadVideoOngoing (state: UploadState) {
90 switch (state.status) {
91 case 'error':
92 const error = state.response?.error || 'Unknow error'
93
94 this.handleUploadError({
95 error: new Error(error),
96 name: 'HttpErrorResponse',
97 message: error,
98 ok: false,
99 headers: new HttpHeaders(state.responseHeaders),
100 status: +state.responseStatus,
101 statusText: error,
102 type: HttpEventType.Response,
103 url: state.url
104 })
105 break
106
107 case 'cancelled':
108 this.isUploadingVideo = false
109 this.videoUploadPercents = 0
110
111 this.firstStepError.emit()
112 this.enableRetryAfterError = false
113 this.error = ''
114 break
115
116 case 'queue':
117 this.closeFirstStep(state.name)
118 break
119
120 case 'uploading':
121 this.videoUploadPercents = state.progress
122 break
123
124 case 'paused':
125 this.notifier.info($localize`Upload cancelled`)
126 break
127
128 case 'complete':
129 this.videoUploaded = true
130 this.videoUploadPercents = 100
131
132 this.videoUploadedIds = state?.response.video
133 break
134 }
135 }
136
fbad87b0 137 ngOnInit () {
43620009 138 super.ngOnInit()
fbad87b0
C
139
140 this.userService.getMyVideoQuotaUsed()
348106f2
C
141 .subscribe(data => {
142 this.userVideoQuotaUsed = data.videoQuotaUsed
143 this.userVideoQuotaUsedDaily = data.videoQuotaUsedDaily
144 })
f6d6e7f8 145
146 this.resumableUploadService.events
147 .subscribe(state => this.onUploadVideoOngoing(state))
fbad87b0
C
148 }
149
2e257e36
C
150 ngAfterViewInit () {
151 this.hooks.runAction('action:video-upload.init', 'video-edit')
152 }
153
fbad87b0 154 ngOnDestroy () {
f6d6e7f8 155 this.cancelUpload()
fbad87b0
C
156 }
157
158 canDeactivate () {
159 let text = ''
160
161 if (this.videoUploaded === true) {
5bfc33b6 162 // FIXME: cannot concatenate strings using $localize
66357162
C
163 text = $localize`Your video was uploaded to your account and is private.` + ' ' +
164 $localize`But associated data (tags, description...) will be lost, are you sure you want to leave this page?`
fbad87b0 165 } else {
66357162 166 text = $localize`Your video is not uploaded yet, are you sure you want to leave this page?`
fbad87b0
C
167 }
168
169 return {
170 canDeactivate: !this.isUploadingVideo,
171 text
172 }
173 }
174
f6d6e7f8 175 onFileDropped (files: FileList) {
c9ff8a08 176 this.videofileInput.nativeElement.files = files
7b992a86 177
f6d6e7f8 178 this.onFileChange({ target: this.videofileInput.nativeElement })
7b992a86
C
179 }
180
f6d6e7f8 181 onFileChange (event: Event | { target: HTMLInputElement }) {
182 const file = (event.target as HTMLInputElement).files[0]
e713698f 183
f6d6e7f8 184 if (!file) return
e713698f 185
f6d6e7f8 186 if (!this.checkGlobalUserQuota(file)) return
187 if (!this.checkDailyUserQuota(file)) return
fbad87b0 188
f6d6e7f8 189 if (this.isAudioFile(file.name)) {
7b992a86 190 this.isUploadingAudioFile = true
bee0abff
FA
191 return
192 }
193
fbad87b0 194 this.isUploadingVideo = true
f6d6e7f8 195 this.fileToUpload = file
fbad87b0 196
f6d6e7f8 197 this.uploadFile(file)
d4132d3f
RK
198 }
199
f6d6e7f8 200 uploadAudio () {
201 this.uploadFile(this.getInputVideoFile(), this.previewfileUpload)
202 }
fbad87b0 203
f6d6e7f8 204 retryUpload () {
205 this.enableRetryAfterError = false
206 this.error = ''
207 this.uploadFile(this.fileToUpload)
208 }
f2eb23cd 209
f6d6e7f8 210 cancelUpload () {
211 this.resumableUploadService.control({ action: 'cancel' })
fbad87b0
C
212 }
213
59c9c5d9
C
214 isPublishingButtonDisabled () {
215 return !this.form.valid ||
216 this.isUpdatingVideo === true ||
c7a53f61
C
217 this.videoUploaded !== true ||
218 !this.videoUploadedIds.id
59c9c5d9
C
219 }
220
f6d6e7f8 221 getAudioUploadLabel () {
222 const videofile = this.getInputVideoFile()
223 if (!videofile) return $localize`Upload`
224
225 return $localize`Upload ${videofile.name}`
226 }
227
fbad87b0 228 updateSecondStep () {
c7a53f61 229 if (this.isPublishingButtonDisabled() || !this.checkForm()) {
fbad87b0
C
230 return
231 }
232
233 const video = new VideoEdit()
234 video.patch(this.form.value)
235 video.id = this.videoUploadedIds.id
236 video.uuid = this.videoUploadedIds.uuid
237
238 this.isUpdatingVideo = true
43620009
C
239
240 this.updateVideoAndCaptions(video)
fbad87b0
C
241 .subscribe(
242 () => {
243 this.isUpdatingVideo = false
244 this.isUploadingVideo = false
fbad87b0 245
66357162 246 this.notifier.success($localize`Video published.`)
fbad87b0
C
247 this.router.navigate([ '/videos/watch', video.uuid ])
248 },
249
250 err => {
7373507f
C
251 this.error = err.message
252 scrollToTop()
fbad87b0
C
253 console.error(err)
254 }
255 )
256 }
7b992a86 257
f6d6e7f8 258 private getInputVideoFile () {
259 return this.videofileInput.nativeElement.files[0]
260 }
261
262 private uploadFile (file: File, previewfile?: File) {
263 const metadata = {
264 waitTranscoding: true,
265 commentsEnabled: true,
266 downloadEnabled: true,
267 channelId: this.firstStepChannelId,
268 nsfw: this.serverConfig.instance.isNSFW,
269 privacy: VideoPrivacy.PRIVATE.toString(),
270 filename: file.name,
271 previewfile: previewfile as any
272 }
273
274 this.resumableUploadService.handleFiles(file, {
275 ...this.uploadxOptions,
276 metadata
277 })
278
279 this.isUploadingVideo = true
280 }
281
282 private handleUploadError (err: HttpErrorResponse) {
283 // Reset progress (but keep isUploadingVideo true)
284 this.videoUploadPercents = 0
285 this.enableRetryAfterError = true
286
287 this.error = genericUploadErrorHandler({
288 err,
289 name: $localize`video`,
290 notifier: this.notifier,
291 sticky: false
292 })
293
294 if (err.status === HttpStatusCode.UNSUPPORTED_MEDIA_TYPE_415) {
295 this.cancelUpload()
296 }
297 }
298
299 private closeFirstStep (filename: string) {
300 const nameWithoutExtension = filename.replace(/\.[^/.]+$/, '')
301 const name = nameWithoutExtension.length < 3 ? filename : nameWithoutExtension
302
303 this.form.patchValue({
304 name,
305 privacy: this.firstStepPrivacyId,
306 nsfw: this.serverConfig.instance.isNSFW,
307 channelId: this.firstStepChannelId,
308 previewfile: this.previewfileUpload
309 })
310
311 this.firstStepDone.emit(name)
312 }
313
7b992a86
C
314 private checkGlobalUserQuota (videofile: File) {
315 const bytePipes = new BytesPipe()
316
317 // Check global user quota
318 const videoQuota = this.authService.getUser().videoQuota
319 if (videoQuota !== -1 && (this.userVideoQuotaUsed + videofile.size) > videoQuota) {
66357162
C
320 const videoSizeBytes = bytePipes.transform(videofile.size, 0)
321 const videoQuotaUsedBytes = bytePipes.transform(this.userVideoQuotaUsed, 0)
322 const videoQuotaBytes = bytePipes.transform(videoQuota, 0)
323
f6d6e7f8 324 const msg = $localize`Your video quota is exceeded with this video (video size: ${videoSizeBytes}, used: ${videoQuotaUsedBytes}, quota: ${videoQuotaBytes})`
7b992a86
C
325 this.notifier.error(msg)
326
327 return false
328 }
329
330 return true
331 }
332
333 private checkDailyUserQuota (videofile: File) {
334 const bytePipes = new BytesPipe()
335
336 // Check daily user quota
337 const videoQuotaDaily = this.authService.getUser().videoQuotaDaily
338 if (videoQuotaDaily !== -1 && (this.userVideoQuotaUsedDaily + videofile.size) > videoQuotaDaily) {
66357162
C
339 const videoSizeBytes = bytePipes.transform(videofile.size, 0)
340 const quotaUsedDailyBytes = bytePipes.transform(this.userVideoQuotaUsedDaily, 0)
341 const quotaDailyBytes = bytePipes.transform(videoQuotaDaily, 0)
f6d6e7f8 342 const msg = $localize`Your daily video quota is exceeded with this video (video size: ${videoSizeBytes}, used: ${quotaUsedDailyBytes}, quota: ${quotaDailyBytes})`
7b992a86
C
343 this.notifier.error(msg)
344
345 return false
346 }
347
348 return true
349 }
350
351 private isAudioFile (filename: string) {
99d362de
C
352 const extensions = [ '.mp3', '.flac', '.ogg', '.wma', '.wav' ]
353
354 return extensions.some(e => filename.endsWith(e))
7b992a86 355 }
fbad87b0 356}