fix: fix error writehead ,add try catch
This commit is contained in:
@@ -64,6 +64,10 @@ export class UserApp {
|
||||
const value = await redis.get(key);
|
||||
return value;
|
||||
}
|
||||
/**
|
||||
* 获取缓存数据,不存在不会加载
|
||||
* @returns
|
||||
*/
|
||||
async getCache() {
|
||||
const app = this.app;
|
||||
const user = this.user;
|
||||
@@ -127,6 +131,10 @@ export class UserApp {
|
||||
message: msg,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* 获取加载状态
|
||||
* @returns
|
||||
*/
|
||||
async getLoaded() {
|
||||
const app = this.app;
|
||||
const user = this.user;
|
||||
|
||||
@@ -165,72 +165,74 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
|
||||
}
|
||||
}
|
||||
const indexFile = isExist; // 已经必定存在了
|
||||
try {
|
||||
let appFileUrl: string;
|
||||
if (domainApp) {
|
||||
appFileUrl = (url + '').replace(`/`, '');
|
||||
} else {
|
||||
appFileUrl = (url + '').replace(`/${user}/${app}/`, '');
|
||||
}
|
||||
const appFile = await userApp.getFile(appFileUrl);
|
||||
if (!appFile) {
|
||||
const [indexFilePath, etag] = indexFile.split('||');
|
||||
const contentType = getContentType(indexFilePath);
|
||||
const isHTML = contentType.includes('html');
|
||||
const filePath = path.join(fileStore, indexFilePath);
|
||||
if (!userApp.fileCheck(filePath)) {
|
||||
res.writeHead(500, { 'Content-Type': 'text/html; charset=utf-8' });
|
||||
res.write('File expired, Not Found\n');
|
||||
res.end();
|
||||
await userApp.clearCacheData();
|
||||
return;
|
||||
}
|
||||
// 如果 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 readStream = fs.createReadStream(filePath);
|
||||
readStream.pipe(res);
|
||||
|
||||
return;
|
||||
} else {
|
||||
const [appFilePath, eTag] = appFile.split('||');
|
||||
// 检查 If-None-Match 头判断缓存是否有效
|
||||
if (req.headers['if-none-match'] === eTag) {
|
||||
res.statusCode = 304; // 内容未修改
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
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 小时
|
||||
ETag: eTag,
|
||||
});
|
||||
if (!userApp.fileCheck(filePath)) {
|
||||
console.error('File expired', filePath);
|
||||
res.end('File expired\n');
|
||||
await userApp.clearCacheData();
|
||||
return;
|
||||
}
|
||||
const readStream = fs.createReadStream(filePath);
|
||||
readStream.pipe(res);
|
||||
|
||||
let appFileUrl: string;
|
||||
if (domainApp) {
|
||||
appFileUrl = (url + '').replace(`/`, '');
|
||||
} else {
|
||||
appFileUrl = (url + '').replace(`/${user}/${app}/`, '');
|
||||
}
|
||||
const appFile = await userApp.getFile(appFileUrl);
|
||||
if (!appFile) {
|
||||
const [indexFilePath, etag] = indexFile.split('||');
|
||||
const contentType = getContentType(indexFilePath);
|
||||
const isHTML = contentType.includes('html');
|
||||
const filePath = path.join(fileStore, indexFilePath);
|
||||
if (!userApp.fileCheck(filePath)) {
|
||||
res.writeHead(500, { 'Content-Type': 'text/html; charset=utf-8' });
|
||||
res.write('File expired, Not Found\n');
|
||||
res.end();
|
||||
await userApp.clearCacheData();
|
||||
return;
|
||||
}
|
||||
// 如果 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 readStream = fs.createReadStream(filePath);
|
||||
readStream.pipe(res);
|
||||
|
||||
return;
|
||||
} else {
|
||||
const [appFilePath, eTag] = appFile.split('||');
|
||||
// 检查 If-None-Match 头判断缓存是否有效
|
||||
if (req.headers['if-none-match'] === eTag) {
|
||||
res.statusCode = 304; // 内容未修改
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
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 小时
|
||||
ETag: eTag,
|
||||
});
|
||||
if (!userApp.fileCheck(filePath)) {
|
||||
console.error('File expired', filePath);
|
||||
res.writeHead(500, { 'Content-Type': 'text/html; charset=utf-8' });
|
||||
res.write('File expired\n');
|
||||
res.end();
|
||||
await userApp.clearCacheData();
|
||||
return;
|
||||
}
|
||||
const readStream = fs.createReadStream(filePath);
|
||||
readStream.pipe(res);
|
||||
|
||||
return;
|
||||
} catch (error) {
|
||||
console.error('getFile error', error);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user