diff --git a/publish/changeLog.md b/publish/changeLog.md index d545465f..8ef5c105 100644 --- a/publish/changeLog.md +++ b/publish/changeLog.md @@ -6,6 +6,7 @@ - 修复列表名翻译显示 - 修复因插入数字类型的ID导致其意外在末尾追加 .0 导致列表数据异常的问题,同时也可能导致同步数据丢失的问题(要完全修复这个问题还需要同时将移动端、同步服务更新到最新版本) +- 修复下载时出现302错误的问题 ### 其他 diff --git a/src/common/utils/download/Downloader.ts b/src/common/utils/download/Downloader.ts index be6e20d5..3aaecedd 100644 --- a/src/common/utils/download/Downloader.ts +++ b/src/common/utils/download/Downloader.ts @@ -37,6 +37,8 @@ class Task extends EventEmitter { progress = { total: 0, downloaded: 0, speed: 0, progress: 0 } statsEstimate = { time: 0, bytes: 0, prevBytes: 0 } requestInstance: http.ClientRequest | null = null + maxRedirectNum = 2 + private redirectNum = 0 constructor(url: string, savePath: string, filename: string, options: Partial = {}) { @@ -59,6 +61,7 @@ class Task extends EventEmitter { async __init() { const { path, startByte, endByte } = this.chunkInfo + this.redirectNum = 0 this.progress.downloaded = 0 this.progress.progress = 0 this.progress.speed = 0 @@ -112,6 +115,7 @@ class Task extends EventEmitter { __httpFetch(url: string, options: Options['requestOptions']) { // console.log(options) + let redirected = false this.requestInstance = request(url, options) .on('response', response => { if (response.statusCode !== 200 && response.statusCode !== 206) { @@ -125,6 +129,15 @@ class Task extends EventEmitter { }) return } + if ((response.statusCode == 301 || response.statusCode == 302) && response.headers.location && this.redirectNum < this.maxRedirectNum) { + console.log('current url:', url) + console.log('redirect to:', response.headers.location) + redirected = true + this.redirectNum++ + const location = response.headers.location + this.__httpFetch(location, options) + return + } this.status = STATUS.failed this.emit('fail', response) this.__closeRequest() @@ -153,6 +166,7 @@ class Task extends EventEmitter { }) .on('error', err => { this.__handleError(err) }) .on('close', () => { + if (redirected) return void this.__closeWriteStream() }) .end()