更新依赖项,使用 Busboy 替代 formidable 处理文件上传,优化上传逻辑,改进权限检查
This commit is contained in:
@@ -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'
|
||||
13
src/modules/fm-manager/pipe-busboy.ts
Normal file
13
src/modules/fm-manager/pipe-busboy.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user