Fix invalid token on upload
This commit is contained in:
parent
f9915efa5e
commit
8366491890
|
@ -50,10 +50,11 @@ export class VideoUploadComponent extends VideoSend implements OnInit, OnDestroy
|
||||||
// So that it can be accessed in the template
|
// So that it can be accessed in the template
|
||||||
protected readonly BASE_VIDEO_UPLOAD_URL = VideoService.BASE_VIDEO_URL + '/upload-resumable'
|
protected readonly BASE_VIDEO_UPLOAD_URL = VideoService.BASE_VIDEO_URL + '/upload-resumable'
|
||||||
|
|
||||||
private uploadxOptions: UploadxOptions
|
|
||||||
private isUpdatingVideo = false
|
private isUpdatingVideo = false
|
||||||
private fileToUpload: File
|
private fileToUpload: File
|
||||||
|
|
||||||
|
private alreadyRefreshedToken = false
|
||||||
|
|
||||||
constructor (
|
constructor (
|
||||||
protected formValidatorService: FormValidatorService,
|
protected formValidatorService: FormValidatorService,
|
||||||
protected loadingBar: LoadingBarService,
|
protected loadingBar: LoadingBarService,
|
||||||
|
@ -68,26 +69,6 @@ export class VideoUploadComponent extends VideoSend implements OnInit, OnDestroy
|
||||||
private resumableUploadService: UploadxService
|
private resumableUploadService: UploadxService
|
||||||
) {
|
) {
|
||||||
super()
|
super()
|
||||||
|
|
||||||
// FIXME: https://github.com/Chocobozzz/PeerTube/issues/4382#issuecomment-915854167
|
|
||||||
const chunkSize = isIOS()
|
|
||||||
? 0
|
|
||||||
: undefined // Auto chunk size
|
|
||||||
|
|
||||||
this.uploadxOptions = {
|
|
||||||
endpoint: this.BASE_VIDEO_UPLOAD_URL,
|
|
||||||
multiple: false,
|
|
||||||
token: this.authService.getAccessToken(),
|
|
||||||
uploaderClass: UploaderXFormData,
|
|
||||||
chunkSize,
|
|
||||||
retryConfig: {
|
|
||||||
maxAttempts: 30, // maximum attempts for 503 codes, otherwise set to 6, see below
|
|
||||||
maxDelay: 120_000, // 2 min
|
|
||||||
shouldRetry: (code: number, attempts: number) => {
|
|
||||||
return code === HttpStatusCode.SERVICE_UNAVAILABLE_503 || ((code < 400 || code > 500) && attempts < 6)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get videoExtensions () {
|
get videoExtensions () {
|
||||||
|
@ -135,6 +116,12 @@ export class VideoUploadComponent extends VideoSend implements OnInit, OnDestroy
|
||||||
onUploadVideoOngoing (state: UploadState) {
|
onUploadVideoOngoing (state: UploadState) {
|
||||||
switch (state.status) {
|
switch (state.status) {
|
||||||
case 'error': {
|
case 'error': {
|
||||||
|
if (!this.alreadyRefreshedToken && state.response.status === HttpStatusCode.UNAUTHORIZED_401) {
|
||||||
|
this.alreadyRefreshedToken = true
|
||||||
|
|
||||||
|
return this.refereshTokenAndRetryUpload()
|
||||||
|
}
|
||||||
|
|
||||||
const error = state.response?.error || 'Unknow error'
|
const error = state.response?.error || 'Unknow error'
|
||||||
|
|
||||||
this.handleUploadError({
|
this.handleUploadError({
|
||||||
|
@ -281,9 +268,9 @@ export class VideoUploadComponent extends VideoSend implements OnInit, OnDestroy
|
||||||
}
|
}
|
||||||
|
|
||||||
this.resumableUploadService.handleFiles(file, {
|
this.resumableUploadService.handleFiles(file, {
|
||||||
...this.uploadxOptions,
|
...this.getUploadxOptions(),
|
||||||
metadata,
|
|
||||||
maxChunkSize: this.serverConfig.client.videos.resumableUpload.maxChunkSize
|
metadata
|
||||||
})
|
})
|
||||||
|
|
||||||
this.isUploadingVideo = true
|
this.isUploadingVideo = true
|
||||||
|
@ -378,4 +365,36 @@ export class VideoUploadComponent extends VideoSend implements OnInit, OnDestroy
|
||||||
|
|
||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private refereshTokenAndRetryUpload () {
|
||||||
|
this.authService.refreshAccessToken()
|
||||||
|
.subscribe(() => this.retryUpload())
|
||||||
|
}
|
||||||
|
|
||||||
|
private getUploadxOptions (): UploadxOptions {
|
||||||
|
// FIXME: https://github.com/Chocobozzz/PeerTube/issues/4382#issuecomment-915854167
|
||||||
|
const chunkSize = isIOS()
|
||||||
|
? 0
|
||||||
|
: undefined // Auto chunk size
|
||||||
|
|
||||||
|
return {
|
||||||
|
endpoint: this.BASE_VIDEO_UPLOAD_URL,
|
||||||
|
multiple: false,
|
||||||
|
|
||||||
|
maxChunkSize: this.serverConfig.client.videos.resumableUpload.maxChunkSize,
|
||||||
|
chunkSize,
|
||||||
|
|
||||||
|
token: this.authService.getAccessToken(),
|
||||||
|
|
||||||
|
uploaderClass: UploaderXFormData,
|
||||||
|
|
||||||
|
retryConfig: {
|
||||||
|
maxAttempts: 30, // maximum attempts for 503 codes, otherwise set to 6, see below
|
||||||
|
maxDelay: 120_000, // 2 min
|
||||||
|
shouldRetry: (code: number, attempts: number) => {
|
||||||
|
return code === HttpStatusCode.SERVICE_UNAVAILABLE_503 || ((code < 400 || code > 500) && attempts < 6)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue