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