aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-03-16 14:16:23 +0100
committerChocobozzz <me@florianbigard.com>2022-03-16 15:45:56 +0100
commit83664918901564830f3b7d1bd9879411a1b857a8 (patch)
treef633fbe2ec7fb12d872b710c5f8bc45a9c4be1fa /client/src
parentf9915efa5ea0714178fc60d11a0d5434e7b1e600 (diff)
downloadPeerTube-83664918901564830f3b7d1bd9879411a1b857a8.tar.gz
PeerTube-83664918901564830f3b7d1bd9879411a1b857a8.tar.zst
PeerTube-83664918901564830f3b7d1bd9879411a1b857a8.zip
Fix invalid token on upload
Diffstat (limited to 'client/src')
-rw-r--r--client/src/app/+videos/+video-edit/video-add-components/video-upload.component.ts67
1 files changed, 43 insertions, 24 deletions
diff --git a/client/src/app/+videos/+video-edit/video-add-components/video-upload.component.ts b/client/src/app/+videos/+video-edit/video-add-components/video-upload.component.ts
index c4438d777..ec858c8a8 100644
--- a/client/src/app/+videos/+video-edit/video-add-components/video-upload.component.ts
+++ b/client/src/app/+videos/+video-edit/video-add-components/video-upload.component.ts
@@ -50,10 +50,11 @@ export class VideoUploadComponent extends VideoSend implements OnInit, OnDestroy
50 // So that it can be accessed in the template 50 // So that it can be accessed in the template
51 protected readonly BASE_VIDEO_UPLOAD_URL = VideoService.BASE_VIDEO_URL + '/upload-resumable' 51 protected readonly BASE_VIDEO_UPLOAD_URL = VideoService.BASE_VIDEO_URL + '/upload-resumable'
52 52
53 private uploadxOptions: UploadxOptions
54 private isUpdatingVideo = false 53 private isUpdatingVideo = false
55 private fileToUpload: File 54 private fileToUpload: File
56 55
56 private alreadyRefreshedToken = false
57
57 constructor ( 58 constructor (
58 protected formValidatorService: FormValidatorService, 59 protected formValidatorService: FormValidatorService,
59 protected loadingBar: LoadingBarService, 60 protected loadingBar: LoadingBarService,
@@ -68,26 +69,6 @@ export class VideoUploadComponent extends VideoSend implements OnInit, OnDestroy
68 private resumableUploadService: UploadxService 69 private resumableUploadService: UploadxService
69 ) { 70 ) {
70 super() 71 super()
71
72 // FIXME: https://github.com/Chocobozzz/PeerTube/issues/4382#issuecomment-915854167
73 const chunkSize = isIOS()
74 ? 0
75 : undefined // Auto chunk size
76
77 this.uploadxOptions = {
78 endpoint: this.BASE_VIDEO_UPLOAD_URL,
79 multiple: false,
80 token: this.authService.getAccessToken(),
81 uploaderClass: UploaderXFormData,
82 chunkSize,
83 retryConfig: {
84 maxAttempts: 30, // maximum attempts for 503 codes, otherwise set to 6, see below
85 maxDelay: 120_000, // 2 min
86 shouldRetry: (code: number, attempts: number) => {
87 return code === HttpStatusCode.SERVICE_UNAVAILABLE_503 || ((code < 400 || code > 500) && attempts < 6)
88 }
89 }
90 }
91 } 72 }
92 73
93 get videoExtensions () { 74 get videoExtensions () {
@@ -135,6 +116,12 @@ export class VideoUploadComponent extends VideoSend implements OnInit, OnDestroy
135 onUploadVideoOngoing (state: UploadState) { 116 onUploadVideoOngoing (state: UploadState) {
136 switch (state.status) { 117 switch (state.status) {
137 case 'error': { 118 case 'error': {
119 if (!this.alreadyRefreshedToken && state.response.status === HttpStatusCode.UNAUTHORIZED_401) {
120 this.alreadyRefreshedToken = true
121
122 return this.refereshTokenAndRetryUpload()
123 }
124
138 const error = state.response?.error || 'Unknow error' 125 const error = state.response?.error || 'Unknow error'
139 126
140 this.handleUploadError({ 127 this.handleUploadError({
@@ -281,9 +268,9 @@ export class VideoUploadComponent extends VideoSend implements OnInit, OnDestroy
281 } 268 }
282 269
283 this.resumableUploadService.handleFiles(file, { 270 this.resumableUploadService.handleFiles(file, {
284 ...this.uploadxOptions, 271 ...this.getUploadxOptions(),
285 metadata, 272
286 maxChunkSize: this.serverConfig.client.videos.resumableUpload.maxChunkSize 273 metadata
287 }) 274 })
288 275
289 this.isUploadingVideo = true 276 this.isUploadingVideo = true
@@ -378,4 +365,36 @@ export class VideoUploadComponent extends VideoSend implements OnInit, OnDestroy
378 365
379 return name 366 return name
380 } 367 }
368
369 private refereshTokenAndRetryUpload () {
370 this.authService.refreshAccessToken()
371 .subscribe(() => this.retryUpload())
372 }
373
374 private getUploadxOptions (): UploadxOptions {
375 // FIXME: https://github.com/Chocobozzz/PeerTube/issues/4382#issuecomment-915854167
376 const chunkSize = isIOS()
377 ? 0
378 : undefined // Auto chunk size
379
380 return {
381 endpoint: this.BASE_VIDEO_UPLOAD_URL,
382 multiple: false,
383
384 maxChunkSize: this.serverConfig.client.videos.resumableUpload.maxChunkSize,
385 chunkSize,
386
387 token: this.authService.getAccessToken(),
388
389 uploaderClass: UploaderXFormData,
390
391 retryConfig: {
392 maxAttempts: 30, // maximum attempts for 503 codes, otherwise set to 6, see below
393 maxDelay: 120_000, // 2 min
394 shouldRetry: (code: number, attempts: number) => {
395 return code === HttpStatusCode.SERVICE_UNAVAILABLE_503 || ((code < 400 || code > 500) && attempts < 6)
396 }
397 }
398 }
399 }
381} 400}