修复下载时出现302错误的问题

pull/1397/head
lyswhut 2023-05-18 16:16:58 +08:00
parent 8018d9b418
commit bd5107bbaf
2 changed files with 15 additions and 0 deletions

View File

@ -6,6 +6,7 @@
- 修复列表名翻译显示
- 修复因插入数字类型的ID导致其意外在末尾追加 .0 导致列表数据异常的问题,同时也可能导致同步数据丢失的问题(要完全修复这个问题还需要同时将移动端、同步服务更新到最新版本)
- 修复下载时出现302错误的问题
### 其他

View File

@ -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<Options> = {}) {
@ -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()