fix: 修改 content type 和下载文件添加file name

This commit is contained in:
2024-10-09 23:56:15 +08:00
parent ace4d2d620
commit 79287d7de9
3 changed files with 50 additions and 6 deletions

View File

@@ -163,8 +163,15 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
const [indexFilePath, etag] = indexFile.split('||');
const contentType = getContentType(indexFilePath);
const isHTML = contentType.includes('html');
// 如果 content是 'application/octet-stream' 会下载文件, 添加文件后缀
if (contentType === 'application/octet-stream') {
// 提取文件名,只保留文件名而不是整个路径
const fileName = path.basename(indexFilePath);
res.setHeader('Content-Disposition', `attachment; filename=${fileName}`);
}
// 不存在的文件,返回indexFile的文件
res.writeHead(200, { 'Content-Type': contentType, 'Cache-Control': isHTML ? 'no-cache' : 'public, max-age=3600' });
const filePath = path.join(fileStore, indexFilePath);
const readStream = fs.createReadStream(filePath);
readStream.pipe(res);
@@ -180,6 +187,12 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
const filePath = path.join(fileStore, appFilePath);
let contentType = getContentType(filePath);
const isHTML = contentType.includes('html');
// 如果 content是 'application/octet-stream' 会下载文件, 添加文件后缀
if (contentType === 'application/octet-stream') {
// 提取文件名,只保留文件名而不是整个路径
const fileName = path.basename(appFilePath);
res.setHeader('Content-Disposition', `attachment; filename=${fileName}`);
}
res.writeHead(200, {
'Content-Type': contentType,
'Cache-Control': isHTML ? 'no-cache' : 'public, max-age=3600', // 设置缓存时间为 1 小时