perf
This commit is contained in:
2
src/module/logger.ts
Normal file
2
src/module/logger.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
import { Logger } from '@kevisual/logger/node';
|
||||
export const logger = new Logger();
|
||||
@@ -6,17 +6,13 @@ import { UserPermission, Permission } from '@kevisual/permission';
|
||||
import { getLoginUser } from '@/middleware/auth.ts';
|
||||
import busboy from 'busboy';
|
||||
import { getContentType } from '../get-content-type.ts';
|
||||
|
||||
const getAiProxy = async (
|
||||
req: IncomingMessage,
|
||||
res: ServerResponse,
|
||||
opts: {
|
||||
createNotFoundPage: (msg?: string) => any;
|
||||
},
|
||||
) => {
|
||||
import { OssBase } from '@kevisual/oss';
|
||||
import { parseSearchValue } from '@kevisual/router/browser';
|
||||
// import { logger } from '@/module/logger.ts';
|
||||
const getAiProxy = async (req: IncomingMessage, res: ServerResponse, opts: ProxyOptions) => {
|
||||
const { createNotFoundPage } = opts;
|
||||
const _u = new URL(req.url, 'http://localhost');
|
||||
|
||||
const oss = opts.oss;
|
||||
const pathname = _u.pathname;
|
||||
const params = _u.searchParams;
|
||||
const password = params.get('p');
|
||||
@@ -32,8 +28,8 @@ const getAiProxy = async (
|
||||
owner = user;
|
||||
}
|
||||
try {
|
||||
const stat = await minioClient.statObject(bucketName, objectName);
|
||||
if (stat.size === 0) {
|
||||
const stat = await oss.statObject(objectName);
|
||||
if (!stat) {
|
||||
createNotFoundPage('Invalid proxy url');
|
||||
return true;
|
||||
}
|
||||
@@ -57,7 +53,7 @@ const getAiProxy = async (
|
||||
'Content-Length': contentLength,
|
||||
etag,
|
||||
'last-modified': lastModified,
|
||||
'file-name': fileName,
|
||||
'x-file-name': fileName,
|
||||
...filterMetaData,
|
||||
};
|
||||
|
||||
@@ -90,11 +86,19 @@ export const getMetadata = (pathname: string) => {
|
||||
}
|
||||
return meta;
|
||||
};
|
||||
export const postProxy = async (req: IncomingMessage, res: ServerResponse, opts: { createNotFoundPage: (msg?: string) => any }) => {
|
||||
|
||||
export const postProxy = async (req: IncomingMessage, res: ServerResponse, opts: ProxyOptions) => {
|
||||
const _u = new URL(req.url, 'http://localhost');
|
||||
|
||||
const pathname = _u.pathname;
|
||||
const oss = opts.oss;
|
||||
const params = _u.searchParams;
|
||||
const force = !!params.get('force');
|
||||
const hash = params.get('hash');
|
||||
let meta = parseSearchValue(params.get('meta'), { decode: true });
|
||||
if (!hash && !force) {
|
||||
return opts?.createNotFoundPage?.('no hash');
|
||||
}
|
||||
let objectName = '';
|
||||
let owner = '';
|
||||
const { user, app } = getUserFromRequest(req);
|
||||
@@ -110,22 +114,48 @@ export const postProxy = async (req: IncomingMessage, res: ServerResponse, opts:
|
||||
if (loginUser?.tokenUser?.username !== owner) {
|
||||
return opts?.createNotFoundPage?.('no permission');
|
||||
}
|
||||
const bb = busboy({ headers: req.headers });
|
||||
const end = (data: any, message?: string, code = 200) => {
|
||||
res.writeHead(code, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ code: code, data: data, message: message || 'success' }));
|
||||
};
|
||||
if (!force) {
|
||||
const check = await oss.checkObjectHash(objectName, hash);
|
||||
if (check) {
|
||||
return end({ success: true, hash }, '文件已存在');
|
||||
}
|
||||
}
|
||||
const bb = busboy({
|
||||
headers: req.headers,
|
||||
limits: {
|
||||
fileSize: 100 * 1024 * 1024, // 100MB
|
||||
files: 1,
|
||||
},
|
||||
});
|
||||
let fileProcessed = false;
|
||||
bb.on('file', async (name, file, info) => {
|
||||
fileProcessed = true;
|
||||
try {
|
||||
await minioClient.putObject(bucketName, objectName, file, undefined, {
|
||||
...getMetadata(pathname),
|
||||
});
|
||||
await oss.putObject(
|
||||
objectName,
|
||||
file,
|
||||
{
|
||||
...getMetadata(pathname),
|
||||
...meta,
|
||||
},
|
||||
{ check: true, isStream: true },
|
||||
);
|
||||
end({ success: true, name, info }, '上传成功', 200);
|
||||
} catch (error) {
|
||||
end({ error: error }, '上传失败', 500);
|
||||
}
|
||||
});
|
||||
const end = (data: any, message?: string, code = 200) => {
|
||||
res.writeHead(code, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ code: code, data: data, message: message || 'success' }));
|
||||
};
|
||||
bb.on('finish', end);
|
||||
|
||||
bb.on('finish', () => {
|
||||
// 只有当没有文件被处理时才执行end
|
||||
if (!fileProcessed) {
|
||||
end({ success: false }, '没有接收到文件', 400);
|
||||
}
|
||||
});
|
||||
bb.on('error', (err) => {
|
||||
console.error('Busboy 错误:', err);
|
||||
end({ error: err }, '文件解析失败', 500);
|
||||
@@ -133,13 +163,15 @@ export const postProxy = async (req: IncomingMessage, res: ServerResponse, opts:
|
||||
|
||||
req.pipe(bb);
|
||||
};
|
||||
export const aiProxy = async (
|
||||
req: IncomingMessage,
|
||||
res: ServerResponse,
|
||||
opts: {
|
||||
createNotFoundPage: (msg?: string) => any;
|
||||
},
|
||||
) => {
|
||||
type ProxyOptions = {
|
||||
createNotFoundPage: (msg?: string) => any;
|
||||
oss?: OssBase;
|
||||
};
|
||||
export const aiProxy = async (req: IncomingMessage, res: ServerResponse, opts: ProxyOptions) => {
|
||||
const oss = new OssBase({ bucketName, client: minioClient });
|
||||
if (!opts.oss) {
|
||||
opts.oss = oss;
|
||||
}
|
||||
if (req.method === 'POST') {
|
||||
return postProxy(req, res, opts);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user