feat: 移除fileProxy函数,更新ProxyInfo类型,修复文件代理逻辑
This commit is contained in:
@@ -7,51 +7,6 @@ import { checkFileExists } from '../file/index.ts';
|
||||
import { log } from '@/module/logger.ts';
|
||||
import { pipeFileStream } from './pipe.ts';
|
||||
import { getContentType } from './module/mime.ts';
|
||||
export const fileProxy = (req: http.IncomingMessage, res: http.ServerResponse, proxyApi: ProxyInfo) => {
|
||||
// url开头的文件
|
||||
const url = new URL(req.url, 'http://localhost');
|
||||
const [user, key, _info] = url.pathname.split('/');
|
||||
const pathname = url.pathname.slice(1);
|
||||
const { indexPath = '', target = '', rootPath = process.cwd() } = proxyApi;
|
||||
if (!indexPath) {
|
||||
return res.end('Not Found indexPath');
|
||||
}
|
||||
try {
|
||||
// 检测文件是否存在,如果文件不存在,则返回404
|
||||
let filePath = '';
|
||||
let exist = false;
|
||||
if (_info) {
|
||||
filePath = path.join(rootPath, target, pathname);
|
||||
exist = checkFileExists(filePath, true);
|
||||
}
|
||||
if (!exist) {
|
||||
filePath = path.join(rootPath, target, indexPath);
|
||||
exist = checkFileExists(filePath, true);
|
||||
}
|
||||
log.debug('filePath', { filePath, exist });
|
||||
|
||||
if (!exist) {
|
||||
res.statusCode = 404;
|
||||
res.end('Not Found File');
|
||||
return;
|
||||
}
|
||||
const ext = path.extname(filePath);
|
||||
let maxAge = 24 * 60 * 60 * 1000; // 24小时
|
||||
if (ext === '.html') {
|
||||
maxAge = 0;
|
||||
}
|
||||
let sendFilePath = path.relative(rootPath, filePath);
|
||||
const file = send(req, sendFilePath, {
|
||||
root: rootPath,
|
||||
maxAge,
|
||||
});
|
||||
file.pipe(res);
|
||||
} catch (error) {
|
||||
res.statusCode = 404;
|
||||
res.end('Error:Not Found File');
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
export const fileProxy2 = (req: http.IncomingMessage, res: http.ServerResponse, proxyApi: ProxyInfo) => {
|
||||
res.statusCode = 501;
|
||||
|
||||
@@ -26,16 +26,6 @@ export type ProxyInfo = {
|
||||
* @default false
|
||||
*/
|
||||
ws?: boolean;
|
||||
/**
|
||||
* type为file时有效
|
||||
* 索引文件,比如index.html, type为fileProxy代理有用 设置了索引文件,如果文件不存在,则访问索引文件
|
||||
*/
|
||||
indexPath?: string;
|
||||
/**
|
||||
* type为file时有效
|
||||
* 根路径, 默认是process.cwd(), type为fileProxy代理有用,必须为绝对路径
|
||||
*/
|
||||
rootPath?: string;
|
||||
s3?: {
|
||||
/**
|
||||
* 如何id存在,使用assistan-config的storage配置
|
||||
|
||||
Reference in New Issue
Block a user