更新依赖项,使用 Busboy 替代 formidable 处理文件上传,优化上传逻辑,改进权限检查

This commit is contained in:
2025-12-21 06:41:27 +08:00
parent 15fcfdad18
commit 8a633feb4f
12 changed files with 344 additions and 155 deletions

View File

@@ -9,4 +9,6 @@ export * from './get-content-type.ts'
export * from './utils.ts'
export { pipeFileStream, pipeStream } from './pipe.ts'
export { pipeFileStream, pipeStream } from './pipe.ts'
export { pipeBusboy } from './pipe-busboy.ts'

View File

@@ -0,0 +1,13 @@
import { isBun } from '@/utils/get-engine.ts';
import http from 'node:http';
export const pipeBusboy = async (req: http.IncomingMessage, res: http.ServerResponse, busboy: any) => {
if (isBun) {
// @ts-ignore
const bunRequest = req.bun.request;
const arrayBuffer = await bunRequest.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
busboy.end(buffer);
} else {
req.pipe(busboy);
}
}

View File

@@ -11,26 +11,36 @@ type ProxyOptions = {
export const UserV1Proxy = async (req: IncomingMessage, res: ServerResponse, opts?: ProxyOptions) => {
const { url } = req;
const { pathname } = new URL(url || '', `http://localhost`);
const [user, app, userAppKey] = pathname.split('/').slice(1);
let [user, app, userAppKey] = pathname.split('/').slice(1);
if (!user || !app || !userAppKey) {
opts?.createNotFoundPage?.('应用未找到');
return false;
}
const data = await App.handleRequest(req, res);
const loginUser = await getLoginUser(req);
if (!loginUser) {
opts?.createNotFoundPage?.('没有登录');
return false;
}
if (loginUser.tokenUser?.username !== user) {
const isAdmin = loginUser.tokenUser?.username === user
// TODO: 如果不是管理员,是否需要添加其他人可以访问的逻辑?
if (!isAdmin) {
opts?.createNotFoundPage?.('没有访问应用权限');
return false;
}
if (!userAppKey.startsWith(user + '-')) {
userAppKey = user + '-' + userAppKey;
}
logger.debug('data', data);
const client = wsProxyManager.get(userAppKey);
const ids = wsProxyManager.getIds();
if (!client) {
opts?.createNotFoundPage?.(`未找到应用 [${userAppKey}], 当前应用列表: ${ids.join(',')}`);
if (isAdmin) {
opts?.createNotFoundPage?.(`未找到应用 [${userAppKey}], 当前应用列表: ${ids.join(',')}`);
} else {
opts?.createNotFoundPage?.('应用访问失败');
}
return false;
}
const value = await client.sendData(data);