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