responseReplace.js处理文件下载请求时,下载的文件名尽量保持原文件名,并且默认不允许使用缓存。
parent
7db1b1f2db
commit
00dfa59ca1
|
@ -32,7 +32,7 @@ module.exports = {
|
||||||
// 替换请求头
|
// 替换请求头
|
||||||
if (requestReplaceConfig.headers) {
|
if (requestReplaceConfig.headers) {
|
||||||
replaceRequestHeaders(rOptions, requestReplaceConfig.headers, log)
|
replaceRequestHeaders(rOptions, requestReplaceConfig.headers, log)
|
||||||
actions += 'headers'
|
actions += (actions ? ',' : '') + 'headers'
|
||||||
}
|
}
|
||||||
|
|
||||||
// 替换下载文件请求的请求地址(此功能主要是为了方便拦截配置)
|
// 替换下载文件请求的请求地址(此功能主要是为了方便拦截配置)
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
const lodash = require('lodash')
|
const lodash = require('lodash')
|
||||||
|
const cacheReq = require('../req/cacheReq')
|
||||||
const REMOVE = '[remove]'
|
const REMOVE = '[remove]'
|
||||||
|
|
||||||
// 替换响应头
|
// 替换响应头
|
||||||
|
@ -71,16 +72,31 @@ module.exports = {
|
||||||
|
|
||||||
let actions = ''
|
let actions = ''
|
||||||
|
|
||||||
|
const replaceHeaders = responseReplaceConfig.headers || {}
|
||||||
|
|
||||||
// 处理文件下载请求
|
// 处理文件下载请求
|
||||||
if (responseReplaceConfig.doDownload || rOptions.doDownload) {
|
if (responseReplaceConfig.doDownload || rOptions.doDownload) {
|
||||||
const filename = (rOptions.path.match('^.*/([^/?]+)/?(\\?.*)?$') || [])[1] || 'UNKNOWN_FILENAME'
|
const filename = (rOptions.path.match('^.*/([^/?]+)/?(\\?.*)?$') || [])[1] || 'UNKNOWN_FILENAME'
|
||||||
res.setHeader('Content-Disposition', 'attachment; filename=' + filename)
|
// 设置文件下载响应头
|
||||||
actions += 'download:' + filename
|
replaceHeaders['content-disposition'] = `attachment; filename="${encodeURIComponent(filename)}"`
|
||||||
|
// 设置文件类型
|
||||||
|
if (replaceHeaders['content-type'] == null) {
|
||||||
|
replaceHeaders['content-type'] = 'application/octet-stream'
|
||||||
|
}
|
||||||
|
// 如果未手动配置需要缓存,则不允许使用缓存
|
||||||
|
const maxAge = cacheReq.getMaxAge(interceptOpt)
|
||||||
|
if (maxAge == null || maxAge <= 0) {
|
||||||
|
replaceHeaders['cache-control'] = '[remove]'
|
||||||
|
replaceHeaders['last-modified'] = '[remove]'
|
||||||
|
replaceHeaders.expires = '[remove]'
|
||||||
|
}
|
||||||
|
|
||||||
|
actions += (actions ? ',' : '') + 'download:' + filename
|
||||||
}
|
}
|
||||||
|
|
||||||
// 替换响应头
|
// 替换响应头
|
||||||
if (replaceResponseHeaders(responseReplaceConfig.headers, res, proxyRes)) {
|
if (replaceResponseHeaders(replaceHeaders, res, proxyRes)) {
|
||||||
actions += 'headers'
|
actions += (actions ? ',' : '') + 'headers'
|
||||||
}
|
}
|
||||||
|
|
||||||
if (actions) {
|
if (actions) {
|
||||||
|
|
Loading…
Reference in New Issue